How to set custom version of jdk in maven? - java

I am new in Maven.
I work in Windows and when I try to do the next instructions mvn clean install in folder with pom.xml file it throw me errors like this:
CLASS_NAME.java error: diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
As I can understand from the message it occurs because maven use jdk version 1.5 (Actually I even didn't install it). In maven settings folder I found toolchains.xml file. If I understood right, it is possible to set custom version of jdk for user using this file. So I add this code to my pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<configuration>
<toolchains>
<jdk>
<version>[1.8]</version>
</jdk>
</toolchains>
</configuration>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
</plugin>
and this to toolchains.xml
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>C:/Program Files/Java/jdk1.8.0_45</jdkHome>
</configuration>
</toolchain>
If somebody know can you tell me how to fix this? I will appreciate any help, idea or explanation.
P.S. JAVA_HOME is C:\Program Files\Java\jdk1.8.0_45

Try configuring the maven-compiler-plugin instead of the maven-toolchains-plugin in your pom.xml.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Source

Related

Java google checkstyle Maven

I'm trying to configure my Maven project to use google java check style with the following configuration:
google_checks.xml: https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>google_checks.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<configLocation>google_checks.xml</configLocation>
<failOnViolation>false</failOnViolation>
<enableFilesSummary>false</enableFilesSummary>
</configuration>
</plugin>
</plugins>
</reporting>
It seems to run mvn checkstyle:check fine at first. But after a few runs I start getting the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check
(default-cli) on project PROJECT: Failed during checkstyle configuration: cannot initialize
module TreeWalker - Token "METHOD_REF" was not found in Acceptable tokens list in check
com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck -> [Help 1]
What does that mean? Why does it only happen some times and how do I get rid of it?
Token "METHOD_REF" was not found in Acceptable tokens list in check
com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck
You are trying to use a newer configuration with an old version of Checkstyle.
The configuration at https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml is in master which is dependent on the snapshot version of checkstyle.
If you are using google configuration without any modifications, you need to use the one that comes embedded in checkstyle. See https://stackoverflow.com/a/35486365/1016482
Otherwise you can integrate a newer version of checkstyle to work with maven. See https://stackoverflow.com/a/27359107/1016482
I was using version 3.0.0 (which is the newest one right now) of the maven-checkstyle-plugin and I still got the error. I solved it by adding the following dependency to the plugin.
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.11</version>
</dependency>

Maven How to show warnings of codes when compiling

I'm using maven to build a new project. There are some warnings in my codes which are underlined by yellow line. I wish maven could report these warnings in console. How can I accomplish that? Can I just add some parameters in the command such as mvn -XXX clean compile?
<showDeprecation>
Sets whether to show source locations where deprecated APIs are used.
Default value is: false
User property is: maven.compiler.showDeprecation
<showWarnings>
Set to true to show compilation warnings.
Default value is: false
User property is: maven.compiler.showWarnings
-- Maven Doku
As simple as mvn clean install -Dmaven.compiler.showDeprecation=true -Dmaven.compiler.showWarnings=true all in one line.
You could even remove the =true part because, when you add a maven parameter with -D it automatically sets its value to true, if not set to something else.
With maven 3.3.9, using the maven-compiler-plugin and Java 1.8, you need a <configuration> section like the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
I got this answer from here when none of the above worked for me: http://frequal.com/java/EnableWarningsInMaven.html
I am guessing you are using the compiler plugin. Have you tried this?
Use the maven compiler plugin in your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-Xlint:all</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
To get to see detailed logs in maven you can use the command line option
-X, --debug Produce execution debug output
e.g this can be used as
mvn clean install -X
Also if what you want to get in the logs is the compiler warnings (The value of the local variable a is not used). You can try modifying your project's pom.xml with the following -
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
... other plugins
</plugins>
</build>

Jenkins vs SonarQube : Run with specific JDK

I have a project which is built and compiled with JDK 1.7 and Sonarqube 6.0 which only runs with JDK 1.8.
On Jenkins dashboard, I set Goal: :org.codehaus.mojo:sonar-maven-plugin:LATEST:sonar and on wrapper.conf on sonarqube folder, i changed wrapper.java.command=C:\Program Files\Java\jdk1.8.0_91\bin\java, Jenkins JDK is set 1.7....but Sonar doesn't work with JDK 1.8.
Please help give me advise.
I had a similar problem.
The solution was to set JDK8 to be used in the Job-Configuration in Jenkins and set JDK7 to be used for the compilation of the sources, the test-sources and surefire plugin.
Something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testSource>1.7</testSource>
<testTarget>1.7</testTarget>
<verbose>true</verbose>
<fork>true</fork>
<executable>C:\java\jdk1.7.0_25\bin\javac</executable>
<compilerVersion>1.7</compilerVersion>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<fork>true</fork>
<executable>C:\java\jdk1.7.0_25\bin\javac</executable>
<source>1.5</source>
<target>1.5</target>
<compilerVersion>1.7</compilerVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<jvm>C:\java\jdk1.7.0_25\bin\java</jvm>
<forkMode>once</forkMode>
</configuration>
</plugin>
If it works for you, you can set the Path to the JDK in settings.xml and use that setting in you pom.xml so each environment/developer can use their own JDK.
<profile>
<id>jdk7</id>
<properties>
<JDK_1_7_HOME>C:\java\jdk1.7.0_25</JDK_1_7_HOME>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
And your pom.xml
...
<executable>${JDK_1_7_HOME}/bin/javac</executable>
...

Could not find dependencies for aspectj-maven-plugin

I have a problem with CTW aspects using aspectj-maven-plugin. I get the following error (execution entry is being highlighted):
Multiple annotations found at this line:
- Execution default of goal org.codehaus.mojo:aspectj-maven-plugin:1.5:compile failed: Plugin
org.codehaus.mojo:aspectj-maven-plugin:1.5 or one of its dependencies could not be resolved: Could not find artifact
com.sun:tools:jar:1.7.0_21 at specified path C:\Program Files\Java\jre7/../lib/tools.jar (org.codehaus.mojo:aspectj-maven-
plugin:1.5:compile:default:compile)
- Execution default of goal org.codehaus.mojo:aspectj-maven-plugin:1.5:test-compile failed: Plugin
org.codehaus.mojo:aspectj-maven-plugin:1.5 or one of its dependencies could not be resolved: Could not find artifact
com.sun:tools:jar:1.7.0_21 at specified path C:\Program Files\Java\jre7/../lib/tools.jar (org.codehaus.mojo:aspectj-maven-
plugin:1.5:test-compile:default:test-compile)
On the configuration:
<build>
<plugins>
<!-- http://mojo.codehaus.org/aspectj-maven-plugin/usage.html -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<outxml>true</outxml>
<source>1.7</source>
<target>1.7</target>
<sources>
<source>
<basedir>src/main/java</basedir>
<includes>
<include>**/*Aspect.java</include>
</includes>
</source>
</sources>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
What am I doing wrong? It looks like as if this plugin was unable to find jdk? But why?
Is your JAVA_HOME set properly? Please check that. It worked perfectly for me. So I think you should add below mentioned plugin and try:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Run mvn compile after that.
Please check the JAVA_HOME env variable. This happened to me when JAVA_HOME is pointed to JRE folder rather than jdk folder.
I had this problem running with java 11, seems like it is only compatible with java 8.
Looking into the project, aspectj-maven-plugin it looks like the update was committed but never actually merged.

Maven project dependency against JDK version

I have projects that need to be build with a specific version of the JDK.
The problem isn't in the source and target parameters but in the jars of the runtime used during compilation.
In some cases I get a compilation error if I try to compile with the wrong JDK, but sometimes the build is successful and I get runtime errors when using the jars.
For example in eclipse I have the ability to establish the execution enviroment for the project in the .classpath file.
Is there a way to handle such situation in maven?
What I would like to have is the ability to handle JRE dependency like other dependencies of the project in the POM file.
UPDATE:
The accepted solution was the best one when I asked this question, so I won't change it. Meanwhile a new solution to this kind of problems has been introduced: Maven Toolchain. Follow the link for further details.
I've found this article:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_1_4_HOME}/bin/javac</executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
I have projects that need to be build with a specific version of the JDK.
You can use the Maven Enforcer plugin to enforce the use of a particular version of the JDK:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>1.5</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
But I'm not sure I really understood the question. If this is not what you want, maybe you could declare your JDK specific dependencies in profiles and use an activation trigger based on the JDK version. For example:
<profiles>
<profile>
<activation>
<jdk>1.5</jdk>
</activation>
...
</profile>
</profiles>
This configuration will trigger the profile when the JDK's version starts with "1.5".
I believe that this can be solved with following plugin in your pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Here you target version 1.6 , or write your own version

Categories