Skip to Content

The Ant Build Tool

ant (or Ant) is the Java world’s equivalent of make, the old standby build tool in the C/C++ world.

The official Ant home is https://ant.apache.org/ and this is a good place to start. It has a full manual, which is probably overkill, but if you go to “Using Apache Ant” and then “Writing a Simple Buildfile”, there is an example in that page that is kind-of simple, but not quite as simple as possible. Its “Example Buildfile” contains an example that initializes the build directory, compiles the source code (that is in a src/ directory) into class files (in the build/ directory), and that packages up the compiled code into a jarfile for distribution! And it can clean up after itself (deletes class files)!

Just like make takes a Makefile, ant takes a build file – but Ant build files are written in XML, and the default file name to use is build.xml. That is, if you just type the command ant, it will look for a file named build.xml in the current directory, and it will perform the build actions that are described in that file.

Below is a simpler Ant build file for your Hello World program for lab 0 (assuming you named your class HelloWorld):

<project name="helloworld" default="run">

 <target name="compile">
   <javac srcdir="." includeantruntime="false" debug="true" />
 </target>
 
 <target name="run" depends="compile">
   <java classname="HelloWorld" fork="true" />
 </target>

 <target name="clean">
   <delete>
     <fileset dir="." includes="*.class"/>
   </delete>
 </target>

</project>

With this Ant build file you can then do (each command-line command is in bold):

prompt> ls
build.xml  HelloWorld.java
prompt> ant
Buildfile: /home/jcook/ws/classes/cs371/examples/helloworld/build.xml

compile:
    [javac] Compiling 1 source file

run:
     [java] Hello World!

BUILD SUCCESSFUL
Total time: 0 seconds
prompt> ls
build.xml  HelloWorld.class  HelloWorld.java
prompt> ant
Buildfile: /home/jcook/ws/classes/cs371/examples/helloworld/build.xml

compile:

run:
     [java] Hello World!

BUILD SUCCESSFUL
Total time: 0 seconds
prompt> ant clean
Buildfile: /home/jcook/ws/classes/cs371/examples/helloworld/build.xml

clean:

BUILD SUCCESSFUL
Total time: 0 seconds
prompt> ls
build.xml  HelloWorld.java
prompt> ant
Buildfile: /home/jcook/ws/classes/cs371/examples/helloworld/build.xml

compile:
    [javac] Compiling 1 source file

run:
     [java] Hello World!

BUILD SUCCESSFUL
Total time: 0 seconds

Notice what happens as the ant commands are performed. The first ant command causes Ant to both compile the code and then run it (the default target action is set to “run” in the build.xml file). But just like make, in the second ant command, Ant recognizes that an up-to-date class file already exists, and so it does not compile it again, it only runs it (there is no action after the “compile:” tag in the output). Then in the third ant command, I bypass the default target and supply “clean” as the target. This causes the “clean” rule to be executed, which deletes the class file. Now I finally do a fourth ant command, and it has to both compile and run my program, since the class file had been deleted.

This shows you the power of having a build tool automate the various tasks that you need to do to compile and test-run your program!

Try it out on your own Hello World program!