Title: ANT
1ANT Another Neat Tool
- Representation and Management of Data on the
Internet
2What is ANT?
- A cross-platform build tool (like make)
- A scripting framework
- Based on industry standards (Java and XML)
- Open Source (development coordinated by the
Apache Jakarta project)
3What can we do with ANT?
- Can be used to
- compile java programs
- create javadoc documentation
- create jar, zip, tar, war files
- delete and copy files
- validate XML files
- etc. (send mail, anything you want)
4ANT Buildfiles
- An ANT buildfile is a file that contains the
instructions for ANTs tasks - A buildfile is written in XML
- A buildfile has the following elements
- Project - a top level collection of targets
- Property - an ANT variable
- Target - a collection of tasks
- Task - a unit of ANT execution (a step)
5ANT Buildfiles
project
. . .
. . .
property
property
Target
Target
. . .
Task
Task
6Buildfile Example
ltproject name"MyProject" default"dist"
basedir"."gt ltproperty name"src" value."/gt
ltproperty name"build" location"build"/gt
lttarget name"compile description"compile the
source"gt lt!-- Compile the java code from
src into build --gt ltjavac
srcdir"src" destdir"build"/gt lt/targetgt
lttarget namejar" descriptionbuild the JAR
file"gt ltjar destfile"dist/lib/app.jar"base
dir"build/classes"/gt lt/targetgt lttarget
name"clean" description"clean up"gt lt!--
Delete the build and dist directory trees
--gt ltdelete dir"build"/gtltdelete
dir"dist"/gt lt/targetgt lt/projectgt
7Projects
- The project is the root element of the buildfile
- One project per buildfile
- Projects can have 3 attributes
- name name of project (optional)
- default default target to use (required)
- basedir base directory for paths (optional)
8Properties
- Properties (global values) are typically defined
as follows - ltproperty namepropName valuepropVal/gt
- Note Properties are XML elements without
contents, therefore we use /gt - A property named propName can be referred to
later using the syntax propName - You can define any properties you want
- Properties are not variable the first value they
are given remains for the whole build!
9Special Property Definitions
- ltproperty name"src" location"srcFile"/gt
- sets src to the absolute file name of srcFile,
taken to be relative to the project's basedir - ltproperty file"pFile"/gt
- reads a set of properties from a file called
pFile - ltproperty environment"env"/gt
- Later, you may use env.VAR_NAME to get the
systems environment variable called VAR_NAME - Built-in Properties (predefined)
- ant.file, ant.java.version, os.name, user.name,
user.home, etc
10Tasks
- A task is a piece of code to be executed
- The general form of a task element is
- lttaskname param1"value1" param2"value2"... /gt
- ANT comes with some built-in tasks, which cover
most of the basic needs for development of
applications - One can also define new tasks (not covered here)
11Some Built-In Tasks Examples
- Directory tasks
- ltmkdir dir"workDir"/gt, ltdelete
dir"workDir"/gt - Archive tasks
- ltjar jarfile"archive.jar" basedir"build.classe
s"/gt - lttar tarfile"archive.tar" basedir"src"/gt
- ltgzip zipfile"archive.tar.gz" src"archive.tar"/gt
- Output tasks
- ltecho message"Hello, user.name"/gt
12More Built-In Tasks Examples
- Java tasks
- ltjava classname"Ex4Main"/gt
- ltjava classname"Main"gtltarg value"-h"/gt lt/javagt
- ltjavac srcdir"src" destdir"build"/gt
- compiles only files that need to be compiled
(time based) - Invoking external programs
- ltexec executable"emacs"gt
- ltarg value"readme.txt"/gt
- lt/execgt
13Targets
- A target is a collection of tasks to be performed
when the target is executed - Targets have the attributes
- name name of the target (required)
- depends comma separated list of targets on which
the target depends (optional) - if, unless, description details omitted (read
about it in the ANT documentation)
14A BuildFile Adding a Target
- ltproject nameMyProject defaultcompilegt
- ltproperty name"buildDir" value"build"/gt
- ltproperty namesrcDir" value."/gt
- lttarget name"compile"gt
- ltjavac srcdir"srcDir" destdir"buildDir"/
gt lt/targetgt - lt/projectgt
A Task
We could also have written ltjavac srcdir.
destdirbuild"/gt
15- ltproject name"MyProject" default"dist"
basedir"."gt - lt!-- set global properties for this build --gt
- ltproperty name"src" value"."/gt
- ltproperty name"build" value"build"/gt
- ltproperty name"dist" value"dist"/gt
- lttarget name"compile"gt
- lt!-- Compile java code from src into
build --gt - ltjavac srcdir"src" destdir"build"/gt
- lt/targetgt
16-
- lttarget name"dist" depends"compile"gt
- lt!-- Create the distribution directory --gt
- ltmkdir dir"dist/lib"/gt
- lt!-- Put everything in build into the jar
file - MyProject.jar file --gt
- ltjar jarfile"dist/lib/MyProject.jar"
basedir"build"/gt - lt/targetgt
- lttarget name"clean"gt
- lt!-- Delete the build and dist directory
trees --gt - ltdelete dir"build"/gt
- ltdelete dir"dist"/gt
- lt/targetgt
- lt/projectgt
17More about Depends
- ANT tries to execute the targets in depends
from left to right - However, a target may be executed earlier when
another one depends on it
18Example 1
lttarget name"A"/gt lttarget name"B"
depends"A"/gt lttarget name"C" depends"B"/gt
lttarget name"D" depends"C,B,A"/gt
- Execute ant D
- In what order will the tasks be performed?
Try D
Try C
Try B
Try A
- Note B is executed before C!
- Note B is executed once!
Do D
Do C
Do B
Do A
19Example 2
lttarget name"A dependsB/gt lttarget name"B"
depends"A"/gt
- Execute ant A
- In what order will the tasks be performed?
- The build fails, ant reacts with
- Circular dependency A lt- B lt- A
20Running ANT
- Type ant
- ANT looks for the file build.xml, and performs
the default task specified there - Use the buildfile option to specify a different
buildfile - You can specify a different target to be
performed - You can define parameters using the D option
21Examples
- Run ANT using build.xml on the default target
- ant
- Run ANT using the test.xml file on the default
target - ant -buildfile test.xml
- Run ANT using the test.xml file on a target
called dist - ant -buildfile test.xml dist
- Run ANT using the test.xml file on a target
called dist, setting the build property to the
value build/classes
- ant -buildfile test.xml -Dbuildbuild/classes
dist
22Make versus ANT
- Make OS dependent -tasks are shell commands
- Runs fast for small tasks
- ANT OS independent -tasks implemented as Java
classes - requires Java ( JDK 1.2)
- Slow for small tasks requires JVM
- Make Non-standard syntax (infamous tabbing
problem) - ANT XML based syntax
- Make state dependencies between program files
- ANT state dependencies between tasks (not
between program files)
23References
- To learn more about ANT
- Look at the documentation on the web (reference
from the table of lecture schedule) - Pay attention to the section Built-in Tasks
there are many tasks and task parameters you
might find useful