How to set specific Java version to Maven

Edit:

A better solution is presented by the answer from Ondrej, which obviates remembering aliases.


Original Answer:

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.


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.

  • You can set the JAVA_HOME parameter just before you start maven (and change it back afterwards if need be).
  • You could also go into your mvn(non-windows)/mvn.bat/mvn.cmd(windows) and set your java version explicitly there.