Apache Ant with Ivy

From Rasim SEN - Knowledge Base/Java/Springframework/Microservices/Cloud-AWS/AI
Jump to: navigation, search

In the beginning, Make was the only build automation tool, beyond homegrown solutions. Make has been around since 1976 and as such, it was used for building Java applications in the early Java years.

However, a lot of conventions from C programs didn’t fit in the Java ecosystem, so in time Ant was released as a better alternative.

Apache Ant (“Another Neat Tool”) is a Java library used for automating build processes for Java applications. Additionally, Ant can be used for building non-Java applications. It was initially part of Apache Tomcat codebase and was released as a standalone project in 2000.

In many aspects, Ant is very similar to Make, and it’s simple enough so anyone can start using it without any particular prerequisites. Ant build files are written in XML, and by convention, they’re called build.xml.

Different phases of a build process are called “targets”.

Here is an example of a build.xml file for a simple Java project with the HelloWorld main class:

    <target name="clean">
        <delete dir="classes" />
    <target name="compile" depends="clean">
        <mkdir dir="classes" />
        <javac srcdir="src" destdir="classes" />
    <target name="jar" depends="compile">
        <mkdir dir="jar" />
        <jar destfile="jar/HelloWorld.jar" basedir="classes">
                <attribute name="Main-Class"
                  value="antExample.HelloWorld" />
    <target name="run" depends="jar">
        <java jar="jar/HelloWorld.jar" fork="true" />

This build file defines four targets: clean, compile, jar and run. For example, we can compile the code by running:

ant compile

This will trigger target clean first which will delete the “classes” directory. After that, target compile will recreate the directory and compile src folder into it.

The main benefit of Ant is its flexibility. Ant doesn’t impose any coding conventions or project structures. Consequently, this means that Ant requires developers to write all the commands by themselves, which sometimes leads to huge XML build files which are hard to maintain.

Since there are no conventions, just knowing Ant does not mean we’ll quickly understand any Ant build file. It’ll likely take some time to get accustomed with an unfamiliar Ant file, which is a disadvantage compared the other, newer tools.

At first, Ant had no built-in support for dependency management. However, as dependency management became a must in the later years, Apache Ivy was developed as a sub-project of the Apache Ant project. It’s integrated with Apache Ant, and it follows the same design principles.

However, the initial Ant limitations due to not having built-in support for dependency management and frustrations when working with unmanagable XML build files led to the creation of Maven.