ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Open Source Java: Ant

by David Thomson

Ant, an open source Java project, has been gaining in usage and attention lately -- and rightfully so. Ant is driving ease of use in cross-platform software development by dragging the redoubtable "make"-style of build tool into this new century. But where did Ant come from?


Ant is based at the Apache Jakarta project's site (see resources listed at the end of this article), which says Ant is "kind of like make without make's wrinkles". It goes on to explain the reasoning behind the development of a replacement for make and make-like programs: all of these programs have serious flaws when it comes to building cross-platform software, which is of particular impact for certain Java projects.

As such, Ant is an ideal choice for building other Java-based projects, although it can also be used for building projects written in other languages.

Makefiles (and IDEs) Considered Harmful

Traditionally, makefiles are based around an arcane ordering of tab characters and spaces. This makes life particularly awkward for programmers who are new to using makefiles. Even those who have worked with them for a while can have trouble.

Integrated Development Environments (IDEs) appear to remove this reliance on buildfiles, but in fact they are not interchangeable between different IDEs, even if your favorite IDE happens to exist on another target platform.

Why Use Ant?

So why has Ant experienced such a rapid uptake in the development community? There are several reasons, but I feel the main ones are as follows.


Ant is an open source project available under the Apache license. You can download and modify the source code as with all other open source projects. Ant's extensibility means, however, that most modifications you may require can be included via mechanisms which we'll cover later.

Additionally, Ant uses XML buildfiles. This means that anyone conversant with basic XML structures and formatting can comprehend and write an Ant file. And from a development point of view, almost everyone understands basic XML.

Cross Platform

Along with XML, the use of Java to develop Ant makes it the perfect solution for those people developing programs designed to run or be built across a range of different operating systems.


Ant is extensible in two ways: new tasks and build listeners. New tasks are used to extend the capabilities of the build process, while build listeners are used to help hook into the build process to add extra error tracking functionality.


Finally, the fact that Ant is extensible and open means that integration with your favorite editor or development environment becomes very easy. Indeed, several people have contributed packages to hook Ant into the most popular environments.

Ant in Practice

If you're convinced of Ant's merits, you'll want to know how to use it. There's not enough space to cover all the details of creating Ant buildfiles. So we'll go over a couple of simple files to examine how Ant can be extended with Java.

There are a few basic elements used throughout these buildfiles that you should be aware of.


This is the central element of the buildfile. There should be only one project per buildfile. Each project may contain one or more targets, and you should always specify a default target to be executed.


A target is a set of tasks that are to be executed which may depend on other targets. For example, it is good practice to have an initialization target which executes a timestamp task prior to an actual compilation target.


A task is a piece of code that can be executed. It can have multiple attributes or arguments. The value of an attribute might contain references to a property. These references will be resolved prior to task execution. Ant includes qtasks written for everything you might want to do during a build process. As we'll see later, it is easy to add new tasks in case you have a specific requirement.


Each project can have a set of properties, which are simply name-value pairs. They can be defined either in the project file or external to Ant. Ant includes these properties:


the absolute path of the project's basedir (as set with the basedir attribute of <project>)


the absolute path of the build file


the Java VM version Ant detected

Running Ant

In order to run Ant, you need the Java Development Kit from Sun (1.1 or higher), an XML parser, and Ant itself. You can use Sun's free XML parser if you don't have one.

Installing Ant requires adding Ant and the XML parser class files to the classpath.

A Simple Example

This example assumes we have a typical Java project -- a directory structure of source files which we want to build into a separate directory containing the compiled class files.

<?xml version="1.0"?>

<project name="HelloProject" default="compile" basedir=".">

<!-- set global properties for this build -->
<property name="src" value="." />
<property name="build" value="classes" />

<target name="init">
<!-- Create the time stamp -->

<target name="compile" depends="init">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" />

Taking each section in turn, we see that the <project> element defines a project name, a default target, and a base directory. Secondly, we see that two properties are set and available for use in the project targets: where the source files are, relative to the base directory; and where the compiled classes should be written to.

Next, we define the task init, which serves only to create a project timestamp. This sets the DSTAMP, TSTAMP, and TODAY properties in the current project.

Finally, we see the actual compile target which depends on init. Before compile is executed, init will be executed in order to prepare the environment. Ant will attempt to execute the attribute targets, depending on the order they appear (from left to right).

Pages: 1, 2, 3

Next Pagearrow