ANT – Another Neat Tool Representation and Management of Data on the Internet
What 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)
What 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)
ANT Buildfiles An ANT buildfile is a file that contains the instructions for ANT’s 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)
ANT Buildfiles property Target Task... project Target
Buildfile Example
Projects 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)
Properties Properties (global values) are typically defined as follows: Note: Properties are XML elements without contents, therefore we use /> 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!
Special Property Definitions -sets src to the absolute file name of srcFile, taken to be relative to the project's basedir -reads a set of properties from a file called pFile -Later, you may use ${env.VAR_NAME}$ to get the system ’ s environment variable called VAR_NAME Built-in Properties (predefined): -ant.file, ant.java.version, os.name, user.name, user.home, etc …
Tasks A task is a piece of code to be executed The general form of a task element is 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)
Some Built-In Tasks Examples Directory tasks:, Archive tasks: Output tasks:
More Built-In Tasks Examples Java tasks: -compiles only files that need to be compiled (time based) Invoking external programs:
Targets 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)
A BuildFile – Adding a Target We could also have written: A Task
<!-- Put everything in ${build} into the jar file: MyProject.jar file -->
More 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
Example 1 Execute: ant D In what order will the tasks be performed? Try DTry CTry BTry A Do DDo CDo BDo A Note: B is executed before C ! Note: B is executed once!
Example 2 Execute: ant A In what order will the tasks be performed? The build fails, ant reacts with: Circular dependency: A <- B <- A
Running 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
Examples 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 -Dbuild=build/classes dist
Make 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)
References 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