Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Apache Maven Cookbook

You're reading from   Apache Maven Cookbook Over 90 hands-on recipes to successfully build and automate development life cycle tasks following Maven conventions and best practices

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher Packt
ISBN-13 9781785286124
Length 272 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started FREE CHAPTER 2. IDE Integration with Maven 3. Maven Lifecycle 4. Essential Maven Plugins 5. Dependency Management 6. Code Quality Plugins 7. Reporting and Documentation 8. Handling Typical Build Requirements 9. Multi-module Projects 10. Java Development with Maven 11. Advanced Maven Usage Index

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.

Getting ready

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:

  1. Open a terminal and run the following command:
    java -version
    
  2. See if you get an output similar to the following:
    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.

  3. Check if 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
    
  4. Find the location of the javac command:
    $ which javac
    

    The output for the preceding command is shown as:

    /usr/bin/javac
    
  5. 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:
    $ readlink /usr/bin/javac
    

    The output for the preceding command is shown as:

    /etc/alternatives/javac
    
  6. 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:
    $ 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
    
  7. We have now located the folder where JDK is installed:
    /usr/lib/jvm/java-1.7.0-openjdk.x86_64/
    
  8. 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:

    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.

How to do it...

To set up Maven on Linux, perform the following steps:

  1. Go to http://maven.apache.org/ and click on the Download link. The links to latest stable versions of Maven will be displayed.
  2. The binaries are available in both .zip and .tar.gz formats. For Mac OS X and Linux, the preferred download format is .tar.gz.
  3. 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.

  4. Execute the following command, and ensure the contents of the 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
    
  5. Set the M2_HOME variable as follows:
    export M2_HOME=/usr/local/apache-maven-3.2.5
    
  6. Update 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.

How it works...

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.

There's more...

Using the following steps, let's confirm that Maven has been set up correctly, by running a Maven command:

  1. Open a command prompt and run the following command:
    mvn –version
    
  2. The following output should be displayed:
    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.

See also

  • The Creating a simple project with Maven recipe in this chapter
You have been reading a chapter from
Apache Maven Cookbook
Published in: Apr 2015
Publisher: Packt
ISBN-13: 9781785286124
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime