0% found this document useful (0 votes)
55 views21 pages

System Integration: ANT - Writing A Simple Build File

This document discusses how to write build files for Apache Ant. It explains that Ant build files are written in XML and contain projects with targets made up of tasks. It provides examples of how to define properties, dependencies between targets, conditional execution, and common tasks and structures used in Ant build files like filesets, paths, and references.

Uploaded by

Brana Šarac
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views21 pages

System Integration: ANT - Writing A Simple Build File

This document discusses how to write build files for Apache Ant. It explains that Ant build files are written in XML and contain projects with targets made up of tasks. It provides examples of how to define properties, dependencies between targets, conditional execution, and common tasks and structures used in Ant build files like filesets, paths, and references.

Uploaded by

Brana Šarac
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

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.

Module SE-C-05 System Integration

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"/>

Module SE-C-05 System Integration

Conditional Execution of Targets g


A target also has the ability to perform its execution if (or unless) a property has been set.
<target name="build-module-A" if="module-Ap present"/> <target name="build-own-fake-module-A" unless="module-A-present"/>

Module SE-C-05 System Integration

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.

Module SE-C-05 System Integration

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

Module SE-C-05 System Integration

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

Module SE-C-05 System Integration

Example Buildfile ( ) p (1)


<project name="MyProject" default="dist" basedir="."> <description> simple example build file </description>
<!-- set global properties for this build -->

<property name="src" location="src"/> <property name="build" location="build"/> property name build location build / <property name="dist" location="dist"/>

Module SE-C-05 System Integration

Example Buildfile ( ) p (2)


<target name="init"> <!-- Create the time stamp --> p <tstamp/> <mkdir dir="${build}"/> </target> <target name="compile" depends="init" description="compile the source " > <javac srcdir="${src}" destdir="${build}"/> srcdir ${src} destdir ${build} /> </target>

Module SE-C-05 System Integration

10

Example Buildfile ( ) p (3)


<target name="dist" depends="compile" p g description="generate the distribution" >
<!-- Create the distribution directory -->

<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-20061213.jar file -->

<jar jarfile="${dist}/lib/MyProjectja ja e ${d st}/ b/ y oject 20061213.jar" basedir="${build}"/> </target>

Module SE-C-05 System Integration

11

Example Buildfile ( ) p (4)


<target name "clean" name="clean" description="clean up" > <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> dir ${build} /> <delete dir="${dist}"/> </target> </project>

Module SE-C-05 System Integration

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

Path-like Structures (2) ( )


As a shortcut, the <classpath> tag supports path and location attributes of its own, so:
<classpath> <pathelement path="${classpath}"/> </classpath>

can be abbreviated to:


<classpath path ${classpath} /> path="${classpath}"/>

Module SE-C-05 System Integration

14

Specifying the Resource Collections p y g


<classpath> <pathelement path="${classpath}"/> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> </fil t> <pathelement location="classes"/> <dirset dir="build"> <include name="apps/**/classes"/> <exclude name="apps/**/*Test*"/> </dirset> <filelist refid="third-party_jars"/> </classpath>
Module SE-C-05 System Integration

15

Referencing the Path-like Structures g


<path id="base.path"> <pathelement path="${classpath}"/> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> </fil t> <pathelement location="classes"/> /p </path> <path id="tests.path"> <path refid="base.path"/> <pathelement location="testclasses"/> </path>
Module SE-C-05 System Integration

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".

Module SE-C-05 System Integration

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.

Module SE-C-05 System Integration

18

References Example ( ) p (1)


Starting example:
<target > > <javac > p <classpath> <pathelement location="lib/"/> <pathelement path="/Users/antoine/dev/asf/ant > <pathelement path="${additional.path}"/> </classpath> </javac> </target>
Module SE-C-05 System Integration

19

References Example ( ) p (2)


Can be rewritten as:
<path id="project.class.path"> id project.class.path > <pathelement location="lib/"/> <pathelement path="/Users/antoine/dev/asf/ /> <pathelement path="${additional.path}"/> th l t th "${ dditi l th}"/ </path> <target ... > <javac ...> <classpath refid="project.class.path"/> refid project.class.path /> </javac> </target>

Module SE-C-05 System Integration

20

Use of External Tasks


Ant supports a plugin mechanism for using third party tasks For using them tasks. you have to do two steps: 1. 1 place their implementation somewhere where Ant can find them 2. 2 declare them Examples: them.
<taskdef name="for" classname="net.sf.antcontrib.logic.For" /> <for ... / /> <taskdef resource="net/sf/antcontrib/antcontrib.propertie s" /> <for ... />
Module SE-C-05 System Integration

21

You might also like