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.