Saturday 6 June 2015

Introduction to Maven - Part I

 
    Maven in simple words is a powerful build tool. A build tool is a tool that automates everything related to building the software project which includes:

        1. Generating source code
        2. Generating documentation from the source code
        3. Compiling source code
        4. Packaging compiled code into JAR files or ZIP files

    The most important reason behind using maven is it helps us manage dependencies. Dependency management is what makes it such a powerful tool. Besides Maven can also be used as a project management tool. It maintains the version information and can be used to generate the javadoc as well as maintain other information about the component.

    Maven is a open source tool managed by the Apache Software Foundation. The reason you would want to use it is its platform independent. We can recreate our builds for any environment. Downloading a dependency will also pull other items it needs i.e it supports transitive dependency. Maven can be integrated with an IDE or can be used standalone.

    You must have seen people comparing Maven and Ant tool but Ant really isn't a build tool as much as it is a scripting tool. You have to explicitly do everything in Ant.

    Ant uses build.xml as the name for a build file. I have listed a simple build.xml file. It is quite easy to understand what this file does. It first cleans the project then compiles it and creates a jar and finally runs the jar. But there is a problem here, what if i call build before clean. This might introduce some errors in my build.

<project>

    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="oata.HelloWorld"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/HelloWorld.jar" fork="true"/>
    </target>

</project>



On the other hand, maven contains a lot of implicit functionality. Maven uses a pom.xml for its build.
A simple pom.xml file is listed below. We will discuss more about pom file later.

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
</project>



You can observe that not much information is explicit in maven and this can get quite confusing sometimes. Maven uses a convention over configuration model i.e we are good to go until we use standard maven convention rather than configuring everything like that in ant build.xml.

    Now this pom file can be used to do all those things like clean, compile, build and run. It will all work just looking at my pom file because you don't see all those stuffs set up if i follow their conventions, their directory structure. So it is a little non-descriptive at first until you understand the semantics of how maven works.
    It is really centered around managing your entire project's lifecycle.

Maven directory structure


    Now lets have a look at the maven folder structure. By default maven looks for a src/main/java directory underneath our project directory. It compiles all the code into a target directory. And it references all these things looking into our pom.xml file.  
src/main/java - where we store our Java code following the standard package declaration

For  example, for a project with package name com.myproject, java files will be placed under src/main/java/com/myproject/ directory.

src/test/java - all your unit tests code goes here

target - this is where everything gets compiled to, where tests get ran from and contents in this directory get packaged into a jar, zip etc.


Maven pom.xml file


A pom file is an XML representation of project resources like source code, test code, dependencies i.e external JARs used etc. The POM contains references to all of these resources.

Now a pom.xml file can be divided into 4 basic parts:

1. Project Information: 

    
    This can include 

    a. groupId - This is the package name used inside our application. For example: com.myproject
    b. artifactId - This is the application name. For example: HelloWorld
    c. version - This is the version number of our application. For example: 1.0.0
    d. packaging - This is how we want to distribute our application For example: jar, war

2. Dependencies: 


    This includes the direct dependencies i.e the artifacts that we want to use in our application.Now for adding a dependency we need to know the following three things:

    1. groupId
    2. artifactId
    3. version

Note: Maven stores everything it downloads in your home directory/.m2 directory.
For example: C:\Users\<your_username>\.m2\repository

This is how we can add a dependency in our pom.xml file

<dependencies>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.1</version>
    </dependency>
</dependencies>


Note: For storing a dependency it uses the above info. For example for storing the commons-lang artifact it uses the following directory structure:
C:\Users\<your_username>\.m2\repository\commons-lang\commons-lang\2.1\commons-lang-2.1.jar


Note: Maven does not store the downloaded artifacts into individual project directory instead it stores the artifacts into a local repo so that if an artifact is included in multiple projects it does not have to go and download that multiple times.

3. Build: 


    This includes 

    a. Plugins - The plugins that we want to use in our application
    b. Directory Structure - This is where we can override the default java and other directories

    For example: Lets change the final artifact name of the packaged application. This can be done by adding the following code to the pom.xml file:

<build>
    <finalName>test</finalName>
</build>

4. Repositories: 


    This is where we download the artifacts from. By default it downloads the artifacts from central maven repository 



I hope this post has given you something to start with maven. If you have any doubts, just leave a comment and I will look to it.
Happy Reading :)




No comments:

Post a Comment