System Integration: ANT - Writing A Simple Build File
System Integration: ANT - Writing A Simple Build File
Module SE-C-05
Supported by:
Joint MSc curriculum in software engineering European Union TEMPUS Project CD_JEP-18035-2003
Version: April 28, 2006
Projects (1) j ( )
Ant's buildfiles are written in XML. Each buildfile contains one project and at least one (default) target. Targets contain task elements elements. A project has three attributes:
name the name of the project default the default target to use when no target is supplied basedir the base directory from which all path calculations are done
Module SE-C-05 System Integration
Projects (2) j ( )
Optionally, a description for the project can be provided as a top-level <description> element Each project defines one or more targets. A target is a set of tasks to be executed. executed When starting Ant, the user Ant can select which target(s) he wants to have executed. When no target is given, the project's default is used.
Targets - dependencies g p
A target can depend on other targets. Ant tries to execute the targets in the t ies e ec te ta gets depends attribute in the order they appear (from left to right) right). Example:
<target <target <target <t t <target name="A"/> name="B" depends="A"/> name="C" d "C" depends="B"/> d "B"/> name="D" depends="C,B,A"/>
Target Attributes g
A target has the following attributes:
name the name of the target (required) depends - a comma-separated list of names of targets on which this target depends. if - the name of the property that must be set in order for this target to execute. g unless - the name of the property that must not be set in order for this target to execute description - a short description of the target's function.
Tasks
A task is a piece of code that can be executed. executed T k structure Task t t
<name attribute1="value1" attribute2="value2" ... />
There is a set of built-in tasks, along with a number of optional tasks, but it is also very easy to write your own
Properties p
A project can have a set of properties. These might be set in the buildfile by the property task, or might be set outside Ant Properties may be used in the value of task attributes. This is done by placing the property name between "${" and "}" ${ } in the attribute value
<property name="src" location="src"/> <property name="build" location="build"/> property name build location build / <property name="dist" location="dist"/>
10
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-20061213.jar file -->
11
12
Path-like Structures(1) ( )
Wherever path-like values need to be specified, a nested element can be used. This takes the general form of:
<classpath> <pathelement path="${classpath}"/> <pathelement location="lib/helper.jar"/> </classpath>
The location attribute specifies a single file or directory relative to the project's project s base directory (or an absolute filename), while the path attribute accepts colonor semicolon-separated lists of locations.
Module SE-C-05 System Integration
13
14
15
16
Command-line Arguments g
Several tasks take arguments that will be passed to another process on the command line. To make it easier to specify arguments that contain space characters, nested arg elements can b used. l t be d For example:
<arg value="-l -a"/>
is a single command-line argument containing a space character not separate commands "-l" character, l and "-a".
17
References
Any project element can be assigned an identifier using its id attribute In most attribute. cases the element can subsequently be referenced by specifying the refid attribute on an element of the same type. This can be useful if you are going yp y g g to replicate the same snippet of XML over and over.
18
19
20
21