Pages

Maven Installation and configuration in Ubuntu

Hello Friends,

In this tutorial I will show you how to install and configure MAVEN in Ubuntu.

Maven is a Java tool for Project Management to make build process easier. I used the maven to compile and package my storm topology to run on storm cluster both in production mode and local mode. 

Storm is a Apache framework for real time computation.


Prerequisites:

JAVA 1.5 or above. follow the following link to see how to install java and set environment for java.

http://ashuuni123.blogspot.com/2013/08/installing-java-on-linuxubuntu.html

Installation of Maven on ubuntu can be pretty straightforward 


$ sudo apt-get install maven2
Files should be installed in /usr/share/maven2

Verification

Type “mvn -version” to verify the installation.

$ mvn -version
Output should something like this-


Apache Maven 2.2.1 (rdebian-8)
Java version: 1.7.0_25
Java home: /usr/local/jdk1.7.0_25/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux" version: "3.2.0-52-generic-pae" arch: "i386" Family: "unix


Where the Maven installed?

The Apt-get installation will install all the required files in the following folder structure-
  1. /usr/bin/mvn
  2. /usr/share/maven2/
  3. /etc/maven2 

Pom File: 

POM is stands for project object model , is an xml file which contains information about the project which you want to build and configuration details used by MAVEN to build the project.
For example it define the directory structure for your source file and your class file. and also define the location where dependencies related to your project is stored.





Maven uses Convention over Configuration which means developers are not required to create build process themselves. Developers do not have to mention each and every configuration details.



Maven provides developers ways to manage following −
  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution
  • mailing list

SNAPSHOT is a special version that indicates a current development copy. Unlike regular versions, Maven checks for a new SNAPSHOT version in a remote repository for every build.


Transitive dependency means to avoid needing to discover and specify the libraries that your own dependencies require, and including them automatically.



The various dependency scope used in Maven are:

Compile: It is the default scope, and it indicates what dependency is available in the classpath of the project

Provided: It indicates that the dependency is provided by JDK or web server or container at runtime

Runtime: This tells that the dependency is not needed for compilation but is required during execution

Test: It says dependency is available only for the test compilation and execution phases

System: It indicates you have to provide the system path

Import: This indicates that the identified or specified POM should be replaced with the dependencies in that POM’s section


For more detail follow the given link-

Default structure create by maven for a project



 

Sample of pom.xml file 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org  /2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>esc</groupId>
    <artifactId>storm-experiment-2</artifactId>
    <version>0.0.1-SNAPSHOT</version>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerVersion>1.7</compilerVersion>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.path.to.main.Class</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>

        <!-- Repository where we can found the storm dependencies -->
        <repository>
            <id>clojars.org</id>
            <url>http://clojars.org/repo</url>
        </repository>

    </repositories>

    <dependencies>

        <!-- Storm Dependency -->
        <dependency>
            <groupId>storm</groupId>
            <artifactId>storm</artifactId>
            <version>0.8.2</version>
        </dependency>

    </dependencies>



</project>

 

* project – root element


* modelVersion –

  The modelVersion element sets what version of the POM model you are using 

* groupId –
  The groupId element is a unique ID for an organization, or a project 

* artifactId –
  The artifactId element contains the name of the project you are building 

* version – The versionId element contains the version number of the project.

  The above groupId, artifactId and version elements would result in a JAR file being built and put into the local Maven repository 

Maven life cycle

Every build follows a specified life cycle. Maven comes with a default life cycle that includes the most common build phases like compiling, testing and packaging.
The following lists gives an overview of the important Maven life cycle phases.
  • validate - checks if the project is correct and all information is available
  • compile - compiles source code in binary artifacts
  • test - executes the tests
  • package - takes the compiled code and package it, for example into a JAR file.
  • integration-test - takes the packaged result and executes additional tests, which require the packaging
  • verify - performs checks if the package is valid
  • install - install the result of the package phase into the local Maven repository
  • deploy - deploys the package to a target, i.e. remote repository

 

Maven Repository

There are 3 types of repository in maven
  1. Local Repository 
  2. Remote Repository
  3. Central  Repository

Local Repository:  When you install and run maven first time, it will create a .m2 directory on your home directory, which contains a another directory name repository like- 

$HOME/.m2/repository
This the default location for the jar which maven check. If the particular jar is not in local repository then it will be downloaded from the remote repository which is set by maven when we installed the maven.
Remote Repository which is developer's own custom repository containing required libraries or other project jars.

Central Repository : Maven central repository is repository provided by Maven community. It contains a large number of commonly used libraries.
When Maven does not find any dependency in local repository, it starts searching in central repository using following URL: http://repo1.maven.org/maven2/

  Using maven behind proxy

To use maven behind the proxy we have to define the proxy setting in setting.xml  file inside the proxy element.
The setting file is found in /etc/maven2/setting.xml
Add the following line under proxy element

<settings>
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>192.168.1.100</host>
<port>3128</port>
<username>your-username</username>
<password>your-password</password>
</proxy>
</proxies>
</settings>