Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Configure Maven 3.2 to use JDK v1.7 under Mac OS X 10.9

Sharing is caring!

In this article i describe how to configure Maven 3.2 to use JDK v1.7 under Mac OS X 10.9
Step 1: Download Maven
Go ahead and download the latest Maven version here. For this tutorial just download the binaries: apache-maven-3.2.1-bin.tar.gz.

Step 2: Install Maven
Once you have downloaded the zipped file extract it. There should be a folder called apache-maven-version (apache-maven-3.2.1 in my case) with contents looking something like this:
- apache-maven-3.2.1
--- bin
--- boot
--- conf
--- lib
--- LICENSE
--- NOTICE
--- README.txt

We will install Maven in ‘/usr/local/apache-maven’ So go ahead and navigate to your /usr/local/ directory. You can do this by either using the Terminal using

cd /usr/local/

or just typing ‘command-shift-g’ in the Finder and then entering ‘/usr/local/’ in the input field.

Next create a new directory called ‘apache-maven’ in your /usr/local/ directory and move the
‘apache-maven-version’ folder into the newly created ‘apache-maven’ directory. So you will have
a directory structure like follows: /usr/local/apache-maven/apache-maven-3.2.1/

Open a new Terminal window and type in:

sudo pico .bash_profile

enter your password and edit your profile file to contain the following:

export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.1
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

Hit ‘control-o’ and then enter to save and finally ‘control-x’ to exit the editor.

Step 3: Install Java v1.7
Go to the official Oracle website and download the latest version of Java SE Kit here.

Mount the .dmg file and double click the Install Package to install the latest version of Java(version 1.7.0_60 in my case).

Java will be installed under ‘/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home’ in Mac OS X 10.9 (Mavericks). You can find this out by typing

/usr/libexec/java_home -v 1.7

in your Terminal.

Step 4: Configure profile file
Finally we need to set the Java home directory. Again type in the following in the Terminal:

sudo pico .bash_profile

authenticate and then enter the following:

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=${JAVA_HOME}/bin:$PATH

So our final .bash_profile file looks like so:

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=${JAVA_HOME}/bin:$PATH

export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.1
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

Again hit ‘control-o’ and then enter to save and finally ‘control-x’ to exit the editor.

Step 5: Verify that everything went smoothly
Run

mvn --version 

to verify that Maven is correctly installed. You should see something like:

Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T18:37:52+01:00)
Maven home: /usr/local/apache-maven/apache-maven-3.2.1
Java version: 1.7.0_60, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.2", arch: "x86_64", family: "mac"
Lucass-MacBook-Air:~ lucas$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home

Similarly run

java -version 

to verify that Java is correctly installed. You should see something like:

java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

Thats it! Thanks