I\'m new to maven, and also to MacOS.
I have setup maven in my terminal, and when getting the version settings (using mvn -v
) it seems it uses JDK 1.6,
I am late to this question, but I think the best way to handle JDK versions on MacOS is by using the script described at: http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/
It helped me. Just add it in your pom.xml.
By default maven compiler plugin uses Java 1.5 or 1.6, you have to redefine it in your pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
You can also do,
<properties>
...
<!-- maven-compiler-plugin , aka JAVA Compiler 1.7 -->
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
...
</properties>
Please check the compatibility. I struggled with mvn 3.2.1
and jdk 1.6.0_37
for many hours. All variables were set but was not working. Finally I upgraded jdk to 1.8.0_60
and mvn 3.3.3
and that worked. Environment Variables as following:
JAVA_HOME=C:\ProgramFiles\Java\jdk1.8.0_60
MVN_HOME=C:\ProgramFiles\apache-maven\apache-maven-3.3.3
M2=%MVN_HOME%\bin extend system level Path- ;%M2%;%JAVA_HOME%\bin;
add the following to your ~/.mavenrc
:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/{jdk-version}/Contents/Home
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
@MasterGaurav 's solution is perfectly working,
I normally put the java switch thing into a zsh function:
alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'
function java_use() {
export JAVA_HOME=$(/usr/libexec/java_home -v $1)
echo export "JAVA_HOME=$(/usr/libexec/java_home -v $1)" > ~/.mavenrc
export PATH=$JAVA_HOME/bin:$PATH
java -version
}
Then you can run java_ls
two get all of the available jvm on your machine,
run java_use 1.7
to use the 1.7 version for both java and maven.