Maven by Example

2.3. Installing Maven

There are wide differences between operating systems such as Mac OS X and Microsoft Windows, and there are subtle differences between different versions of Windows. Luckily, the process of installing Maven on all of these operating systems is relatively painless and straightforward. The following sections outline the recommended best-practice for installing Maven on a variety of operating systems.

2.3.1. Installing Maven on Mac OSX

from http://maven.apache.org/download.html. Download the current release of Maven in a format that is convenient for you to work with. Pick an appropriate place for it to live, and expand the archive there. If you expanded the archive into the directory /usr/local/apache-maven-3.0.3, you may want to create a symbolic link to make it easier to work with and to avoid the need to change any environment configuration when you upgrade to a newer version:

/usr/local % cd /usr/local
/usr/local % ln -s apache-maven-3.0.3 maven
/usr/local % export M2_HOME=/usr/local/maven
/usr/local % export PATH=${M2_HOME}/bin:${PATH}

Once Maven is installed, you need to do a couple of things to make it work correctly. You need to add its bin directory in the distribution (in this example, /usr/local/maven/bin) to your command path. You also need to set the environment variable M2_HOME to the top-level directory you installed (in this example, /usr/local/maven).

Note

Installation instructions are the same for both OSX Tiger and OSX Leopard. It has been reported that Maven 2.0.6 is shipping with a preview release of XCode. If you have installed XCode, run mvn from the command-line to check availability. XCode installs Maven in /usr/share/maven. We recommend installing the most recent version of Maven 3.0.3 as there have been a number of critical bug fixes and improvements since Maven 2.0.6 was released.

You’ll need to add both M2_HOME and PATH to a script that will run every time you login. To do this, add the following lines to .bash_login.

export M2_HOME=/usr/local/maven
export PATH=${M2_HOME}/bin:${PATH}

Once you’ve added these lines to your own environment, you will be able to run Maven from the command line.

NOTE:> These installation instructions assume that you are running bash.

Installing Maven on OSX using MacPorts

If you are using MacPorts, you can install the maven2 port by executing the following command-line:

$ sudo port install maven2
Password: ******
--->  Fetching maven2
--->  Attempting to fetch apache-maven-3.0.3-bin.tar.bz2
from http://www.apache.org/dist/maven/binaries
--->  Verifying checksum(s) for maven2
--->  Extracting maven2
--->  Configuring maven2
--->  Building maven2 with target all
--->  Staging maven2 into destroot
--->  Installing maven2 3.0.3_0
--->  Activating maven2 3.0.3_0
--->  Cleaning maven2

For more information about the maven2 port, see the maven2 Portfile. For more information about MacPorts and how to install it, see the MacPorts project page.

2.3.2. Installing Maven on Microsoft Windows

Installing Maven on Windows is very similar to installing Maven on Mac OSX, the main differences being the installation location and the setting of an environment variable. This book assumes a Maven installation directory of c:\Program Files\apache-maven-3.0.3, but it won’t make a difference if you install Maven in another directory as long as you configure the proper environment variables. Once you’ve unpacked Maven to the installation directory, you will need to set two environment variables—PATH and M2_HOME. To set these environment variables from the command-line, type in the following commands:

C:\Users\tobrien > set M2_HOME=c:\Program Files\apache-maven-3.0.3
C:\Users\tobrien > set PATH=%PATH%;%M2_HOME%\bin

Setting these environment variables on the command-line will allow you to run Maven in your current session, but unless you add them to the System environment variables through the control panel, you’ll have to execute these two lines every time you log into your system. You should modify both of these variables through the Control Panel in Microsoft Windows.

Setting Environment Variables

  • Go into the Control Panel
  • Select System
  • Go in Advanced tab and click on Environment Variables.
figs/web/installation_set_env_var_3.png

Figure 2.1. Go in Advanced tab and click on Environment Variables


  • Click on New (the lower in the System variables section).
figs/web/installation_set_env_var_4.png

Figure 2.2. Click on New (the lower)


  • Fill in the fields with M2_HOME and c:\Program Files\apache-maven-3.0.3 and click on OK button.
figs/web/installation_set_env_var_5.png

Figure 2.3. Fill in the fields with M2_HOME and c:\Program Files\apache-maven-3.0.3 and click OK.


  • Click on the Path variable in the System variables section and click on Edit button.
figs/web/installation_set_env_var_6.png

Figure 2.4. Click on the Path variable in the System variables section and click on Edit button.


  • Add the string %M2_HOME%\bin; in the Variable value field to the front of the existing value and click on the OK button in this and the following dialogs

2.3.3. Installing Maven on Linux

To install Maven on a Linux machine follow the exact procedure outlined in Section 2.3.1, “Installing Maven on Mac OSX”.

2.3.4. Installing Maven on FreeBSD or OpenBSD

To install Maven on a FreeBSD or OpenBSD machine, follow the exact procedure outlined in Section 2.3.1, “Installing Maven on Mac OSX”.