—last Modified on 25 Aug 2004
Information in this tutorial was taken from several sources including the ant-user mailing list and the ant manual. Where possible the date and location of a source material has been noted to provide credit to members of the community and to aid in your search for more information. If there is any material in this tutorial which is not public or is copyrighted please contact the authors of this tutorial and this information will be removed.
All e-mail addresses in this document have been munged in an attempt to block spam spiders. Replace [at] with the @ symbol to un-munge the address.
Warning and Disclaimer: No guarantee exists that this tutorial is correct, up to date, or even harmless.
This tutorial is copyrighted and is provided as a resource for the open source community. In that spirit it is freely available to be copied, mirrored, modified, and taken for your own use provided that full credit is given to the authors and Ant community in all redistributions and derivative works.
—Dylan Schiemann on 14 Feb 2002
What is Ant?
Apache Ant is a build based tool. It has a vast array of uses for programmers. Often overlooked is its usefulness for client-side web developers. It is especially useful for preprocessing tokens across many pages of a web site, rather than relying on SSI, php, asp, or jsp. It is also very useful for building a site with an xslt processor such as xalan. If you are not familiar with Ant, be sure to read the ant manual which has instructions for installing and using Ant.
Download the optional task jar from the ant web site, the bsf.jar, and rhino.jar from mozilla (rename js.jar to rhino.jar). Include these jars in your classpath. Note that some of the examples described here do not work effectively with older versions of Ant. There are also issues with mixing older and newer versions of BSF and Ant.
Adding XSLT to Ant
To include an xslt processor such as Xalan-J-2, you will also need to download and configure it. You will also need to add the location of your xalan jars to your ant classpath, or make a copy of the necessary jars.
Hello World Example
The obligatory hello world example simply echos Hello World to Ant’s generated results
generates: setup: main: Hello World! BUILD SUCCESSFUL
—Ant Manual Script Task on 11 Feb 2002
To do more interesting things with Ant, you have access to properties, targets, and references. For an explanation of properties, targets, and references, refer to the ant manual
project.getProperty(string propertyName); project.getUserProperty(string propertyName); project.getTarget(string targetId); project.getReference(string refereceId); project is a shortcut for getProject(), which returns the project object. self returns the script task itself for logging and other task specific utilities.
If you have two things with the same name/id, only the last one in the document order is used. Illegal Java names cause it to be omitted.
—Erik Hatcher (jakarta-ant[at]ehatchersolutions.com) on 12 Feb 2002
To use the standard XSLT task, with a single input and output file, and to pass a parameter to the stylesheet, you could do the following in your build file:
To process an entire directory of documents, you could do the following:
---Ant Manual Style Task on 11 Feb 2002
---Dylan Schiemann on 14 Feb 2002
*** I am presently unable to get this to work as it seems that property files only work with tasks that uses tokens ***
Looping through tasks
---based on example by Diane Holt on 25 Sep 2001