Maven

Difference Between ANT and Maven

In this example we are going to see some differences between ant and maven.

Maven is a build automation tool used mainly for java projects from apache.

Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files

We are going to see an example of a project modeled with ant and the same project modeled with ant.

For this example we use the following technologies:

  • MAC OSX
  • Eclipse Mars.1
  • Maven3
  • Ant 1.9.3
  • JDK 1.8.0_65 64bits

1. Introduction

Both tools come from apache software foundation, and both tools are designed to resolve the same thing, in other words, related to support the software build process. Ant was released in early 2000, it has a similar structure with unix make task. It is implemented in java language and is best situated to build java projects.

In front of it, Maven was originally released in 2004, and is situable for manage and build java projects. Maven is an ant evolution and is capable of manage ‘out of the box’ some things like manage dependencies, build lifecycle events (such as compile, test, package, etc…) and so many things without any action needed by the user.

In this example we are going to see the same project under both technologies, and how we have to organize the project in order to achieve the same result.

The example project is a java project with an unique dependency (log4j) that is packaged as a jar file.

2. High level comparision

Ant and maven has several differences between themselves, there are some deep and important differences in how they manage the project build, dependencies, lifecyle management, the way that it execute task, and many other things.

Let´s see the more important ones:

2.1 High level comparision

  • Project structure: Ant has not a defined project conventions, you can put things in any place, and later instruct ant for know where the things are. Maven has a project conventions and has several archetypes for predefined projects, so maven is easier because it knows where things are if you follow the project convention.

    2.2 Execution way

    Ant is a procedural tool, you have to tell it when, what and how it has to do all the things: compile, then copy, then package, then deploy, etc… Maven is a declarative tool, you only have to declare your project object model (pom) and put your source code and resources in the correct folder, mave will take care of the rest for you.

    2.3 Lifecycle management

    Ant do not have a lifecycle management, you have to declare some goals and define which of those goals run first, what run later and so on manually. Maven has a lifecycle management.

    2.4 Dependency management

    Ant does not have any mechanism to manage dependencies, you have to manage it manually: donwload all dependencies, place the dependencies in a folder and later copy it to the packaged artifact, maven has a mechanism to manage dependencies, you have a local repository that acts like a cache and you can define some remote repositories in order to download more dependencies, you only have to define all your dependencies needed and maven will download it for you.

    3. Ant project

    In this example we have a java project that will be packaged as a jar file. The project has an example class that defines a Main method that print Hello World! with log4j framework.

    You can see below the ant project

    Ant project
    Ant project

    We have a source package called src, inside of it you will find a package called jcg.ant.example with a class called MyClass. You will find also a file called log4j.xml which is de log for java descriptor file.

    Inside the lib folder you will find all the needed libs in order to compile the project. In this example we only need the log4j jar file. You have to manually instruct eclipse to add it to the compile and runtime claspath through Project -> Properties -> Java Build Path.

    Inside build folder you will find the ant build script, called build.xml and a properties file called build.properties. Below you can see the ant build descriptor content

    ant build file:

    <project name="jcg" basedir="." default="generate_jar">
        <!-- Fichero Properties -->
        <property file="build.properties"/>
    
        <!-- Create folders target -->
        <target name="create_folders">
            <echo>Crearing needed folders...</echo>
            <mkdir dir="${app_dir}"/>
            <mkdir dir="${app_dir}/META-INF"/>
            <echo>Done!</echo>
        </target>
    
        <!-- Compilarion Target -->
        <target name="compile">
            <echo>Compiling classes...</echo>
            <javac
                   encoding="UTF-8"
                   classpath="${classpath}"
                   srcdir="${source_dir}"
                   destdir="${app_dir}/${classes_dir}"
                   debug="true"/>
            <echo>Done!</echo>
        </target>
    
        <!-- Copy Target -->
        <target name="copy">
            <echo>Copying files...</echo>
            <copy todir="${app_dir}/${meta_dir}">
                <fileset dir="${root_dir}/${meta_dir}/" includes="*.xml"/>
            </copy>
            <echo>Copying META-INF</echo>
    
            <copy file="${root_dir}/${source_dir}/log4j.xml" todir="${app_dir}" />
            
            <echo>Cpoying classes...</echo>
            <echo>Done!</echo>
        </target>
    
        <!-- Clean Target -->
        <target name="clean">
            <delete dir="${app_dir}"/>
        </target>
    
        <target name="generate_jar">
            <echo>Generating jar...</echo>
            <antcall target="create_folders"/>
            <antcall target="compile"/>
            <antcall target="copy"/>
        	<jar destfile="jcg.jar" basedir="${app_dir}"/>
            <echo>Done!</echo>
            <antcall target="clean"/>
        </target>
    </project>
    

    ant build properties file:

    generated_dir=generated
    root_dir=.
    app_dir=app
    meta_dir=../META-INF
    classes_dir=.
    lib_dir=../lib
    jars_dir=jars
    source_dir=../src
    compiled_classes=classes
    classpath=../lib/log4j-1.2.17.jar;
    

    The project has a default task called generate_jar. This task is based on other tasks

    • create_folders -> Create the needed folders in order to build the jar structure. This task creates the app directory inside the build directory and the app/META-INF directory, that is the base structure for the jar to build
    • compile -> This task compiles all the classes inside the source classes directory. It will put the compiled classes inside the output app folder
    • copy -> This task is responsible for copy all the needed files inside the output folder

    After those task are executed, the main task will pack the output folder as a jar file, after all, will execute the clean task in order to delete all the temporaly folders used for build the jar file.

    If you run the ant descriptor file, you will see an output like this

    ant run output:

    Buildfile: C:\workspace\i+d\maven_vs_ant_ant\build\build.xml
    generate_jar:
         [echo] Generating jar...
    create_folders:
         [echo] Crearing needed folders...
        [mkdir] Created dir: C:\workspace\i+d\maven_vs_ant_ant\build\app
        [mkdir] Created dir: C:\workspace\i+d\maven_vs_ant_ant\build\app\META-INF
         [echo] Done!
    compile:
         [echo] Compiling classes...
        [javac] C:\workspace\i+d\maven_vs_ant_ant\build\build.xml:21: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
        [javac] Compiling 1 source file to C:\workspace\i+d\maven_vs_ant_ant\build\app
         [echo] Done!
    copy:
         [echo] Copying files...
         [echo] Copying META-INF
         [copy] Copying 1 file to C:\workspace\i+d\maven_vs_ant_ant\build\app
         [echo] Cpoying classes...
         [echo] Done!
          [jar] Building jar: C:\workspace\i+d\maven_vs_ant_ant\build\jcg.jar
         [echo] Done!
    clean:
       [delete] Deleting directory C:\workspace\i+d\maven_vs_ant_ant\build\app
    BUILD SUCCESSFUL
    Total time: 979 milliseconds
    

    Now, inside the build folder you will find the jcg.jar file.

    4. Maven project

    In this example we have a java project that will be packaged as a jar file. The project has an example class that defines a Main method that print Hello World! with log4j framework.

    You can see below the maven project

    Maven project
    Maven project

    You will see the maven folder convention for code and resources: src/main/java and src/main/resources and also for test environment: src/test/java and src/test/resources.

    Inside main java you will find the MyClass class, and inside the java resources you will find the log4j descriptor file.

    You only have to define the needed dependencies inside maven descripor file pom.xml in the project root folder. You can see it below

    pom:

    <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/maven-v4_0_0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>com.javacodegeeks.examples</groupId>
    	<artifactId>maven_vs_ant_maven</artifactId>
    	<version>1.0.0-SNAPSHOT</version>
    	<name>Maven VS Ant ::  example</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>log4j</groupId>
    			<artifactId>log4j</artifactId>
    			<version>1.2.17</version>
    		</dependency>
    
    	</dependencies>
    
    </project>
    

    Maven will take care of download and cache the requiered dependencies, compile the code and generate the jar file. If you run the maven descriptor file with clean install goals, you will see an output like this

    maven run output:

    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven VS Ant ::  example 1.0.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven_vs_ant_maven ---
    [INFO] Deleting C:\workspace\i+d\maven_vs_ant_maven\target
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven_vs_ant_maven ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 1 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven_vs_ant_maven ---
    [INFO] Compiling 1 source file to C:\workspace\i+d\maven_vs_ant_maven\target\classes
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven_vs_ant_maven ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ maven_vs_ant_maven ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven_vs_ant_maven ---
    [INFO] 
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven_vs_ant_maven ---
    [INFO] Building jar: C:\workspace\i+d\maven_vs_ant_maven\target\maven_vs_ant_maven-1.0.0-SNAPSHOT.jar
    [INFO] 
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ maven_vs_ant_maven ---
    [INFO] Installing C:\workspace\i+d\maven_vs_ant_maven\target\maven_vs_ant_maven-1.0.0-SNAPSHOT.jar to C:\Users\fhernans\.m2\repository\com\javacodegeeks\examples\maven_vs_ant_maven\1.0.0-SNAPSHOT\maven_vs_ant_maven-1.0.0-SNAPSHOT.jar
    [INFO] Installing C:\workspace\i+d\maven_vs_ant_maven\pom.xml to C:\Users\fhernans\.m2\repository\com\javacodegeeks\examples\maven_vs_ant_maven\1.0.0-SNAPSHOT\maven_vs_ant_maven-1.0.0-SNAPSHOT.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 3.329s
    [INFO] Finished at: Mon Mar 21 12:58:33 CET 2016
    [INFO] Final Memory: 14M/209M
    [INFO] ------------------------------------------------------------------------
    

    After running the maven command you will find the jar file under target folder.

    5. Projects difference

    As you can see ant require that you indicates all the needed task to do, in the correct order, and depends on you to put all the things in the correct place, to compile the classes and so on…

    In the other side, maven take care for you of compile classes, copy resources, generate jars file and so on…

    Maven project is more standard, is easier to create and to maintain and is better for manage dependencies than ant project.

    6. Conclusions

    As we have saw the maven project is much easier than the ant project. Maven will take care of some parts for you automatically, in front of it, you have to instruct ant in how it has to make those kind of things, like compile the code, copy files, execute tests, build the result structure, package it as a jar file and so on. With maven, you only have to be worried about tell it what are the needed dependencies.

    7. Download

    Download
    You can download the full source code of this example here: maven vs ant
  • Francisco Hernandez

    JEE technologies geek and development engineer. I have over 13 years of experience as software engineer in JEE architectures: Design, development, improvement etc. Currently I work as software architect and consultant. I am mainly involved in projects related to the bank and energy sectors based on Java technologies and Oracle products. I am also very interested in open-source projects
    Subscribe
    Notify of
    guest

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    1 Comment
    Oldest
    Newest Most Voted
    Inline Feedbacks
    View all comments
    Melo Mashabane
    Melo Mashabane
    6 years ago

    Very good article. Thoroughly understood difference between ant and maven.

    One drawback are the spelling errors. This may deter readers. It gives the impression that the information is not authentic.

    Back to top button