2.1. Introduction to Ant
Ant is concerned with the fundamental entities called tasks; the tasks to perform are collected in a build file. A task may be thought of as a single operation that corresponds roughly to a single command issued at a DOS or Unix shell prompt. Tasks may take parameters just as shell commands may take arguments. Unlike command arguments, task parameters are all named. This avoids the confusing issues of syntax and ordering that accompany shell commands and, by extension, make commands.Tasks are specified in a build file as XML nodes where the name of the node indicates the task to be performed and the attributes specify parameters to the task. The simplest task is called echo and it may be used from within an build file as
Ant provides tasks that manage compilation, file operations, archiving, and a great deal more. We will examine many of these shortly. Programmers can also create new tasks; this ability is discussed at the end of this chapter.Tasks are grouped into units called targets, which have a name and an optional description. Within a target, tasks are executed sequentially. A target that echos several consecutive messages could be written as
<echo message="Hello world!"/>
All targets are contained within the top-level node of the build file, called the project. The project specifies the default target to run, which may be overridden when Ant is started. The project may also include a name and description.A complete and valid build file would result from placing the preceding sample target within a project tag, such as
<target name="sample"
description="A basic target">
<echo message="This is a simple target"/>
<echo message="It just echos several messages"/>
<echo message="Not very exciting so far"/>
</target>
By default Ant will look for a file called build.xml when it is run. If build.xml contains the preceding project, then Ant can be invoked by issuing the command
<project default="sample">
...
</project>
under Windows or
ant.bat
under any variant of Unix, including Mac OS X. The result will be several lines of output
ant
This output announces the build file that Ant is using, which, as mentioned, is build.xml by default. It then announces the target that is being run, which is sample here. Each of the echo tasks in the sample target then runs and produces a line of output. Note that Ant prepends the name of the task to any output from that task, which can help clarify exactly what is happening. Finally, Ant announces that the build was successful, meaning that all tasks executed correctly, and announces how long the build took.[1]
Buildfile: build.xml
sample:
[echo] This is a simple target
[echo] It just echos several messages
[echo] Not very exciting so far
BUILD SUCCESSFUL
Total time: 5 seconds
[1] Five seconds may seem like a long time to print three lines of text. Much of this is the time required for the Java Virtual Machine (VM) to start up, the Ant classes to load, and the build file to be parsed. For larger projects this overhead will quickly diminish to insignificance, and overall Ant will be much faster than performing the corresponding actions by hand.