0..n nested elements all Specifies which files and directories are passed as arguments to the command... flatten all, boolean, N If true, the directory structure of the source files is
Trang 1target (all, String, N)
The name of the target to invoke in the new project If omitted, the new project's default target is invoked
Content
0 n nested <property> elements (all)
Passes a property to the new build process
Example Usage
Invoke the default target on util_buildfile.xml in the current directory:
<ant antfile="util_buildfile.xml"/>
Invoke the clean target on build.xml in the gui directory:
<ant dir="gui" target="clean"/>
Invoke another buildfile, passing a new value for the builddir property The value is explicitly set to utiloutput even if the property was defined elsewhere in the calling buildfile:
Invokes a target in the current buildfile Properties are passed to the new target using nested
<param> elements An investigation of the Ant source code reveals that antcall instantiates and calls the ant task using the current buildfile This means that a new project instance is created and properties work the same as they do for ant
Attributes
inheritall (1.4, boolean, N)
Defines how properties are propagated to the new target Defaults to true, meaning all properties in the current build process are inherited by the new target Prior to Ant 1.4,
Trang 2Ant: The Definitive Guide
126
this was the only behavior When false, properties set by users on the command line are the only ones passed to the new target
target (all, String, Y)
The name of the target to call
Content
0 n nested <param> elements (all)
Passes a property to the new build process Each <param> element is implemented using the same class as the property task; all property attributes are applicable
Trang 3output (all, File, Y)
The name of the DTD file to generate
Trang 4Ant: The Definitive Guide
Specifies whether to attempt using the JVM's built-in command launcher, rather than
an antRun script Defaults to true
dest is required if you specify a nested <mapper>
Content
0 n nested <arg> elements (1.3, 1.4)
Defines command-line arguments
0 n nested <env> elements (1.3, 1.4)
Specifies environment variables to pass to the command
1 n nested <fileset> elements (1.3, 1.4)
Trang 5Specifies which files and directories are passed as arguments to the command Unless the <srcfile> element is specified, files are appended to the end of the command line
0,1 nested <mapper> elements (1.3, 1.4)
When defined, compares timestamps of target files to timestamps of source files 0,1 nested <srcfile> elements (1.3, 1.4)
When present, controls where files specified by the <fileset> elements are placed on the command line The <srcfile> element does not have any attributes, and is placed between the appropriate <arg> elements
0,1 nested <targetfile> elements (1.3, 1.4)
This element is only allowed when a <mapper> element and the destdir attribute are specified It has no attributes, and is used to mark the position of target filenames on the command line It works the same as the <srcfile> element
Example Usage
Show the contents of build.xml using the type command — only if running Windows 2000:
<! Set vmlauncher="false", otherwise this fails when using
Conditionally sets a property if a resource is available at runtime The resource can be a
class, file, directory, or Java system resource If the resource is present, the property is set to
true, or whatever the optional value attribute is set to Otherwise, the property is not set
Attributes
Trang 6Ant: The Definitive Guide
130
classname (all, String, *)
A Java class name to look for, such as com.oreilly.book.Author
classpath (all, Path, N)
The classpath to use when looking up a class name or resource
classpathref (all, Reference, N)
A reference to a classpath defined elsewhere in the buildfile
file (all, File, *)
The name of a file to look for
filepath (1.4, Path, N)
The path of the file
property (all, String, Y)
The name of the property this task sets if the resource is found
resource (all, String, *)
A Java resource to look for For more information on what constitutes a resource, see the various getResource( ) methods in java.lang.ClassLoader
type (1.4, String, N)
Specifies what the file attribute represents In Ant 1.4, legal values are "file" or
"dir" If not specified, the file attribute represents either a file or directory
value (all, String, N)
The value assigned to the property if the resource is found Defaults to "true"
One of classname, file, or resource is required
Content
0,1 nested <classpath> elements (all)
Path element used in place of the classpath attribute
0,1 nested <filepath> elements (1.4)
Trang 7Path element used in place of the filepath attribute
Example Usage
The following example sets the Servlet23.present property to true if Version 2.3 or later
of the Java servlet API is available on the classpath:
Changes permissions on one or more files, just like the Unix chmod command This task
only works on Unix platforms
Attributes
defaultexcludes (all, boolean, N)
Determines whether to use default excludes, as described in Chapter 4 under "FileSet DataType." Defaults to true
dir (all, File, *)
The directory holding files whose permissions will be changed
excludes (all, String, N)
A comma-separated list of file patterns to exclude These are in addition to the default excludes
excludesfile (all, File, N)
The name of a file containing one exclude pattern per line
file (all, File, *)
The name of a file or directory to change permissions on
includes (all, String, N)
Trang 8Ant: The Definitive Guide
132
A comma-separated list of file patterns to include
includesfile (all, File, N)
The name of a file containing one include pattern per line
parallel (all, boolean, N)
If true, change permissions of all files using a single chmod command Defaults to
true
perm (all, String, Y)
The new permissions to apply, such as g+w
type (all, Enum, N)
Determines if names of plain files or directories are sent to the command Allowable values are file, dir, or both Defaults to file
Exactly one of dir or file must be specified, or at least one nested <fileset> element
0 n nested <fileset> elements (all)
Specifies which files and directories are passed as arguments to the command
Trang 9<or>
Contains any number of nested conditions, evaluating to true if any nested condition
is true Conditions are evaluated left-to-right, and evaluation stops when a condition evaluates to true.3 Does not have any attributes
This is the same behavior as Java's && operator.
3 This is the same behavior as Java's || operator.
Trang 11If true, token filtering (see the filter task) using any global buildfile filters takes place Nested filters that are specified using <filterset> are always applied, regardless of this attribute Defaults to false
flatten (all, boolean, N)
If true, the directory structure of the source files is not preserved, and all files are copied to a single destination directory You can achieve the same results using a nested <mapper> Defaults to false
includeemptydirs (all, boolean, N)
If true, empty directories are also copied Defaults to true
overwrite (all, boolean, N)
If true, files are copied even when destination files are newer Defaults to false preservelastmodified (1.3, 1.4, String, N)
If true, destination files are given the same last modified timestamp as source files Defaults to false
todir (all, File, *)
The destination directory to which files are copied
tofile (all, File, *)
The destination file, applicable only when a single file is copied using the file
0 n nested <fileset> elements (all)
Selects files to copy The todir attribute is required when <fileset>s are present 0 n nested <filterset> elements (1.4)
Defines token filters for text substitution as files are copied See the filter task for more info
0,1 nested <mapper> elements (1.3, 1.4)
Trang 12Ant: The Definitive Guide
136
Defines how filenames are transformed when copied By default, an identity transformation is performed, meaning that filenames are not modified
Example Usage
This example copies all Java source files to a new directory, replacing all occurrences of
@VERSION@ with the value of app.version
Trang 13command (all, String, N)
The name of the CVS command Defaults to checkout
cvsroot (all, String, N)
Specifies where the repository is located Equivalent to the CVSROOT environment variable
date (all, String, N)
Specifies that this command applies to files up to and including the specified date Equivalent to the -D CVS option
dest (all, File, N)
Specifies where to put checked-out files Defaults to the project base directory
error (all, File, N)
A file for logging standard error output from the CVS command Defaults to the Ant log using the MSG_WARN log level
noexec (all, boolean, N)
When true, do not do anything that modifies the filesystem Equivalent to the -n CVS option Defaults to false
output (all, File, N)
A file for logging standard output from the CVS command Defaults to the Ant log using the MSG_INFO log level
package (all, String, N)
Specifies the CVS module to retrieve
passfile (1.4, File, N)
The name of a CVS password file Defaults to ~/.cvspass
port (1.4, int, N)
The port number CVS uses to communicate with a server Defaults to 2401
quiet (all, boolean, N)
Trang 14Ant: The Definitive Guide
138
When true, CVS output is less verbose This is equivalent to the -q CVS option Defaults to false
tag (all, String, N)
Specifies a CVS tag name Equivalent to the -tag CVS option
Trang 15The password to add
Deletes one or more files and directories
This is the most dangerous task in Ant You can very easily erase your entire project with a single tag: <delete dir="."/>
Attributes
defaultexcludes (all, boolean, N)
Determines whether to use default excludes, as described in Chapter 4 under "FileSet DataType." Defaults to true
dir (all, File, *)
The directory to delete, including all its files and subdirectories Somewhat surprisingly, this attribute has nothing to do with the file attribute or nested
<fileset> Specifically, it does not specify the directory where a file given in the
file attribute is found Instead, this attribute tells the task to "brutally" delete an entire directory tree
excludes (all, String, N)
A comma-separated list of file patterns to exclude These are in addition to the default excludes
excludesfile (all, File, N)
Trang 16Ant: The Definitive Guide
140
The name of a file containing one exclude pattern per line
failonerror (1.4, boolean, N)
If true, the build process fails when this task fails Defaults to true
file (all, File, *)
The name of a file to delete
includeemptydirs (1.3, 1.4, boolean, N)
If true, directories are deleted even if they are empty Relevant only when using nested <fileset>s Defaults to false
includes (all, String, N)
A comma-separated list of file patterns to include
includesfile (all, File, N)
The name of a file containing one include pattern per line
quiet (1.3, 1.4, boolean, N)
If true, do not fail if a file or directory cannot be deleted Defaults to false
verbose (all, boolean, N)
When true, show the names of files as they are deleted Defaults to false
At least one of either dir or file is required, or a nested <fileset>
0 n nested <fileset> elements (all)
Selects files to delete Deletes only empty directories when includeemptydirs=true
Example Usage
Trang 17Here is a common target found in just about every Ant buildfile It deletes the build directory and all of its contents:
<target name="clean" description="Remove all generated code">
Attributes
None
Content
Requires at least one of either <srcfileset> or <srcfilelist>, as well as at least one
<targetfileset> or <targetfilelist> The fileset elements are used when missing files are not important When using filelists, on the other hand, any missing files cause all
target files to be removed
0 n nested <srcfileset> elements (1.4)
All files in this fileset are compared against all files specified by the
<targetfileset> and <targetfilelist> elements
0 n nested <srcfilelist> elements (1.4)
All files in this filelist are compared against all files specified by the
<targetfileset> and <targetfilelist> elements
0 n nested <targetfileset> elements (1.4)
All files in this fileset are compared against all files specified by the <srcfileset>
and <srcfilelist> elements If any are older, all are deleted
Trang 18Ant: The Definitive Guide
142
0 n nested <targetfilelist> elements (1.4)
All files in this filelist are compared against all files specified by the
<srcfileset> and <srcfilelist> elements If any are older, all are deleted
Example Usage
This example erases all class files in the build directory if the Ant buildfile or any one of the
.java files are newer than any of the class files
<dependset>
<srcfileset dir="${basedir}" includes="build.xml"/>
<srcfileset dir="${srcdir}" includes="**/*.java"/>
<targetfileset dir="${builddir}" includes="**/*.class"/>
Creates Enterprise Application Archive (EAR) files Although the jar task is also capable
of creating EAR files, the ear task simplifies the process EAR files are the deployment mechanism for J2EE applications, and are little more than JARs consisting of well-defined directories and files
Attributes
appxml (1.4, File, Y)
Specifies the location of the deployment descriptor, which is always renamed to
META-INF/application.xml in the generated EAR file The source file does not have
Trang 19The behavior used when no files match Legal values are fail (abort the build), skip
(don't create the EAR file), or create Defaults to create, meaning create an empty EAR file when no files are present
Trang 20Ant: The Definitive Guide
0,1 nested <metainf> elements (1.4)
Defines a fileset containing all files placed in the META-INF directory of the EAR file If a file named MANIFEST.MF is found, it is ignored and a warning is issued
0 n nested <fileset> elements (1.4)
Specifies the files and directories to include in the EAR file
0 n nested <zipfileset> elements (1.4)
See the documentation for the zip task for more information
Trang 21Attributes
append (all, boolean, N)
If true, append to an existing file Defaults to false
file (all, File, N)
The file to write the message to
message (all, String, *)
The text to write
The message attribute is required unless text is included as content of the XML tag, as shown
in the example that follows
Content
Text content (all)
Text content is allowed when the message attribute is not specified Property references such as ${builddir} are allowed
Example Usage
The first of the following examples specifies the text to write using the message attribute The second example specifies the text to write by enclosing it within <echo> </echo> tags
<echo message="Building to ${builddir}"/>
<echo>You are using version ${java.version}
of Java! This message spans two lines.</echo>
org.apache.tools.ant.taskdefs.ExecTask
Executes a system command Like the apply task, this provides a way to access native functionality outside of the Java and Ant build environment
The apply task requires a nested <fileset>, specifying a list of files and directories passed
as arguments to the system command The exec task differs in that it does not allow this nested <fileset>
Attributes
command (1.1, CommandLine, *)
The command to execute, including arguments Deprecated as of Ant 1.2