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:
- The Begin and End tags for project (<project> and </project>) MUST start and end the file.
- The Begin <project> MUST have an attribute called default which is the name of one of the targets.
- Each build file must have at least one target.
- The Begin and End tags for <target> and </target> must also match EXACTLY.
- Each target MUST have a name.
- Targets depend only on other targets and reference them by their target name. Targets NEVER depend on projects or tasks.
- Target depends are optional.
- Anything between <echo> and </echo> tags is outputted to the console if the surrounding target is called.
- 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 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
Which triggers only the init target
Buildfile: C:\AntClass\Lab01\build.xml init: [echo] Running target init BUILD SUCCESSFUL Total time: 188 milliseconds