Saturday 18 January 2014

How to install maven on Ubuntu?

What is Maven?

Maven is essentially a build automation tool similar to apache ant but provides much more features that just building. It addresses two important aspects -  
  1. how your software will be built and 
  2. your software dependency resolution
For complete details you can visit the following sites
  1.  Wiki
  2. Apache Maven Homepage
Lets get started then.



How to install Maven?

 You can search for the maven package using the following command

apt-cache search maven


You may see a lot of packages but what we are interested is -

maven - Java software project management and comprehension tool

So lets go ahead and install it. Use the following command to install the package

sudo apt-get install maven

I am using Ubuntu so I have apt-get. You can use yum  if you have Fedora/CentOS.

To verify the installation you can simply execute the following command

 mvn -v

v here stands for version. You can also type complete word i.e  mvn -version.
 You will see some details about the installed maven package.

This indicated maven is successfully installed.

Install latest versions

Using apt-get you may not get the latest version of maven. For latest version download the binary from maven site and install it.

Use the following commands - 

  1. cd ~/Downloads
  2. wget http://apache.mirrors.timporter.net/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.tar.gz
  3. sudo mkdir -p /usr/local/apache-maven
  4. sudo mv apache-maven-3.1.1-bin.tar.gz /usr/local/apache-maven
  5. cd /usr/local/apache-maven
  6. sudo tar -xzvf apache-maven-3.1.1-bin.tar.gz
  7. Edit ~/.profile with gedit ~/.profile and add these four lines:
    1. export M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
    2. export M2=$M2_HOME/bin
    3. export MAVEN_OPTS="-Xms256m -Xmx512m"
    4. export PATH=$M2:$PATH


Next immediate question that may come to anyone’s mind - 

Where is maven installed?

Maven by default gets installed in the following directory

/usr/share/maven


and maven configuration files go to the following directory

/etc/maven


settings.xml has default configurations like path to local repository. yes maven downloads dependencies and stores it in a local repository so that there is no need to download same dependency twice.

Path to maven(mvn) executable is not added to $PATH variable then how is the command recognized?

If you print your path variable by using

echo $PATH

You will notice one of the paths in the variable is

/usr/bin

Now if you see the contents of /usr/bin you will notice that it has mvn executable. It is actually soft link to the actual executable. This approach is a common approach. We cannot keep adding all installed packages folder to $PATH. Instead create a soft link to executable and keep it in  /usr/bin and add this to the $PATH.

This was just how do we install maven on ubuntu. In the next post we will see how to compile and run Java code using maven.


Related Links

No comments:

Post a Comment

t> UA-39527780-1 back to top