Java Reference
In-Depth Information
Working with build files and targets
As we've mentioned, Ant takes its cues from an XML build file, typically named
build.xml. An Ant build file is a collection of build targets . Each target defines a
series of sequential tasks, which are executed to perform some specific task. For
example, a target might create a JAR file from a collection of class files or gener-
ate documentation via an XSLT transform.
A target can also specify dependencies on other targets in the file. If a target
has dependencies, the dependent targets are executed first. If a target's job is
building a JAR file, for example, a likely dependency is the target in charge of
compiling the project's source. If any dependent targets fail, execution of the
build stops immediately, and errors are reported back to the user. Listing 5.1
shows an example of a very simple build file that compiles Java source code and
archives it into a JAR file.
Listing 5.1
A simple Ant build file that can compile classes into a JAR file
<project name="Example" default="dist" basedir=".">
<property name="src" value="src"/>
<property name="build" value="build" />
<property name="dist" value="dist"/>
<target name="compile" >
<mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile">
<mkdir dir="${dist}"/>
<jar jarfile="${dist}/example.jar" basedir="${build}"/>
</target>
</project>
Each task in the Ant toolkit performs a different operation and requires different
arguments. As you saw in an earlier example, the task for compiling looks differ-
ent than the task that makes directories. Ant is good at operating on large groups
of files, such as source code and output folders. It has a sophisticated pattern-
matching scheme that lets you group different types of files together by status,
type, or location.
Each target in an Ant build file is given a unique name. This name is used not
only to relate dependencies between targets but also to direct execution. When
you invoke Ant, you specify the target you wish to run. This lets you create a series
of targets designed for different purposes. For example, one target may build
 
 
 
 
 
Search WWH ::




Custom Search