Maven uses the JAVA_HOME
parameter to find which Java version it is supposed to run. I see from your comment that you can't change that in the configuration.
JAVA_HOME
parameter just before you start maven (and change it back afterwards if need be). mvn
(non-windows)/mvn.bat
/mvn.cmd
(windows) and set your java version explicitly there.On windows
If you do not want to change your JAVA_HOME
variable inside the system variables.
Edit your mvn.bat
file and add a line like this
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45\jre
This can be done after @REM ==== START VALIDATION ====
like mentionned by @Jonathan
On Mac (& Linux ?)
If you do not want to change your JAVA_HOME
variable inside your ~/.bashrc
or ~/.bash_profile
you can create a ~/.mavenrc
file and redefine your JAVA_HOME
using the java_home tool
export JAVA_HOME=`/usr/libexec/java_home -v 1.7.0_45`
Sanity Check
You can verify that everything is working fine by executing the following commands. The jdk version should be different.
mvn -version
then
java -version
You could configure compiling sources using different JDK with maven-compiler-plugin
.
Just specify path to javac
in <executable>
tag. E.g for java11 it looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk-11.0.1\bin\javac</executable> <!--PATH TO JAVAC -->
</configuration>
</plugin>
Also you can have two versions of maven installed, and edit one of them, editing here:
mvn(non-windows)/mvn.bat/mvn.cmd(windows)
replacing your %java_home% appearances to your java desired path. Then just execute maven from that modified path
On Linux/Unix, the JAVA_HOME within 'mvn' shell script is overridden by the settings in
$HOME/.mavenrc
please Where to add JAVA_HOME and MAVEN path variables in linux
On Macs, (assuming you have the right version installed)
JAVA_HOME=`/usr/libexec/java_home -v 1.8` mvn clean install -DskipTests
In the POM, you can set the compiler properties, e.g. for 1.8:
<project>
...
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
...
</project>
You can set Maven to use any java version following the instructions below.
Install jenv in your machine link
Check the available java versions installed in your machine by issuing the following command in command line.
jenv versions
You can specify global Java version using the following command.
jenv global oracle64-1.6.0.39
You can specify local Java version for any directory(Project) using the following command in the directory in command line.
jenv local oracle64-1.7.0.11
add the correct java version in your pom.xml
if you are running maven in command line install jenv maven plugin using below command
jenv enable-plugin maven
Now you can configure any java version in your machine to any project with out any trouble.
Adding my two cents and explicitly providing the solution.
I have two JDKs installed on my Windows Machine - JDK 1.5
and JDK 1.6
.
My default (and set to windows system environment variable) JAVA_HOME
is set to JDK 1.5
.
However, I have a maven project that I need to build (i.e., JBehave Tutorial's Etsy.com) using JDK 1.6
.
My solution in this scenario (which worked!), is as suggested by @DanielBarbarian
to set it in mvn.bat
.
For some not familiar with window's batch file, I just basically added the set JAVA_HOME=<path_to_other_jdk>
line after @REM ==== START VALIDATION ====
in mvn.bat
(i.e., %MAVEN_HOME%\bin\mvn.bat
):
@REM ==== START VALIDATION ====
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45\jre
if not "%JAVA_HOME%" == "" goto OkJHome
Adding a solution for people with multiple Java versions installed
We have a large codebase, most of which is in Java. The majority of what I work on is written in either Java 1.7 or 1.8. Since JAVA_HOME
is static, I created aliases in my .bashrc
for running Maven with different values:
alias mvn5="JAVA_HOME=/usr/local/java5 && mvn"
alias mvn6="JAVA_HOME=/usr/local/java6 && mvn"
alias mvn7="JAVA_HOME=/usr/local/java7 && mvn"
alias mvn8="JAVA_HOME=/usr/local/java8 && mvn"
This lets me run Maven from the command line on my development machine regardless of the JDK version used on the project.
Edit: A better solution is presented by the answer from Ondrej, which obviates remembering aliases.
On windows, I just add multiple batch files for different JDK versions to the Maven bin folder like this:
mvn11.cmd
@echo off
setlocal
set "JAVA_HOME=path\to\jdk11"
set "path=%JAVA_HOME%;%path%"
mvn %*
then you can use mvn11
to run Maven in the specified JDK.
One simple solution to the problem -
JAVA_HOME=/usr/lib/jvm/java-6-sun/ mvn clean install
On Mac, it would look something like -
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/ mvn clean install
PS: One special case that i found is the above given command does not work on 'fish' shell. I also had bash shell available and it worked fine there. just use command 'bash' to switch to bash shell.
I did not have success on mac with just setting JAVA_HOME
in the console but I was successful with this approach
~/.mavenrc
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
Without changing Environment Variables, You can manage java version based on the project level by using Maven Compiler Plugin.
Method 1
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Method 2
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
I just recently, after seven long years with Maven, learned about toolchains.xml. Maven has it even documented and supports it from 2.0.9 - toolchains documentation
So I added a toolchain.xml file to my ~/.m2/ folder with following content:
<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/opt/java8</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.7</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/opt/java7</jdkHome>
</configuration>
</toolchain>
</toolchains>
It allows you to define what different JDKs Maven can use to build the project irrespective of the JDK Maven runs with. Sort of like when you define JDK on project level in IDE.
To avoid any impact to your project and to your Environment Variables, you can configure the Maven Compiler Plugin just to the project's POM, specifying the Source and Target java version
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
...
</plugins>
I've used the base idea from @Jonathan. I've set the windows with: set JAVA_HOME=C:\Program Files\java\AdoptOpenJDK-11.0.8+10 call mvn clean package -DskipTests
Source: Stackoverflow.com