Cover | Table of Contents | Colophon
http://www.oreilly.com/catalog/anttdg/.
<?xml version="1.0"?>
<!-- build.xml - a simple Ant buildfile -->
<project name="Simple Buildfile" default="compile" basedir=".">
<!-- The directory containing source code -->
<property name="src.dir" value="src"/>
<!-- Temporary build directories -->
<property name="build.dir" value="build"/>
<property name="build.classes" value="${build.dir}/classes"/>
<property name="build.lib" value="${build.dir}/lib"/>
<!-- Target to create the build directories prior to the -->
<!-- compile target. -->
<target name="prepare">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.lib}"/>
</target>
<target name="clean" description="Removes all generated files.">
<delete dir="${build.dir}"/>
</target>
<target name="compile" depends="prepare"
description="Compiles all source code.">
<javac srcdir="${src.dir}" destdir="${build.classes}"/>
</target>
<target name="jar" depends="compile"
description="Generates oreilly.jar in the 'dist' directory.">
<!-- Exclude unit tests from the final JAR file -->
<jar jarfile="${build.lib}/oreilly.jar"
basedir="${build.classes}"
excludes="**/*Test.class"/>
</target>
<target name="all" depends="clean,jar"
description="Cleans, compiles, then builds the JAR file."/>
</project>
http://www.oreilly.com/catalog/anttdg/.
<?xml version="1.0"?>
<!-- build.xml - a simple Ant buildfile -->
<project name="Simple Buildfile" default="compile" basedir=".">
<!-- The directory containing source code -->
<property name="src.dir" value="src"/>
<!-- Temporary build directories -->
<property name="build.dir" value="build"/>
<property name="build.classes" value="${build.dir}/classes"/>
<property name="build.lib" value="${build.dir}/lib"/>
<!-- Target to create the build directories prior to the -->
<!-- compile target. -->
<target name="prepare">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.lib}"/>
</target>
<target name="clean" description="Removes all generated files.">
<delete dir="${build.dir}"/>
</target>
<target name="compile" depends="prepare"
description="Compiles all source code.">
<javac srcdir="${src.dir}" destdir="${build.classes}"/>
</target>
<target name="jar" depends="compile"
description="Generates oreilly.jar in the 'dist' directory.">
<!-- Exclude unit tests from the final JAR file -->
<jar jarfile="${build.lib}/oreilly.jar"
basedir="${build.classes}"
excludes="**/*Test.class"/>
</target>
<target name="all" depends="clean,jar"
description="Cleans, compiles, then builds the JAR file."/>
</project>compile, type the following command from the
directory containing our sample build.xml file:
ant
compile). You should see the following output,
assuming your directory is called antbook:
Buildfile: build.xml
prepare:
[mkdir] Created dir: C:\antbook\build
[mkdir] Created dir: C:\antbook\build\classes
[mkdir] Created dir: C:\antbook\build\lib
compile:
[javac] Compiling 3 source files to C:\antbook\build\classes
BUILD SUCCESSFUL
Total time: 5 secondsprepare
followed by compile. This is because
compile is the default target, which has a
dependency on the prepare target. Ant prints the
name of each task within brackets, along with other messages unique
to each task.
ant -buildfile proj.xml
clean target, we type ant -buildfile
proj.xml clean. Our output would look something like
this:
Buildfile: proj.xml clean: [delete] Deleting directory C:\antbook\build BUILD SUCCESSFUL Total time: 2 seconds
ant [option [option...]] [target [target...]] option := {-help |-projecthelp |-version |-quiet |-verbose |-debug |-emacs |-logfile filename |-logger classname |-listener classname |-buildfile filename |-Dproperty=value |-find filename}
description
attribute of any <target>, along with any
text contained within a <description>
element. Targets with description attributes are
listed as "Main targets," those
without are listed as "Subtargets."
echo
task in the buildfile.
[
taskname
].
<project> element with its
nested <target>,
<property>, and
<path> elements.
<project default="all">
<property name="a.property" value="a value"/>
<property name="b.property" value="b value"/>
<path id="a.path">
<pathelement location="${java.home}/jre/lib/rt.jar"/>
</path>
<target name="all">
<javac srcdir=".">
<classpath refid="a.path"/>
</javac>
</target>
</project><project> element
and at least one <target> element.
<project>
element's default attribute.
<project> element per
buildfile.
http://jakarta.apache.org/ant/;
you can choose either a release build or a nightly build. To ease
installation, different distributions are provided for Unix and
Windows systems.
http://jakarta.apache.org/builds/jakarta-ant/nightly/.
The nightly builds include the latest bug fixes and features, and are
a constantly changing target. The vast majority of software
development teams should opt instead for a release version of Ant,
available at the following URLs:
http://jakarta.apache.org/builds/jakarta-ant/release/v1.4.1/bin/
http://jakarta.apache.org/builds/jakarta-ant/release/v1.4.1/src/
http://jakarta.apache.org/ant/;
you can choose either a release build or a nightly build. To ease
installation, different distributions are provided for Unix and
Windows systems.
http://jakarta.apache.org/builds/jakarta-ant/nightly/.
The nightly builds include the latest bug fixes and features, and are
a constantly changing target. The vast majority of software
development teams should opt instead for a release version of Ant,
available at the following URLs:
http://jakarta.apache.org/builds/jakarta-ant/release/v1.4.1/bin/
http://jakarta.apache.org/builds/jakarta-ant/release/v1.4.1/src/
|
Directory
|
Description
|
|---|---|
|
bin
|
Batch files, Perl scripts, and shell scripts for running Ant.
|
|
docs
|
Ant documentation.
|
|
lib
|
Libraries required by Ant to run.
|
|
src
|
Source code for Ant. Provided only in the source
distribution.
|
CLASSPATH includes
ant.jar and any JAXP-compliant XML parser.
CLASSPATH. For Java 2,
tools.jar must be added. This is necessary for
tasks like javac.
ant.home Java system
property be set to the Ant installation directory. This is
accomplished by launching the JVM with the -D
flag, as shown shortly.
CLASSPATH.
java -Dant.home=pathToAnt org.apache.tools.ant.MainANT_HOME
/lib. These, along
with any other JAR files found in this directory, are automatically
added (by ant or ant.bat)
to the CLASSPATH used by Ant.
jaxp.jar contains the JAXP API, while
parser.jar is an XML parser implementation from
Sun.
http://java.sun.com/xml/. This
won't affect you if you are using Ant 1.4 or later,
since it ships with JAXP 1.1. Since JAXP 1.1 is backwards compatible
with JAXP 1.0, you can safely replace the
<project> — to the lowest-nested tag,
the document object model (or DOM). The first or
root element of any buildfile is always the
<project> tag. No buildfile can be without
one, nor can it have more than one. The DOM lays elements out in a
tree-like hierarchy, making the buildfile more of an object model
than simply a plain process-description document. The following
example shows a valid project tag:
<project name="MyProject" default="all" basedir="."> ... </project>
<project> tag has three attributes:
name, default, and
basedir. The name attribute
gives the project a name. A project name is valuable for purposes of
identifying log output (to know what project you're
building). For systems that manage buildfiles, such as an IDE that
can read buildfiles, the project name acts like an identifier for the
buildfile. The default attribute refers to a
target name within the buildfile. If you run Ant without specifying a
target on the command line, Ant executes the default target. If the
default target doesn't exist, Ant returns an error.
While we do not recommend it, the value of default
does not have to be a valid target name (i.e., a name corresponding
to an actual target name in the buildfile). We suggest either making
the default target compile everything or display help for using the
buildfile. The basedir attribute defines the root
directory of a project. Typically, it is
".", the
directory in which the buildfile resides, regardless of the directory
you're in when you run Ant. However,
basedir can also define different points of
reference. For example, a buildfile that is part of a hierarchical
project structure needs a different reference point, referring to the
project's root directory. You can use the
http://dreamland.tky.hut.fi/IrssiBot). This
project requires all the features of a typical build: compiling
source code, packaging classes, cleaning directories, and deploying
the application. As an exercise, we took this project and converted
it to use Ant.
<project>
element, Ant does a breadth-first traversal of the XML elements. This
means that it loads and processes all of the elements just below the
level of the <project> element first, and
then moves on to the first target. Inside a target, Ant does a
depth-first traversal. This means that, starting with a
target's first element, Ant processes each element
as deep as it can before it moves on to the next element.
<property> tag. This, of course, completely
ignores the rich data capabilities of XML, and Ant's
developers know this. In addition to the property,
there is the concept of an Ant data element — for example, the
path DataType. The limitation is you cannot create
DataTypes "in-language," as you can
with a scripting language. Instead, you must write a class (or a set
of classes) to represent a new data type in Ant; this is probably
more effort than you are willing to put in for simply encapsulating
groups of data values. If you're looking at Ant as
an XML-based build scripting language and want to create your own
data elements, you'll run into this dead-end fairly
quickly.
build.compiler property for the
javac task. No attribute exists for specifying the
type of compiler the javac task uses. Instead, it
relies upon the buildfile defining build.compiler.
If a task uses a magic property, we specify this in the task
definition provided in Appendix B.
<copy todir="newdir">
<fileset dir="foo">
<include name="**/*.xml"/>
</fileset>
</copy>copy task element copies any XML files from
foo to newdirfileset DataType being used to identify groups
of files to copy in order to deploy the irssibot application.
DataTypes are important when using Ant, and
fileset is just one of the many available to you:
org.apache.tools.ant.types package, usually
extending from the
org.apache.tools.ant.types.DataType base class.
EnumeratedAttribute,
Commandline, Environment, and
Reference are also treated as DataTypes, although
they do not extend from DataType. Figure 4-1 contains a basic UML class diagram
illustrating this aspect of Ant's design.
org.apache.tools.ant.ProjectComponent, provides
logging functionality as well as access to the
Project object. Not shown here,
ProjectComponent is also the base class for every
Ant task. These tasks are detailed in Chapter 7 and Chapter 8.
org.apache.tools.ant.types package, usually
extending from the
org.apache.tools.ant.types.DataType base class.
EnumeratedAttribute,
Commandline, Environment, and
Reference are also treated as DataTypes, although
they do not extend from DataType. Figure 4-1 contains a basic UML class diagram
illustrating this aspect of Ant's design.
org.apache.tools.ant.ProjectComponent, provides
logging functionality as well as access to the
Project object. Not shown here,
ProjectComponent is also the base class for every
Ant task. These tasks are detailed in Chapter 7 and Chapter 8.
"all" means Ant Versions 1.2 and later.
String indicates that an attribute holds textual
data. See Table 4-1.
|
Type name
|
Implemented by
|
Description
|
|---|---|---|
boolean |
N/A
|
Performs a case-insensitive string comparison, converting
on, true, and
yes to true. All other values
are false.
|
Enum |
org.apache.tools.ant.types.EnumeratedAttribute |
apply,
exec, and java tasks accept
nested <arg> elements, specifying
command-line arguments for their respective process calls. The
org.apache.tools.ant.types.Commandline.Argument
class implements this DataType. If several <arg>
elements are specified, each is treated as a separate argument to the
process call. Following is a list of all
<arg> attributes:
file (all, File,*)line (all, String,*) path (all, Path, *)value (all, String, *) java task to invoke Apache's
Xalan XSLT processor, transforming an XML file into HTML using
XSLT. As you might expect, the
java task invokes any Java class with a
main( ) method. Use <arg>
elements to pass arguments to the java task.
<?xml version="1.0"?>
<project name="arg demo" default="xslt" basedir=".">
<property name="xalan.home" value="C:/java/xalan-j_2_1_0"/>
<property name="xalan.jar" value="${xalan.home}/bin/xalan.jar"/>
<property name="xerces.jar" value="${xalan.home}/bin/xerces.jar"/>
<property name="xmldata" value="familyTree.xml"/>
<property name="stylesheet" value="familyTree.xslt"/>
<property name="result" value="Family Tree.html"/>
<path id="project.class.path">
<pathelement location="${xalan.jar}"/>
<pathelement location="${xerces.jar}"/>
</path>
<target name="clean">
<delete file="${result}"/>
</target>
<target name="xslt">
<echo message="Transforming '${xmldata}' using '${stylesheet}'"/>
<java fork="true" classname="org.apache.xalan.xslt.Process"
failonerror="true">
apply and
exec tasks, which execute system commands, accept
zero or more nested <env> elements. These
elements specify which environment variables are passed to the system
command being executed, and they are implemented by the
org.apache.tools.ant.types.Environment.Variable
class. The <env> element accepts the
following attributes:
file (all, File,*) key (all, String,Y) path (all, Path, *) value (all, String, *) file, path, or
value is required.
TOMCAT_HOME environment variable is available
because of the <env> element:
<property name="tomcat.home" value="/path/to/tomcat"/>
<target name="deploy">
<!-- Call a deployment script, setting up the TOMCAT_HOME -->
<!-- environment variable. -->
<exec executable="deploy.bat">
<env key="TOMCAT_HOME" value="${tomcat.home}"/>
</exec>
</target>exec and
env. Ant also allows you to use environment
variables within your own buildfiles. This is an excellent way to
avoid hardcoding, although it can limit portability. Because it deals
with environment variables, using environment variables in buildfiles
is closely related to the environment DataType.
However, the environment DataType is not used to
access environment variables from within Ant. Instead, this use of
environment variables is implemented as a special feature of the
property task, which is described in Chapter 7.
filelist is a DataType supporting a named list of
files, implemented by
org.apache.tools.ant.types.FileList. The files do
not have to exist in order to be included in a
filelist. Following are the allowable attributes:
dir (1.4, File, *)files (1.4, String, *)refid (1.4, Reference, N)<filelist> defined
elsewhere. The <filelist> being referred to
defines a list of files. This is useful if you wish to define a list
of files once, and then refer to it from several places in your
buildfile.
dir and files are
required, unless refid is specified, in which case
neither dir nor files is
allowed.
filelist DataType was introduced in Ant 1.4,
along with the dependset task. (Since
filelist is only used with
dependset, we must talk about the
dependset task to explain the
filelist DataType). The
dependset task compares one or more input files to
one or more output files. If any of the input files are newer, then
all of the output files are erased. Additionally, if any of the input
files are missing, all of the output files are erased. Comparing
output files to a set of input files that may not yet exist is why
the filelist DataType is necessary.
filelist DataType and the
dependset task is valuable. In this example, we
are comparing a list of XML and XSLT files to a single HTML file. The
HTML file, employeeDirectory.html, should be
erased if any input file is missing or newer than it.
<?xml version="1.0"?> <project name="filelist demo" default="xslt" basedir="."> <filelist id="stylesheets" dir="." files="header.xslt,footer.xslt,body.xslt"/> <filelist id="xmlfiles" dir="." files="employees.xml"/> <target name="xslt"> <!-- erase employeeDirectory.html if any of the XML files or XSLT stylesheets are newer --> <dependset> <srcfilelist refid="stylesheets"/> <srcfilelist refid="xmlfiles"/>
fileset DataType
defines a group of files and is commonly represented by the
<fileset> element. However, many Ant tasks
form implicit
filesets, which
means they support all fileset attributes and
nested elements. Unlike the filelist type, files
represented by fileset must exist.
Filesets may also be specified as target-level
buildfile elements (i.e., children of
<project>) and referenced by their
ids. Following is a list of
fileset attributes:
dir (all, Path, Y)fileset.casesensitive (1.4.1, boolean N)false, the fileset is
not case-sensitive when matching filenames. Defaults to
true. Ant versions prior to 1.4.1 use
case-sensitive matching.
defaultexcludes (all, boolean, N)true. Default excludes consists of:
**/*~, **/#*#,
**/.#*, **/%*%,
**/CVS, **/CVS/**,
**/.cvsignore, **/SCCS,
**/SCCS/**, and
**/vssver.scc.
excludes (all, String, N)excludesfile (all, File, N)includes (all, String, N)includesfile (all, File, N)fileset
may also contain the following:
patternset elements: <exclude>, <include>, <patternset> (all); <excludesfile>, <includesfile>. (1.4)fileset. All are described shortly in Section 4.7.
Other than <patternset>, these nested
elements are used in place of their corresponding attributes.
fileset depends heavily on
patternset, you should continue on and read the
"Patternset DataType" section after
studying these examples. The first example uses
filesets group
files together, patternsets group patterns. These
are closely related concepts, because filesets
rely on patterns to select files. The
<patternset> element may appear as a
target-level buildfile element (i.e., as a child of
<project>), and later be referenced by its
id. As shown in the previous examples, it may also
appear as a nested element of <fileset>.
Tasks that are implicit filesets also