< Zurück | Inhalt | Weiter >

Chapter 9


An Introduction


Ant1 is a tool for building and deploying collections of files. It is particularly suited to building and deploying Java applications (in no small part because it is written in Java). Ant is well worth knowing and using for all but the simplest Java applications.



• How to install and set up Ant.

• Enough about XML to read an Ant buildfile.

• The basic tags used in most buildfiles.

• The extent of our remaining ignorance after completing the chapter.


1. I must mention something about the title of this chapter: There was a Monty Python’s Flying Circus episode that had this title, and my inner geek couldn’t resist.



James Duncan Davidson had a problem. Perhaps you’ve had this problem, too. It has to do with the building of software—compiling, copying, and otherwise modifying files to get all the pieces in all the right places for running a collection of programs.

There are a number of ways to automate the building of software. You can script the compilation using shell scripts or batch jobs. That works fine, but there are two problems with that solution. First, scripts are generally not portable across very different operating systems. That’s a serious problem for a language like Java, which is intended to be portable across operating systems. Second, it is difficult if not impossible, using scripting languages, to prevent wasted compiles; the checking and comparing of date stamps on source and object files makes scripts large and difficult to maintain.

Very well, we hear you say. There’s make. The make program has been around for a long time. It is available on many operating systems. It handles the conditional compilation of files very well. It has been around for centuries (it seems). It is well known and widely used. All of this is true, but even this venerable tool falls a bit short in the Java world. First of all, although makefiles are generally far more portable than other scripts, there are still considerable variations in details, and make does nothing to mask the differences in file, path, and disk designations that exist across operating systems. Moreover, both make and scripts suffer from a more basic problem. Although Java programs can execute reasonably quickly, the overhead of starting a JVM and tearing it down again is considerable. Since javac is written in Java, each time it is in- voked to compile a source file (one file at a time is the make way) this setup and teardown time cost is paid.

But, we once more hear you protest, you can just use javac on the entire project! Doesn’t it build everything that needs to be built? In the simplest case, yes, it does. But as soon as you share code between projects, or use RMI which requires execution of the rmic compiler, or use EJBs, or link in native methods, then javac’s dependency resolution just will not work.

Luckily, James Duncan Davidson had this problem. And luckily it really bothered him. And even more luckily for us all, he decided to share his solution.

His solution was Ant, which we will from now on refer to as ant. Why ant? Well, he suggests that it might be because ants are little things2 that build big things. He has also suggested (in his preface to the O’Reilly book Ant: The Definitive Guide, Jesse Tilly and Eric Burke) that it might stand for “Another Neato Tool.” We’re inclined to put forth the former, but believe the latter.

James Duncan Davidson wrote ant and contributed it to the Apache project, so it is Free Software. And it makes the problems cited above rather piffling. Through the rest of this chapter we will describe how.


You can obtain ant from the Apache Web site.3 Which version you download will depend on your system and your needs. There are stable releases and daily builds. Unless you have a compelling need for a feature not yet in a stable re- lease, we would suggest you stick with the most recent stable release. As of this writing, that is version 1.5.1.

If you are using RedHat, or another Linux distribution that uses the RedHat Package Manager, rpm, then the simplest way to install would be to download the RPMs linked from the Web site and install those:

$ rpm -i ant-1.5.1-3jpp.noarch.rpm

You have two other options besides the trusty old RPM method. First, you may download a binary tarball, a word often used for a compressed file created with the tar utility, or you may download and compile the ant source code.

Let’s take these each in turn.


9.3.1 Installing a Binary Tarball

9.3.2 Installing a Source Tarball

9.4.1 XML for the Uninitiated

9.4.2 The Buildfile Tags

9.4.3 A Real, Live Buildfile