A guide to Ant, an advanced build system

From Elvanör's Technical Wiki
Jump to navigation Jump to search

Ant is a very advanced and flexible build system. It seems really modular and able to accomplish a lot of tasks. However, it also seems complex to understand and learn.

The Basics

  • Ant build files are XML files.
  • These XML buildfiles (default name, like "Makefile" for GNU Make, is "build.xml") contain a list of targets. A target itself contains a list of tasks.
  • A task is anything Ant can do: it can launch a Java compilation, copy files to a directory, and LOTS of other stuff.

Hints & Tips

  • To import another file from a first one, you can use the import task:
<import file="configuration.ant.xml"/>

In this case, the imported file must be a valid Ant XML file. You can also (more easily) just include a list of properties from a file directly with the property task.

  • To be able to reference a variable and expand it (eg, for ${DeploymentDirectory} to be replaced by /var/www/), you must use a property (here, with the name "DeploymentDirectory"). It won't work if you try to reference an XML node, for example.
  • In the javac task, don't include part of the package structure in the srcdir attribute. Eg, don't write srcdir=src/com/example/. Else it will confuse Ant, and Ant will rebuild every Java source file at every run.
  • It does not seem possible to easily echo a file set. However you can build path structures containing file sets very easily with nested fileset nodes.
<path id="hosted.class.path">
  <pathelement path="${java.class.path}/" />
  <fileset dir="/usr/share/tomcat-5.5/common/lib" casesensitive="yes">
  <include name="**/*.jar" />
</fileset>
  • To use special characters like " (quotes) in Ant, use the HTML/XHTML special representations (
    "
    ).