Wednesday, 27 June 2012

Advantage to migrate to Maven


Ant excels at build process, it is a build system modeled after make with targets and dependencies. Each target consists of a set of instructions which are coded in XML. There is a copy task and a javac task as well as a jar task.

A Simple Ant build.xml file. 
<project name="my-project" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
    <!-- set global properties for this build -->
    <property name="src" location="src/main/java"/>
    <property name="build" location="target/classes"/>
    <property name="dist"  location="target"/>

    <target name="init">
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build}"/>
    </target>

    <target name="compile" depends="init"
            description="compile the source " >
        <!-- Compile the java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build}"/>
    </target>

    <target name="dist" depends="compile"
            description="generate the distribution" >
        <!-- Create the distribution directory -->
        <mkdir dir="${dist}/lib"/>

        <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
        <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
    </target>

    <target name="clean"
            description="clean up" >
        <!-- Delete the ${build} and ${dist} directory trees -->
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
    </target>
</project>
Contrast the previous Ant example with a Maven example. In Maven, to create a JAR file from some Java source, all you need to do is create a simple pom.xml, place your source code in ${basedir}/src/main/java and then run mvn install from the command line.


A Sample Maven pom.xml. 
That’s all you need in your pom.xml. Running mvn install from the command line will process resources, compile source, execute unit tests, create a JAR, and install the JAR in a local repository for reuse in other projects.

The good thing with Maven that it is extremely flexible, because it consists of core and numerous plugins that are accessible in online repositories. Relations between different projects and subprojects are based on a single source of information POM (xml) file - Project object Model. One POM points to another POMs which can have flat or hierarchical structure.

The main advantage to migrate to Maven are : standardization of how to handle a project, dependencies are clearly defined, the concept of artifacts that can be shared to other projects, documentation and reports, remote and local repository concepts.


A project produces an artifact - jar, ear, war... The next features are good with Maven:
Dependencies are downloaded automatically
Standardized, very consistent layout
Standardized, very consistent naming
Code coverage
Extensive reports
Maven easily works with JUnit tests