
Apache Maven Cookbook
By :

Let us look at the steps to install Maven on Mac OS X. This applies to the latest version of Mac OS X, namely Yosemite.
Earlier, Apple provided Java for Mac, but stopped doing so from Java 7 onwards. Apple Java is not available on recent versions of Mac OS X.
Let us check if the Java environment is available on your Mac:
/usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
/usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
As you can see, my system has both Java 1.7 and 1.8.
JAVA_HOME
to the desired JDK. This can be done in two ways, depending on what you desire:If this is for the duration of the session, run the following command:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
If this is permanent, add the preceding line in .bash_profile
in your HOME
folder
Ensure you have the JDK installation and not JRE.
If Java is not present, download and install Java from the Oracle Java download page at http://www.oracle.com/technetwork/java/javase/downloads/index.html.
Once installed, verify the Java installation by following the preceding steps.
Now, let us set up Maven on Mac OS X.
Let's download Maven from its official website by performing the following steps:
.zip
and .tar.gz
formats. Choose one of them./usr/local
folder./usr/local
folder. If you do not have access, you can place the contents in a subfolder of your HOME
folder./usr/local/apache-maven-3.2.5$ ls -l
The output is shown as:
total 27
-rw-r--r-- 1 root wheel 17464 Aug 12 02:29 LICENSE
-rw-r--r-- 1 root wheel 182 Aug 12 02:29 NOTICE
-rw-r--r-- 1 root wheel 2508 Aug 12 02:26 README.txt
drwxr-xr-x 8 root wheel 4096 Aug 19 13:41 bin
drwxr-xr-x 3 root wheel 0 Aug 19 13:41 boot
drwxr-xr-x 4 root wheel 0 Oct 14 17:39 conf
drwxr-xr-x 67 root wheel 28672 Aug 19 13:41 lib
M2_HOME
variable as follows:export M2_HOME=/usr/local/apache-maven-3.2.5
PATH
to include Maven's bin
folder:export PATH=$PATH:$M2_HOME/bin
Like JAVA_HOME
, the preceding settings can be persisted by updating .bash_profile
with the preceding lines.
In the preceding steps, we discussed the steps to download Maven from its official website. We will now discuss installing Maven using brew. Brew is a popular application on Mac OS X to install open source software. If you have brew installed on your Mac OS X, run the following command to install Maven:
brew install maven
The output for the preceding command will be displayed as shown in the following screenshot:
The Maven installation is essentially a set of JAR files, configuration files, and a Mac OS X shell script, namely mvn
.
The mvn
command essentially runs this script. It first checks for JAVA_HOME
. This file is present in the bin
folder of the Maven installation and, hence, it needs to be in PATH
.
If the shell script does not find JAVA_HOME
, it looks for Java
in its PATH
. This can lead to unexpected results, as typically the Java
in PATH
is usually the JRE installation and not JDK.
The shell script then looks for M2_HOME
, which is the location for the Maven installation. It does this so that it can load the libraries that are present.
Additionally, it also reads values specified in MAVEN_OPTS
. This variable allows you to run Maven with an additional heap size and other Java parameters.
Using the values for JAVA_HOME
, M2_HOME
, and MAVEN_OPTS
, the shell script runs its main class org.codehaus.plexus.classworlds.launcher.Launcher
.
Verify your Maven installation using the following steps:
mvn –version