In this tutorial, you will learn about Apache Ant – one of most popular build tools for software development with Java. By the end of the tutorial, you will understand the role of Ant in developing Java projects and able to develop a simple Java project using Ant build. You will also know how IDEs like Eclipse, NetBeans and IntelliJ IDEA support Ant.

 

1. What is Ant?

Apache Ant is a Java library and command-line tool for automating software build processes. Imagine you are working on a Java project that contains many classes and the build process involves in compiling .java files to .class files, then packaging the compiled files to an executable JAR file. In each build, the name of the JAR file is suffixed by the current date time to track versions and releases. If you do this build process manually, you have to use javac command to compile, jar command to package and maybe an OS-dependent command to change the JAR file name.

That build process is repetitive and time-consuming if done manually. So you can automate the build process by creating an Ant build script that describes all the tasks need to be executed. Then you can use only a single Ant command to build the project, comfortably.

That’s basically how Ant works and the reason why we need to use it. But Ant is not limited to that, it is a powerful tool that can be used to automate any type of process which can be described in terms of targets and tasks (a target contains a set of tasks).

 

2. A Brief History of Ant

Ant was created by James Ducan Davidson in early 2000 when he was working on the Apache Tomcat project. James was creating Tomcat’s builds for different operating systems and he found that there’s no cross-platform build tools he can use. He used the Make build tool on Unix, but on Windows he had to write different build script. So James wrote Ant in pure Java code as a replacement to Make.

Soon thereafter, Ant was used by several open source Java projects and spread like a virus. It became an independent project managed by the Apache Software Foundation – hence the official name Apache Ant. The name Ant is an acronym for “Another Neat Tool” – according to its original author James Duncan Davidson.

The first official release of Ant was Ant 1.1, released on 19 July 2000, and the latest version (at the time of this writing) is Ant 1.10.7 released on 5 Sep 2019.



Today Ant is the build tool of choice for a lot of projects, and it is the default build tool in NetBeans IDE (version 8.2 and older).

 

3. A Sample Ant Build script

Let’s look at a typical Ant build script to learn more. The default Ant build file name is build.xml which is usually placed in the project’s root directory. Following is the code of a sample Ant build script for a Java project:

<project name="MyProject" default="dist" basedir=".">

  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>

  <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}">
            <manifest>
                <attribute name="Main-Class" value="net.codejava.HelloAnt"/>
            </manifest>    
    </jar>
  </target>

  <target name="clean"
        description="clean up">
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
As you can see, we use XML to describe the build process in terms of targets and tasks. There are 4 targets defined in this build file: init, compile, dist and clean. When you run an Ant build, the default target will be executed.

In this project, the default target is dist:

<project name="MyProject" default="dist" basedir=".">
Look at the description of the dist target, you will see it depends on the compile target:

 <target name="dist" depends="compile”…>
In turn, the compile target depends on the init target:

<target name="compile" depends="init"…>
So the sequence of execution is init, compile and dist. The init target initializes resources for the build, e.g. create a timestamp and make the build directory. It is done using the tstamp and mkdir tasks:

<target name="init">
  <tstamp/>
  <mkdir dir="${build}"/>
</target>
Then the compile target compiles all Java source files and put the compile .class files to the build directory, using the javac task:

<target name="compile" depends="init">
  <javac srcdir="${src}" destdir="${build}"/>
</target>
And finally the dist target generates an executable JAR file and put it into the distribution directory dist/lib. It is done using the mkdir and jar tasks:

<target name="dist" depends="compile">
  <mkdir dir="${dist}/lib"/>

  <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}">
          <manifest>
              <attribute name="Main-Class" value="net.codejava.HelloAnt"/>
          </manifest>    
  </jar>
</target>
That’s the execution flow of this build file. And you can notice that the clean target doesn’t depend on any targets, which means it will run standalone if requested.

From the command line prompt, just type ant run the build starting from the default target. And you would see the following output:

run-ant-build 

You can also type ant<target> to run a specific target, for example:

run-target-clean 

This executes the clean target that deletes everything in the build and dist directories.

You can reuse this sample build.xml file for any Java project.

 

4. Benefits of using Ant

Ant helps automating the build process so you can save time and effort when developing Java projects using Ant build. You can avoid doing repetitive tasks manually, which is boring and time-consuming.

In addition, Ant is written in pure Java code so you can reuse Ant build files across platforms.


5. Drawbacks of using Ant

Ant build files, which are written in XML, can be complex and verbose when the project becomes complex and large. Ant doesn’t directly support dependency management (automatic download and management of library JAR files), though you can do it using Apache Ivy, which requires longer learning curve.

 

6. Ant supported by IDEs

NetBeans IDE versions 8.2 and older uses Ant as the default build for its projects. From newer versions (Apache NetBeans), it allows programmers to explicitly choose a build tool for new projects (Ant, Maven or Gradle). Eclipse and IntelliJ IDEA allow you to generate Ant build files for projects.

And all these IDEs support code completion and suggestion for Ant build files, which allows you to write Ant script easily.

To learn more, visit Apache Ant homepage.

 


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment