Installing Maven on Linux
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:
- Open a terminal and run the following command:
- See if you get an output similar to the following:
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.
- Check if
javac
works; it does only if JDK is installed, not JRE:The output for the preceding command is shown as:
- Find the location of the
javac
command:The output for the preceding command is shown as:
- In the preceding output,
javac
is a symbolic link to the actual location of the file. Try to determine this location in the following way:The output for the preceding command is shown as:
- By executing the preceding command, we again got the symbolic link. To get the path to the location of
javac
, we execute the following command again:The output for the preceding command is shown as:
- We have now located the folder where JDK is installed:
- Set
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:
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:
- Go to http://maven.apache.org/ and click on the Download link. The links to latest stable versions of Maven will be displayed.
- The binaries are available in both
.zip
and .tar.gz
formats. For Mac OS X and Linux, the preferred download format is .tar.gz
. - Extract the downloaded binary to a folder you want Maven to reside in. The typical location in Linux is the
/usr/local
folder.Tip
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.
- Execute the following command, and ensure the contents of the
apache-maven-3.2.5
folder are similar to the following output:The output for the preceding command is shown as:
- Set the
M2_HOME
variable as follows: - Update
PATH
to include Maven's bin
folder:
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:
- Open a command prompt and run the following command:
- The following output should be displayed:
If you get an error, recheck the installation steps and repeat them.
- The Creating a simple project with Maven recipe in this chapter