Writing a good ant build file

Some important rules to remember if you write an ant build file:

Shorter is better. There should be as few “public” targets as possible (where a “public” target is defined as one which shows up in the output of “ant -p”; that is, a target with a description attribute).

In particular, never create small “convenience” targets like “clean-and-compile”. They add no value whatsoever, especially since anyone can simply run a command like “ant clean jar” to do the same thing using the individual targets.

Every ant build file should enable a new developer to build with no assistance other than what “ant -p” provides. That is, “ant -p” should tell a developer everything he/she needs to know in order to compile and create a complete jar file from scratch, with no preparation other than having checked out the unit’s entire tree from CVS.

This means “public” targets (those with a description attribute) should have names and descriptions which are short but informative. Don’t give descriptions to targets that people will probably never want to run separately; they’ll just add clutter to the output of “ant -p”.

Avoid dependence on other property files, or other XML files. If your ant build file is so large that you feel it should be split into pieces, then it’s probably too complicated and needs to be redesigned. Reuse of targets from “library” files may seem noble but the cost is greater than the benefit. The simplicity of usage that comes from having a single atomic build file is much more valuable than saving twenty or so lines.

Never require an external properties file. It is acceptable to read in one external properties file, as long as the build can proceed in a perfectly smooth manner without such a file. Remember that anyone can set properties using the -D option and people rarely need to customize so many properties that an external file is needed to hold them all. If they do, the build file needs to be redesigned so it assumes sensible defaults.

ant is not a shell script language. It is a dependency tool. Do not attempt to do ant’s work by explicitly checking for files or timestamps, except in the case of tools which don’t do that checking themselves. Do not add redundant <echo> elements which tell the user things which are already obvious from ant’s output, such as:

<echo message="Compiling java files..."/>
<echo message="Compile finished at ${time}."/>