The default Java compiler version used by Maven is Java 1.5 .
Before moving on, we can check the default JDK version of Maven. Running the mvn -v command will show the Java version in which Maven runs.
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.A better solution is presented by the answer from Ondrej, which obviates remembering aliases.
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.
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>
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With