Monday, February 2, 2015

Installing Maven on Ubuntu

There is very little purpose in attempting to download and install binaries as complex, as say Hadoop, without using a tool like Maven.

Maven includes dependency management, such that any direct and indirect dependencies to the primary JAR files will be automatically downloaded and any conflicts resolved. This article covers the basics of downloading and installing Maven.


Where do I download Maven?


I have downloaded the latest version of Apache Maven from
http://maven.apache.org/download.cgi
At the time of this article, I’m going to download Maven 3.2.3.

I prefer not to use apt to install maven. The package manager will attempt to download OpenJDK and run this in combination with Maven. Probably nothing wrong with this approach, but I don't prefer it.


Where should I install Maven?


If you are not familiar with the Linux directory layout, this article is recommended:
http://www.nixtutor.com/linux/understanding-the-linux-directory-layout/
Apache Maven should be installed in the “usr” directory. This directory houses all the binaries, documentation, libraries, and header files for all the user applications. Most user binaries will be installed into this folder making this one of the largest folders in the Linux directory.

My preferred path is:
/usr/lib/apache/maven/3.2.3/ 
This leaves opportunty to distinguish between various apache binaries and maven versions. How do I install Maven? Typically, the file will be downloaded to your “downloads” folder in your home directory.

Using a terminal, following these steps:

Create the Directory Path:
$ sudo mkdir -p /usr/lib/apache/maven/3.2.5/
This creates the directory path for the new installation. The -p flag creates the directory hierarchy. You will need to run this command as the super user (su + do = sudo).

Move the File:
$ sudo mv ~/Downloads/apache-maven-3.2.5-bin.tar.gz /usr/lib/apache/maven/3.2.5/  
Moves the downloaded file to the installation path.

Unzip the File:
$ cd /usr/lib/apache/maven/3.2.5/  
$ sudo tar -zxvf apache-maven-3.2.5-bin.tar.gz  
Navigates to the newly created folder, and unpacks the downloaded file.


Setting the PATH


I recommend setting the path in the /etc/environment file. This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line.
$ sudo gedit /etc/environment

and once completed, I have this:
1
2
3
4
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk/jdk1.8.0_25/bin/:/usr/lib/apache/maven/3.2.3/bin"  
export PATH  
export JAVA_HOME=/usr/lib/jvm/jdk/jdk1.8.0_25  
export MAVEN_HOME=/usr/lib/apache/maven/3.2.5

To activate these path variables for the current terminal session, type
$ source /etc/environment 

If your path variables were set successfully, you should be able to type
$ mvn –ver

in the terminal window and get back something that looks like this:
$ mvn -ver
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T09:29:23-08:00)  
Maven home: /usr/lib/apache/maven/3.2.5  
Java version: 1.8.0_31, vendor: Oracle Corporation  
Java home: /usr/lib/jvm/jdk/1.8.0_31/jre  
Default locale: en_US, platform encoding: UTF-8  
OS name: "linux", version: "3.16.0-23-generic", arch: "amd64", family: "unix"  



Script Automation


This entire process can be automated in a single shell script:
install_maven() {
    if hash mvn 2>/dev/null; 
        then
        echo "Maven is already Installed"
        mvn --version | grep "Apache Maven"
    else
        #   provision the classpath
        sudo sh -c 'echo "export MAVEN_HOME=/usr/lib/apache/maven/3.2.5" >> /etc/environment'
        sudo sh -c 'echo "PATH=\"$PATH:/usr/lib/apache/maven/3.2.5/bin\"" >> /etc/environment'
        sudo sh -c 'echo "export PATH" >> /etc/environment'
        #   create the directories
        sudo mkdir -p $MAVEN_HOME
        #   download and extract the files
        cd $MAVEN_HOME
        sudo wget http://mirrors.koehn.com/apache/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz
        sudo tar -zxvf apache-maven-3.2.5-bin.tar.gz
        #   massage the directory structure and cleanup
        sudo rm apache-maven-3.2.5-bin.tar.gz
        sudo mv apache-maven-3.2.5/* .
        sudo rm -rf apache-maven-3.2.5/
        source /etc/environment
    fi
}



References

  1. Resources
    1. [GitHub] Installation Script
    2. [GitHub] Docker Container (extends java:8)
  2. Installing Oracle's JDK on Ubuntu
  3. Installing Eclipse on Ubuntu

No comments:

Post a Comment