How to install Maven on Mac OSX

Since Lion 10.7.1 10.6.8 MacOSx Snow Leopard (correct me if wrong), Mac OS X comes with Apache Maven 3 built in, and can be located at /usr/share/maven

1. Maven Built-In

To verify the Maven installation, in terminal, issue the command mvn -version.


$mvn -version

Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800)
Maven home: /usr/share/maven
Java version: 1.6.0_33, 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.7.4", arch: "x86_64", family: "mac"

1.1 – Where is Maven installed?
A quick check with whereis command :


$ whereis mvn
/usr/bin/mvn

$ cd /usr/bin
$$ ls -ls | grep mvn
 8 lrwxr-xr-x   1 root   wheel        24 May 23 15:57 mvn -> /usr/share/maven/bin/mvn

By default, Maven is installed at /usr/share/maven. No configuration is needed.

2. Install Maven Manually

After the upgrade of Mac OS X Mavericks, Maven was removed (no idea why). So, You have to install it manually.

2.1 – Download Maven
Download the Maven, for example apache-maven-3.1.1-bin.tar.gz. Extracts it,


tar -xvf apache-maven-3.1.1-bin.tar.gz

$pwd
/Users/mkyong/apache-maven-3.1.1

2.2 – Set Maven command in environment variable.
Update ~/. bash_profile – create this file if doesn’t exist.


$ vim ~/.bash_profile
~/.bash_profile

export M2_HOME=/Users/mkyong/apache-maven-3.1.1
export PATH=$PATH:$M2_HOME/bin

Restart the terminal. Test it.


$mvn -version

Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 23:22:22+0800)
Maven home: /Users/mkyong/apache-maven-3.1.1
Java version: 1.7.0_05, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"

3. Post Configuration

A common post-installation for Maven, change the local repository to another location.


$sudo vim /usr/share/maven/conf/settings.xml

$vim /usr/mkyong/maven/conf/settings.xml
settings.xml

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
 <localRepository>/Users/mkyong//maven/repo/</localRepository>

Above example changed the default repository from ~/.m2/repository to /Users/mkyong//maven/repo/.

References

  1. Apache Maven
  2. Set environment variables on Mac OS X

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Jono
Guest
Jono

I would recommend Homebrew for installing maven – it should almost always be the install method of choice. ‘$ brew install maven’

Milena Angelova
Guest
Milena Angelova

Perhaps, you have to add ‘source ~/.bash_profile’. Otherwise is a good article.

siddhartha
Guest
siddhartha

Or you can just use
brew install maven
if you are using mac

Churro
Guest
Churro

Thank you, it worked!

Reinaldo Burgos Avila
Guest
Reinaldo Burgos Avila

When I trie to run mvn from ant in a terminal I get this message.

${env.M2_HOME}/boot does not exist

This is the script:

This is my configuration on mac:

$ mvn -version

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)

Maven home: /Users/xxxx/Developer/apache-maven-3.3.9

Java version: 1.8.0_66, vendor: Oracle Corporation

Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre

Default locale: en_US, platform encoding: UTF-8

OS name: “mac os x”, version: “10.11.2”, arch: “x86_64”, family: “mac”

Anuj Nagpal
Guest
Anuj Nagpal

I tried the same getting this

mvn -v

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:637)

at java.lang.ClassLoader.defineClass(ClassLoader.java:621)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)

at java.net.URLClassLoader.access$000(URLClassLoader.java:58)

at java.net.URLClassLoader$1.run(URLClassLoader.java:197)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:190)

at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:401)

at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:42)

at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)

at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:254)

at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)

at org.codehaus.plexus.classworlds.launcher.Launcher.getMainClass(Launcher.java:144)

at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:266)

at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)

at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)

at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

Humberto Moura
Guest
Humberto Moura

Just configure the $JAVA_HOME variable.

Richard Clayton
Guest
Richard Clayton

Strangely, Maven was removed in OSX Mavericks (at least after the upgrade it’s nowhere to be found). I’m having to install manually (thank you for the original path locations!).

José
Guest
José
Navreet
Guest
Navreet

I am not able to edit the file settings.xml.I searched in internet. I found that I should copy it and create another file.But I don’t know how to do that.
Will there be any problem if i don’t change the location of the local repository to another location?

Flavio
Guest
Flavio

Hi My Friend,

MAVEN came pre installed since version 10.6.8 MacOSx Snow Leopard.

Cheers,

Flavio

Mayuresh
Guest
Mayuresh

How do I override default maven version and make the OS point to some other version (say 2.2.1)?