Apache Ant/Build File Structure

Here is the structure of a typical build.xml file:

  <?xml version="1.0"?>
  <project name="MyFirstAntProject" default="MyTarget">
     <target name="init">
        <echo>Running target init</echo>
     </target>
     <target name="MyTarget" depends="init">
        <echo>Running target MyTarget</echo>
     </target>
  </project>

Here are a few things to note:

  1. The Begin and End tags for project (<project> and </project>) MUST start and end the file.
  2. The Begin <project> MUST have an attribute called default which is the name of one of the targets.
  3. Each build file must have at least one target.
  4. The Begin and End tags for <target> and </target> must also match EXACTLY.
  5. Each target MUST have a name.
  6. Targets depend only on other targets and reference them by their target name. Targets NEVER depend on projects or tasks.
  7. Target depends are optional.
  8. Anything between <echo> and </echo> tags is outputted to the console if the surrounding target is called.
  9. Every task has to be in a target.

You can execute this from a DOS or UNIX command prompt by creating a file called build.xml and typing:

  ant

Ant will search for the build file in the current directory and run the build.xml file.

Here is a sample output of this build:

  Buildfile: C:\AntClass\Lab01\build.xml
  init:
       [echo] Running target init
  MyTarget:
       [echo] Running target MyTarget
  BUILD SUCCESSFUL
  Total time: 188 milliseconds

Optionally you can also pass ant the name of the target to run as a command line argument

  ant init

Which triggers only the init target

  Buildfile: C:\AntClass\Lab01\build.xml
  init:
       [echo] Running target init
  BUILD SUCCESSFUL
  Total time: 188 milliseconds

Next Chapter