How to Setup maven on Mac OS X


Let us see how to setup maven project : "A Project Object Model (POM) based management, build, reporting and documentation tool" on Mac Operating system.

Environment:

Operating System: Mac OS X Yosemite 10.10

Maven version: v3.3.3

Java version: Java Development Kit JDK 7 or higher

Disk requirement: min 10Mb

You can download the latest build from Apache Maven website : https://maven.apache.org/download.cgi.

Note : If you are using older version of Mac OS X (1.6) you may find maven installed under /usr/share/maven.

Setting up maven:

Download maven Binary tar.gz archive ( latest : http://mirror.nexcess.net/apache/maven/maven-3/3.3.3/binaries/apache-maven-3.3.3-bin.tar.gz)

Go to terminal app

Execute the following commands to set Java and Maven bin path :

export PATH=/Users/username/Setups/apache-maven-3.3.3/bin:$PATH JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/

To check if maven is configured correctly run the following command: mvn -version

localhost:Users username$ mvn -version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T17:27:37+05:30)
Maven home: /Users/username/Setups/apache-maven-3.3.3
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.10.3", arch: "x86_64", family: "mac"

localhost:Users username$

Note : If the path is not set correctly you may get errors when you run mvn - version

localhost:Users username$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/
localhost:Users username$ mvn -version
Error: JAVA_HOME is not defined correctly.
We cannot execute /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Hom//bin/java
localhost:Users username$

Note: Maven 3.3.3 requires java version 1.7 or above, if you use a version below it you may get the following error,

localhost:Users username$  mvn --version

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClassCond(ClassLoader.java:2337)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:121)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:441)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:134)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:13)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:122)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:240)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:341)
	at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:22)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:343)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:234)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:339)
	at org.codehaus.plexus.classworlds.launcher.Launcher.getMainClass(Launcher.java:134)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:246)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:332)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:135)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:345)

localhost:Users username$