Home > Articles

  • Print
  • + Share This
This chapter is from the book

Building with Ant

Apache Ant is a Java-based build tool. It helps automate the compile process. If you need to compile just a handful of files, or many files that are all in the same directory, javac will do. However, when the package structure becomes complex, a build tool such as Ant, modeled after Make, is helpful.

Ant has two compelling features: It is extended using Java classes, and the configuration files are XML-based. Therefore, you can write a program that is essentially a wrapper for Ant and have unparalleled control over the build process. On the other end, you can control the build with only simple changes to XML files.


Avoid including working or temporary files in your project submission. You will likely use test classes, backup source files, and tool configuration files. Make sure none of these files find their way to your evaluator's screen. Using Ant can ensure that only the essential files are "distributed" to the certification evaluator.

You can get the binary edition of Ant from the Ant Web page at http://ant.apache.org/. You must have a Java API for an XML Processing (JAXP)–compliant XML parser installed on your classpath. With the SDK 1.4, you don't have to do anything because JAXP is included (see http://java.sun.com/xml/). After you install Ant (see http://ant.apache.org/manual/install.html#installing), you can start building with it.

Many popular Java Integrated Development Environments (IDEs) have Ant functionality. For example, JBuilder automatically recognizes Ant build files named build.xml. It even has an Ant wizard. Also, the Unix launch script that comes with Ant works correctly with Cygwin, but you might have to tweak things, such as Cygwin file paths.

Follow these three steps to get started with Ant:

  1. Download the latest version.

  2. Install Ant.

  3. Read the manual.

After these steps, compiling an application with Ant is as simple as issuing this command:



When compiling with Ant, you might need to tweak the build.xml before issuing the simple compiling command ant. An example is provided later in this chapter.

This simple command runs Ant using the build.xml file in the current directory, on the default target specified in the same build.xml configuration file. The following line shows how you will probably use it:

ant -buildfile scjd.xml mypackage

This command runs Ant using the scjd.xml file in the current directory on the target called mypackage.


Use Ant to automate the build task. Also, you can configure Ant to generate javadoc documentation and even place the whole project into a single JAR file, just as the instructions demand.

The configuration XML file, usually build.xml, is the key to using Ant. In this configuration file are the instructions Ant follows. A task is a discrete step that Ant performs. Basically, you place a sequence of tasks in the build.xml file. The following simple example (tested with Ant 1.4.1) compiles the *.java files in the source directory (subdirectory of the current directory) and places the generated *.class files in the distribution directory. Notice that a temporary build directory is also created. You can also add a task to delete temporary files.

<project name="SCJD" default="distribution" basedir=".">
    practice SCJD build file
 <property name="source" location="source"/>
 <property name="build" location="build"/>
 <property name="distribution" location="distribution"/>

 <target name="initialization">
  <!-- command that assigns DSTAMP -->
  <mkdir dir="${build}"/>

 <target name="compile" depends="initialization"
    description="compile the source " >
  <!-- Compile *.java in ${source} and place
     *.class files into ${build} -->
  <javac srcdir="${source}" destdir="${build}"/>

 <target name="distribution" depends="compile"
    description="generate the distribution" >
  <mkdir dir="${distribution}/library"/>
  <jar jarfile="${distribution}/scjd-${DSTAMP}.jar" 

These are the primary Ant command-line options:

  • -help—Prints out the help file.

  • -version—Prints the Ant version.

  • -diagnostics—Prints bug report.

  • -quiet, -q—Prints minimal information.

  • -verbose—Prints everything.

  • -debug—Prints debug information.

  • -logfile, -l—Specifies the log file.

  • -buildfile—Names the build file.

  • -propertyfile—Specifies using properties from this file.

Using Ant removes one burden by automating the build process. You can also include tasks for compiling the project, generating javadoc, and packaging all your project files into one JAR file, thus meeting another requirement along the way.

Because the configuration file is XML, several tags could be useful for your project. The following primary task "types" are the ones you are most likely to use with the Ant tool:

  • Description—Describes the project.

  • DirSet—Defines a group of directories.

  • FileList—Defines an explicitly named list of files.

  • FileSet—Defines a group of files.

  • File Mappers—Tells Ant how to find a source file.

  • Selectors—Specifies a way to select files from a FileSet using one of the following options:

    • <contains>—Contains a particular string.

    • <date>—Filters file by date.

    • <depend>—Modified more recently than other files it "depends" on.

    • <depth>—Files are x directories down in the tree.

    • <filename>—Filters by filename.

    • <size>—Filesare selected by size.

These types are used throughout the build.xml configuration file. If you want to speed up the build process for your certification project, I recommend installing and using Ant. It takes about an hour to download, install, configure and test. This is a good investment; it will save you many hours because you need to build your application numerous times before submitting it.

  • + Share This
  • 🔖 Save To Your Account