How to Setup maven on Mac OS X


Posted on : Wed, 26th Aug 15 12:17 pm (Updated on : Wed, 26th Aug 15 12:17 pm) UTC
Author : Code2care
100+ votes

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 :

  1. Operating System : Mac OS X Yosemite 10.10
  2. Maven version : v3.3.3
  3. Java version : Java Development Kit JDK 7 or higher
  4. 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 :

  1. 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)
  2. Go to terminal app
  3. 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$ 

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +