JavaScript and Ant

By on September 25, 2001 12:36 am

—last Modified on 25 Aug 2004

This tutorial is a collection of information about using JavaScript and XSLT with Ant, and is focused towards intermediate to expert client-side web developers. It is presently a very rough draft.

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.

The most up to date version of this document is located at

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

Why JavaScript in Ant?

Ant does not have standard commands for programming techniques like recursion. Instead, it contains prebuilt tasks for using your language of choice. JavaScript is an easy to use, widely known scripting language, and is easily added to Ant through its optional tasks.

Adding JavaScript to 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



Hello World!


To include JavaScript within Ant, you use a script tag much like you do in any xml document. To define the Script language, use the older language declaration rather than the newer type syntax, . You may also use a src attribute to include an external script.

Ant Manual Script Task on 11 Feb 2002

General Objects

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] 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: