Recipe 4.1 Obtaining and Setting Up Ant


You want to download and set up Apache Ant on your computer.


Point your browser to, download the binary or source distribution of Ant, then follow the instructions given in this recipe and on the Ant support site.


The binary distribution of Apache Ant can be downloaded from You can also download the source distribution, which contains the Java source files for Ant, from You must have the Java Software Development Kit (SDK) installed.

Ant v1.5.3 will be the last release that supports JDK 1.1. Ant v1.5.1 can run with JDK 1.1, although some tasks work only on JDK 1.2.

To use Ant, you must have a Java API for XML Processing (JAXP)-compliant XML parser available on your classpath. The binary Ant distribution includes the Apache Xerces2 XML parser. If you opt for a different JAXP-compliant parser, you should remove xercesImpl.jar and xmlParserAPIs.jar from Ant's top-level /lib directory (as in jakarta-ant-1.5.1/lib ) and put the JAR file(s) for the alternative parser into Ant's /lib directory. You can also add them directly to your user classpath.

The user classpath is the classpath represented by the CLASSPATH environment variable on your machine. This classpath overrides the default value for the user classpath (., or the current directory). The java command-line tool's -cp or -classpath switches override the CLASSPATH environment variable. The user classpath can also be set by a JAR file specified by the java tool's -jar switch. This designation in turn overrides the other ways of specifying a classpath. The bottom line is that it is easier to place your parser of choice in the jakarta-ant-1.5.1/lib directory instead of fooling around with these classpath issues.

The complete installation directions for Ant and links to related Web pages are at

Take the following steps to get Ant running on your machine:

  1. Unpack the compressed file (in ZIP or TAR format) containing the Ant tool. With Ant v1.5.1, unpacking the distribution file creates a directory called jakarta-ant-1.5.1 .

  2. Set the ANT_HOME environment variable to the directory where you installed Ant. On Unix, this can be accomplished by typing a command-line phrase:

     export ANT_HOME=/usr/local/jakarta-ant-1.5.1 

    On Windows type:

     set ANT_HOME=h:\jakarta-ant-1.5.1 
  3. Add the <Ant-installation-directory>/bin directory to your PATH environment variable. This allows the developer to change to any working directory with a build.xml file and type ant to run this file (read the next recipe for a description of executing a build.xml file). The <Ant-installation-directory>/bin directory contains the scripts which launch the Java classes that form the basis of Ant.

  4. Optionally, set the JAVA_HOME environment variable to the directory where your JDK is installed. You might as well set the JAVA_HOME environment variable, because the scripts that are provided with Ant in its /bin directory can then automatically add the required JDK-related classes when you want to use the javac or rmic tasks. Tasks are XML elements that do certain jobs in Ant files, such as war (to create Web Archive files) and javac (to compile Java classes with Ant).

  5. Test your installation by typing ant -version . If everything goes well, this command produces a return value like this:

     K:\>ant -version Apache Ant version 1.5.1 compiled on October 2 2002 

See Also

Recipe 4.2 on using Ant targets; Recipe 4.3 on including Tomcat JAR files in the Ant classpath; Recipe 4.4 on compiling a servlet with Ant; Recipe 4.5 on creating a WAR file with Ant; Recipe 4.6 on using Ant to create JAR files; Recipe 4.7 and Recipe 4.8 on starting and stopping Tomcat with Ant; Recipe 2.1 and Recipe 2.6 on deploying web applications using Ant; the Apache Ant manual:; the Apache Ant Project:

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: