I'm experiencing an odd behavior in maven as well as in eclipse itself.
Even though i configured my project to be compiled in Java 1.8, I can compile and run (eclipse) a piece of code that was introduced in Java 9
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
The code line in question:
LocalTime.ofInstant(cal.toInstant(), cal.getTimeZone().toZoneId());
I'm using Oracle's JDK 11 locally for compiling and running in eclipse without any errors. When i package it into a docker container using openjdk:8-jdk-alpine it will boot up, but throw the following Exception when I call the method:
java.lang.NoSuchMethodError: java.time.LocalTime.ofInstant(Ljava/time/Instant;Ljava/time/ZoneId;)Ljava/time/LocalTime
How can I avoid and identify these situations before they go to testing? Am I doing something wrong or is it a bug in the build system or in JDK11?
thanks in advance
The source option specifies that the source code must be compatible with Java 8, the target option that the classes should be compatible with Java 8. However, you will still compile with the Java 11 class library if you build with Java 11 and then you can get errors like the one you have.
There are two good solutions. One is to use the Maven toolchains plugin and build with Java 8. Then you can have multiple Java versions installed and Maven will use the configured one on a per-project basis.
The other is to use the new release and testRelease options. They will build with API classes from the given release. Just add <release>1.8</release>.
If you are using JDK 11, configure your maven pom.xml like that:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
I am trying to build a Hudson plugin I've modified and it requires jdk1.6. This is fine, but I don't see how I can tell maven where the different jdk is. I've found few mentions on the internet but they don't seem to apply to me. Some suggest adding some config to .m2/settings.xml but I don't have a settings.xml. Plus, I don't want to use 1.6 for all maven builds.
One kink is I am using mvn in cygwin, if that matters at all. It appears I should be able to make the specification in the project pom file, but the existing pom is pretty bare.
So bottom line is, is there a way to specify a jdk for a single invocation of maven?
So bottom line is, is there a way to specify a jdk for a single invocation of maven?
Temporarily change the value of your JAVA_HOME environment variable.
Seems that maven now gives a solution here : Compiling Sources Using A Different JDK
Let's say your JAVA_HOME points to JDK7 (which will run maven processes)
Your pom.xml could be :
<build>
<plugins>
<!-- we want JDK 1.6 source and binary compatiblility -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- ... -->
<!-- we want sources to be processed by a specific 1.6 javac -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_1_6_HOME}/bin/javac</executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
</plugins>
</build>
If your developpers just add (and customize) the following lines in their settings.xml, your pom will be platform independant :
<settings>
[...]
<profiles>
[...]
<profile>
<id>compiler</id>
<properties>
<JAVA_1_4_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_4_HOME>
<JAVA_1_6_HOME>C:\Program Files\Java\j2sdk1.6.0_18</JAVA_1_6_HOME>
</properties>
</profile>
</profiles>
[...]
<activeProfiles>
<activeProfile>compiler</activeProfile>
</activeProfiles>
</settings>
compile:compile has a user property that allows you to specify a path to the javac.
Note that this user property only works when fork is true which is false by default.
$ mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable=/path/to/the/javac compile
You might have to double quote the value if it contains spaces.
> mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable="C:\...\javac" compile
See also Maven custom properties precedence.
As u said "Plus, I don't want to use 1.6 for all maven builds."....So better I will say modify your pom file and specify which jdk version to use.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
</plugins>
</build>
It will ensure that your particular project uses that version of jdk.
I say you setup the JAVA_HOME environment variable like Pascal is saying:
In Cygwin if you use bash as your shell should be:
export JAVA_HOME=/cygdrive/c/pathtothejdk
It never harms to also prepend the java bin directory path to the PATH environment variable with:
export PATH=${JAVA_HOME}/bin:${PATH}
Also add maven-enforce-plugin to make sure the right JDK is used. This is a good practice for your pom.
<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.6</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Please, see Maven Enforcer plugin – Usage.
If you have installed Java through brew in Mac then chances are you will find your Java Home Directory here:
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
The next step now would be to find which Java Home directory maven is pointing to. To find it type in the command:
mvn -version
The fields we are interested in here is:
Java version and runtime.
Maven is currently pointing to Java 13. Also, you can see the Java Home path under the key runtime, which is:
/usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home
To change the Java version of the maven, we need to add the Java 8 home path to the JAVA_HOME env variable.
To do that we need to run the command:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
in the terminal.
Now if we check the maven version, we can see that it is pointing to Java 8 now.
The problem with this is if you check the maven version again in the new terminal, you will find that it is pointing to the Java 13. To avoid this I would suggest adding the JAVA_HOME variable in the ~/.profile file.
This way whenever your terminal is loading it will take up the value you defined in the JAVA_HOME by default. This is the line you need to add in the ~/.profile file:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
You can open up a new terminal and check the Maven version, (mvn -version) and you will find it is pointing to the Java 8 this time.
I know its an old thread. But I was having some issues with something similar to this in Maven for Java 8 compiler source. I figured this out with a quick fix mentioned in this article thought I can put it here and maybe can help others:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Maven uses variable $JAVACMD as the final java command, set it to where the java executable is will switch maven to different JDK.
You could also set the JDK for Maven in a file in your home directory ~/.mavenrc:
JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-11.0.5.jdk/Contents/Home'
This environment variable will be checked by the mvn script and used when present:
if [ -f "$HOME/.mavenrc" ] ; then
. "$HOME/.mavenrc"
fi
https://github.com/CodeFX-org/mvn-java-9/tree/master/mavenrc
Hudson also allows you to define several Java runtimes, and let you invoke Maven with one of these. Have a closer look on the configuration page.
Yet another alternative to manage multiple jdk versions is jEnv
After installation, you can simply change java version "locally" i.e. for a specific project directory by:
jenv local 1.6
This will also make mvn use that version locally, when you enable the mvn plugin:
jenv enable-plugin maven
I update my ~/.m2/settings.xml
<settings>
<profiles>
<profile>
<id>j8</id>
<profile>
<id>j8</id>
<properties>
<maven.compiler.fork>true</maven.compiler.fork>
<maven.compiler.executable>${env.JAVA_HOME8}/bin/javac.exe</maven.compiler.executable>
</properties>
</profile>
<profile>
<id>j11</id>
<properties>
<maven.compiler.fork>true</maven.compiler.fork>
<maven.compiler.executable>${env.JAVA_HOME11}/bin/javac.exe</maven.compiler.executable>
</properties>
</profile>
</profiles>
<settings>
For build with Java8 I run mvn with properties:
mvn compile -Pj8
and for Java 11
mvn compile -Pj11
If nothing else works and even after you set JAVA_HOME to a correct path, check if there is no override of the JAVA_HOME path in <user>/.mavenrc!
As a further tip, the mvn file is a bash script (on Linux).. so if necessary you can inspect the source [and change it].
I had build problem with maven within Eclipse on Windows 7.
Though I observed mvn build was running just fine from command line.
mvn -T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug --settings ..\..\infra-scripts\maven-conf\settings.xml > output.log
Eclipse was considering as default JVM a JRE installation instead of JDK so it was failing on compilation.
I added to eclipse.ini following line:
-vm
C:\Program Files (x86)\Java\jdk1.8.0_25\bin
Also when starting from eclipse I used in "Goals" section following list:
-T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug --settings ..\..\infra-scripts\maven-conf\settings.xml
Compilation error got solved.
For Java 9 :
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
I have Java maven project with TestNG
Please see this pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</executable>
</configuration>
</plugin>
So inside Windows when I want to run this project from command line I just navigate into this pom.xml folder and then:
mvn clean test
And this will start all my tests.
Now inside this pom.xml i have my javac.exe path so in order to run this project in MAC what I need to add/ change? (I want it to support both OS)
The best practice would probably be to rely on the standard JAVA_HOME environment variable:
<executable>${env.JAVA_HOME}/bin/java</executable>
I was trying to install the dependency packages into my location maven repository with running mvn clean install from my maven project.
But unfortunately. I got a compile error for some source files which said :annotations are not supported in -source 1.3(use -source 5 or higher to enable annotations)#override.
After researching. I found I need to specify the source version of my maven project.
So I add the following configuration in the POM.
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
Actually. It does work fine. But what makes me confused is where does the JDK1.6 comes from and make the Maven compile work ? I didn't have the JDK1.6 install in my computer(Currently, the available JDK installed in my computer is JDK1.8), Thanks.
Later versions of Java allow you to compile your program as earlier versions. Since you have Java 8 installed you can compile your program as a Java 6 one that will run on a machine with only the 1.6 JRE. The limitation is that you can't use any of the language features added in the Java 7 and 8 releases.
I work on a Java program that should be compatibe with Java 5. I had #Override annotations on implemented interface methods which is allowed in Java 6, but not in 5. I use a Java 6 SDK. Eclipse correctly gives error messages on the #Override when I set it to 5.0 compliance. My Maven build, however, runs without even a warning, using the following settings in my POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
Am I correct in expecting that this should actually make the build fail? Why doesn't it, and is there something I can do?
This is actually a JDK problem, not a Maven problem. The #Override annotation is not failing with a -source 1.5 flag to to a 1.6 javac. Go ahead and try it. The only way to make it fail, is, unfortunately, to use a 1.5 javac.
Sorry, HTH.
EDIT
I ran into this problem myself, and I also wondered if it's actually looking at the setting in the pom. Turning on debug output (-X I believe, was a while ago) will print the javac command line to the standard output, and you'll see that it is indeed using the -source 1.5 parameter.
As roe's answer says you need to use a 1.5 compiler because the JDK isn't doing its job quite right. It's worth pointing out that you can avoid messing about with paths etc. by tweaking the maven-compiler-plugin configuration to use a specific compiler:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_1_5_HOME}/bin/javac</executable>
<compilerVersion>1.5</compilerVersion>
</configuration>
</plugin>
...
</plugins>
You can then specify the path to the compiler in your project or settings.xml
<properties>
<JAVA_1_5_HOME>/path/to/1.5jdk</JAVA_1_5_HOME>
</properties>