Specify JDK for Maven to use
Solution 1
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.
Solution 2
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>
Solution 3
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.
Solution 4
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.
Solution 5
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.
Related videos on Youtube
DanInDC
Updated on October 21, 2021Comments
-
DanInDC over 2 years
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 asettings.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?
-
700 Software about 3 yearsIntelliJ-specific answer: stackoverflow.com/questions/25888023/…
-
-
Devanshu Mevada about 14 yearsThe OP is building an hudson plugin on the command line, not under hudson (at least, this is my understanding).
-
acdcjunior almost 10 yearsExample in windows:
set JAVA_HOME="C:\Java\jdk7"
-
Sergey Ushakov over 9 yearsThis is just the starting point, but not the solution. This is the requirement for the maven compiler plugin to compile for 1.7. And then the trick is to make maven really capable of compiling for 1.7, which is not so trivial if your current java version is different...
-
Jin Kwon over 9 yearsVoted up! I found I can use
-Dmaven.compiler.fork=true
and-Dmaven.compiler.executable=/path/to/target/javac
in command line. -
Paul Gregoire over 9 yearseven using those java-opts, you still must add this to the compiler plugin <executable>${maven.compiler.executable}</executable>
-
Enrique San Martín about 9 yearsin lubuntu: JAVA_HOME="/home/desa/programas/jdks/jdk1.6.0_45/" mvn -v
-
RedYeti about 9 yearsAnd in case anyone else forgets and spends ages wondering why they can't change it:
JAVA_HOME
can be set for Maven (on Mac at least) in this file:/private/etc/mavenrc
- And that can use something like (note the backticks not single quotes!):export JAVA_HOME=`/usr/libexec/java_home -v 1.7.0_75`
-
Christophe Roussy about 8 yearsUnix:
export JAVA_HOME='D:/dev/java/jdk8/jre'
(works for me) -
bojingo about 8 yearsThis was perfect for specifying the exact version of the JDK. For example, I have a project that fails when using the initial JDK 1.8, but if using JDK 1.8.0_77, it works just fine. I had both JDKs installed, and with this solution maven told me I was using the wrong version of 1.8 until I changed my JAVA_HOME path to target the specific 1.8.0_77 folder. The other answers did not allow you to be so granular on the version.
-
Gaëtan Lehmann about 8 years@JinKwon Passing options with -D works fine, even without defining it in the compiler plugin section. This is nice for occasional use or for scripting. You should put it in a separate answer so we can vote it up!
-
Dave over 7 yearsI appreciate @Cerber's answer below, but in the end, this is just simpler and avoids any more subtle errors. It also mimicks the way CI servers like Jenkins essentially do it.
-
Paul Verest over 7 yearsThat is other question stackoverflow.com/questions/16723533/…
-
edwin almost 7 yearsThis solution failed when I try to run tests in Java 8 I was getting *Unsupported major.minor version 52.0 *
-
ocramot over 5 yearsif I change JAVA_HOME and then I do
java -version
, it still prints the previous version. -
Romano over 5 yearsA Maven Update might be required after adding these lines (Eclipse: Right click on the project, Maven, Update project)
-
Bruce Adams about 5 yearsBased on this answer I run the following before executing maven:
export JAVA_HOME=`java -XshowSettings:properties -version 2>&1 | grep java.home | awk '{ print $3; }'`
-
Enrico Giurin over 4 yearsSetting maven.compiler properties doesn't prevent the code from using methods added in java 11. For instances String.repeat(). This when building with jdk 11. So the maven builds but I wish it would fail
-
Martin about 4 yearsTo change
JAVA_HOME
for a single invocation without having to reset it, useJAVA_HOME="C:\Program Files\jdk-13.0.2" mvn --version
-
Enrique S. Filiage about 4 yearsI've also needed to overwrite my
JAVA_HOME
variable. For example (in bash shell):JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable=/usr/lib/jvm/java-8-openjdk-amd64/bin/javac spring-boot:run
-
RSharma almost 4 yearsThanks for the jenv maven recommendation.
-
barfoos almost 3 yearsHighly underrated comment, with this I was able to persistently set the JAVA_HOME version in RHEL 8
-
Sumit Jha almost 3 yearsupvoted. exactly what I needed. Thanks mate :)
-
Catherine almost 3 yearsHow to force Maven to run itself using JDK 11 and build project using JDK 1.5 ?
-
mcwayliffe over 2 years@ocramot That's because when you run
java -version
your shell is looking up the location of thejava
program in yourPATH
environment variable. Maven, on the other hand, is looking up where your JRE is via theJAVA_HOME
environment variable. -
MADforFUNandHappy almost 2 yearsAlso worked perfectly on MacOS 12.4 with latest and version 11 of openjdk installed through homebrew