Maven fix to use default Java version on Mac OS X
Recently I installed Maven 3.2.1 on Mac OS X and discovered that Maven did not use my default Java version (set to 1.7). Instead, it was using Java version 1.6.
The java -version
command was giving following output:
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
Maven’s version command (mvn --version
) was giving output like this:
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T17:37:52+00:00)
Maven home: /usr/local/apache-maven-3.2.1
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.9.3", arch: "x86_64", family: "mac"
A quick look at Maven start-up command ($M2_HOME/bin/mvn
) and you can see that during start-up, Maven reads two files /etc/mavenrc
and ~/.mavenrc
. All I had to do was to create one of these files (I chose /etc/mavenrc
to apply the setting for all system users) and assign Java 1.7 home directory path to JAVA_HOME
environment variable there.
Mac OS X has /usr/libexec/java_home
command that returns a path suitable for setting the JAVA_HOME
environment variable. You can find more options from manual pages: man java_home
.
So my /etc/mavenrc
looks like this:
JAVA_HOME=`/usr/libexec/java_home`
And after that change Maven uses the correct version of Java: mvn --version
returns:
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T17:37:52+00:00)
Maven home: /usr/local/apache-maven-3.2.1
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.3", arch: "x86_64", family: "mac"