How to Install Maven

Maven is a popular, if not the most popular, Java build tool and dependency management framework. It is a good idea to use a build tool rather than rely on your IDE build scripts if you are planning on collaborating with others on your Java project.

You can install Maven and be rockin' in just a few steps:

1. Install Java

First you need to have the Java JDK (Java Development Kit) installed; having the JRE (Java Runtime Environment) is not enough. To check if you have the JDK installed, open a command prompt or terminal and type javac -version. If you have a JDK installed, you will see your javac version output, eg. javac 1.7.0_01. If you get an error that javac is not a recognized command, download and install the Java JDK.

2. Download Maven

Download Maven from a mirror off the Apache Maven website.

3. Unpack and Set System variables
Windows
  1. Unzip the Maven download to the folder to which you would like to install Maven, eg. "C:\Program Files\Apache Software Foundation". The subdirectory apache-maven-x.x.x will be created from the archive, where x.x.x is the version.
  2. Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-x.x.x (or wherever you unzipped Maven). Be sure to omit any quotation marks around the path even if it contains spaces.
  3. In the same dialog, add the M2 environment variable in the user variables with the value "%M2_HOME%"\bin.
  4. In the same dialog, update/create the Path environment variable in the user variables and prepend the value "%M2%" to add Maven available in the command line.
  5. In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.7.0_06 and that %JAVA_HOME%\bin is in your Path environment variable.
  6. Open a new command prompt (type cmd in Start menu) and run mvn --version to verify that it is correctly installed.
Mac/Linux
  1. Extract the distribution archive, i.e. apache-maven-x.x.x-bin.tar.gz (where x.x.x is the version) to the directory you wish to install Maven. These instructions assume you chose /usr/local/apache-maven. The subdirectory apache-maven-x.x.x will be created from the archive.
  2. In a command terminal, add the M2_HOME environment variable. export M2_HOME=/usr/local/apache-maven/apache-maven-x.x.x
  3. Add the M2 environment variable export M2=$M2_HOME/bin
  4. Add M2 environment variable to your path. export PATH=$M2:$PATH
  5. Make sure that JAVA_HOME is set to the location of your JDK, e.g. export JAVA_HOME=/usr/java/jdk1.7.0_06 and that $JAVA_HOME/bin is in your PATH environment variable.
  6. Run mvn --version to verify that it is correctly installed.
You now have Maven set up! Stay tuned for another post on how to build a simple Maven project.

More reading:

Stack Exchange - Getting Started With Build Tools
Apache - Maven in 5 Minutes
Posted in Maven Tagged with: , ,

Sponsored

Follow Me

RSS FeedFollow me on Twitter