
Apache Maven Cookbook
By :

Let us look at the steps to install Maven on Linux.
While there are many flavors of Linux (Ubuntu, Fedora, RHEL, SUSE, CentOS, and so on), the steps to set up Maven are similar.
Maven needs Java, specifically the Java Development Kit (JDK). Using the following steps, let us check if it is installed in your Linux system, which is a bit tricky:
java -version
java version "1.7.0_65"
OpenJDK Runtime Environment (rhel-2.5.1.2.el6_5-x86_64 u65-b17)
The preceding output will still not tell you where your Java is installed, which is required to set JAVA_HOME
. You can get this information by performing the next set of steps.
javac
works; it does only if JDK is installed, not JRE:$ javac -version
The output for the preceding command is shown as:
javac 1.7.0_65
javac
command:$ which javac
The output for the preceding command is shown as:
/usr/bin/javac
javac
is a symbolic link to the actual location of the file. Try to determine this location in the following way:$ readlink /usr/bin/javac
The output for the preceding command is shown as:
/etc/alternatives/javac
javac
, we execute the following command again:$ readlink /etc/alternatives/javac
The output for the preceding command is shown as:
/usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin/javac
/usr/lib/jvm/java-1.7.0-openjdk.x86_64/
JAVA_HOME
to the preceding folder. This can be done in two ways, depending on what you desire:If it is for the duration of the session, run the following command:
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk.x86_64/
If this is permanent, add the preceding line in .bash_profile
in your HOME
folder.
If Java is not present, download and install Java from the Oracle Java download page at http://www.oracle.com/technetwork/java/javase/downloads/index.html.
If you have an rpm-based Linux distribution, you can download and install rpm
. Otherwise, you can download the .tar.gz
format of the distribution and extract it to a folder of your choice.
In the earlier case, you know exactly where Java is installed and can set JAVA_HOME
correspondingly. Once installed, verify the Java installation by following the preceding steps.
Now, let us set up Maven on Linux.
To set up Maven on Linux, perform the following steps:
.zip
and .tar.gz
formats. For Mac OS X and Linux, the preferred download format is .tar.gz
./usr/local
folder.You will need a super user (su) or administrator access to place contents in the /usr/local
folder. If you do not have access, you can place this in a subfolder of your HOME
folder.
apache-maven-3.2.5
folder are similar to the following output:/usr/local/apache-maven-3.2.5$ ls -l
The output for the preceding command is shown as:
total 27
-rw-r--r-- 1 root root 17464 Aug 12 02:29 LICENSE
-rw-r--r-- 1 root root 182 Aug 12 02:29 NOTICE
-rw-r--r-- 1 root root 2508 Aug 12 02:26 README.txt
drwxr-xr-x 8 root root 4096 Aug 19 13:41 bin
drwxr-xr-x 3 root root 0 Aug 19 13:41 boot
drwxr-xr-x 4 root root 0 Oct 14 17:39 conf
drwxr-xr-x 67 root root 28672 Aug 19 13:41 lib
M2_HOME
variable as follows:export M2_HOME=/usr/local/apache-maven-3.2.5
PATH
to include Maven's bin
folder:export PATH=$PATH:$M2_HOME/bin
Like JAVA_HOME
, the preceding settings can be persisted by updating .bash_profile
.
The Maven installation is essentially a set of JAR files, configuration files, and a Linux shell script, namely mvn
.
The mvn
command essentially runs this script. It first checks for JAVA_HOME
. This file is present in the bin
folder of the Maven installation and hence needs to be in PATH
.
If the shell script does not find JAVA_HOME
, it looks for java
in its PATH
. This can lead to unexpected results, as typically, the Java
in PATH
is usually JRE and not JDK.
The shell script then looks for M2_HOME
, which is the location of the Maven installation. It does this so that it can load the libraries that are present.
Additionally, it also reads values specified in MAVEN_OPTS
. This variable allows you to run Maven with an additional heap size and other Java parameters.
Using the values for JAVA_HOME
, M2_HOME
, and MAVEN_OPTS
, the shell script runs its org.codehaus.plexus.classworlds.launcher.Launcher
main class.
Using the following steps, let's confirm that Maven has been set up correctly, by running a Maven command:
mvn –version
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T22:59:23+05:30)
Maven home: /usr/local/maven
Java version: 1.7.0_65, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.65.x86_64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "2.6.32-279.22.1.el6.x86_64", arch: "amd64", family: "unix"
If you get an error, recheck the installation steps and repeat them.