SE452: Notes on Ant

Contents [0/8]

Ant: What is it? [1/8]
Ant: Download [2/8]
Ant: Setup [3/8]
Ant: Environment Settings [4/8]
Ant: Running Ant [5/8]
Ant: The development directory [6/8]
Ant: The build file - intro to XML [7/8]
Ant: A concrete example [8/8]

Ant: What is it? [1/8]

Ant

Ant stands for Another Neat Tool

Basically, Ant is a Java based build tool. It uses Java to build Java code for you. It also happens to have a number of interesting features that make life for the Java developer a lot easier.

Ant has been described as make without make's wrinkles.

Ant was written originally to build Tomcat, and the code was quickly re-used by other projects, and Ant was turned into its own project. Ant is now a top level apache project, it is not under the jakarta group of projects.

Ant: Download [2/8]

Download it from here. You should get the .zip version if you are on Windows, get the tar.gz if you are on Unix. For now, just get apache-ant-1.5.4-bin[.zip or .tar.gz].

On Windows, unzip it to a directory using your favorite zip tool. With WinZip, you just double click on the file, click the extract button, and choose a good directory to unzip it to. I recommend something close to the root of your drive, like D:\ or C:\ or C:\se452. Once extracted, it will be in a directory called apache-ant-1.5.4. We will call this directory ANT_HOME

On Unix, extract the ant tarball to a directory where you have permissions to keep applications. On linux, this might be /usr/local, on HP it might be /apps or /opt. If you don't have root access, you can install Ant anywhere.

With Gnu tar, you can use the command tar -xvzf apache-ant-1.5.4-bin.tar.gz. With the standard tar, use something like gzip -c apache-ant-1.5.4-bin.tar.gz | tar xf - from the directory where you want the ANT_HOME to be.

With either download, you can check the PGP signature of the file using pgp or gpg.

Ant: Setup [3/8]

Ant is a command line tool, although it has been integrated with numerous IDEs. Since we are using the command line, it will fit in well with our development environment.

Once Ant has been extracted, you will need to setup your environment to run from the command line properly. This will involve setting the PATH and ANT_HOME environment variables.

From the command line:

On Windows:

        set ANT_HOME=C:\apache-ant-1.5.4
        set PATH=%PATH%;%ANT_HOME%\bin
        
On Unix (bash):
        export ANT_HOME=/usr/local/apache-ant-1.5.4
        export PATH=$PATH:$ANT_HOME/bin
        

Ant: Environment Settings [4/8]

To make this the default on your machine:

On Windows (NT-ish systems):

  1. Go to Start->Settings->Control Panel->System
  2. Click on Environment
  3. In the Variable text box, add ANT_HOME
  4. In the Value text box, add the path to your Ant install (i.e. C:\apache-ant-1.5.4)
  5. Click on the PATH environment variable.
  6. Edit it to be <previous settings>;%ANT_HOME%\bin
  7. Click Apply/OK

On Windows (2000/NT/XP-ish systems)

  1. Go to Start->Settings->Control Panel->System
  2. Click on Advanced->Environment Variables
  3. In the User variables section, click Add and add ANT_HOME in the Variable Name box.
  4. For the Variable value text box, add the path to your Ant install (i.e. C:\apache-ant-1.5.4)
  5. Click on the PATH environment variable (in either system or user variables) and click Edit.
  6. Edit it to be <previous settings>;%ANT_HOME%\bin
  7. Click Apply/OK

On Windows 9x:

Add an entry in your autoexec.bat for ANT_HOME and PATH that is identical to the settings above from the command line.

On Unix:

Add an entry in your .profile for ANT_HOME and PATH that is identical to the settings above from the command line.

Ant: Running Ant [5/8]

OK, enough setup. Now to run Ant. You should be able to go to a random directory in a command window and type ant and see something like the following on your screen:

Buildfile: build.xml does not exist!
Build failed

This is a good thing. This means ant is now working. Now we will look at the setup of your development directory and the build file to compile your code.

Ant: The development directory [6/8]

So far, you have been developing code in a directory, running java and javac by hand. Perhaps you have written .bat or .sh scripts to run these commands repetitively. You also have probably set your system classpath at the global level for this class.

By setting up Ant properly, we'll eliminate the need for this.

Create the following directory structure on your computer where you will work on your homework assignments

base directory (like c:/se452 or /home/username/se452
|
+------>build.xml
        /src
        +-------->/html
                  /java
                  /sql
                  /etc., etc.
	/lib

The /src directory will contain all of your source files in their full package directories

The /lib directory will contain any third party .jar files that are supplied by me or others

Other directories will be created as we go through the quarter and by the build process itself.

Ant: The build file - intro to XML [7/8]

The build file is written in XML. This means that it has to conform to XML rules, as well as be properly configured for what Ant expects.

Some XML basics.

The Ant build file structure.

project
  property
  property
  target
    task
    task
  target
    task
    task

Properties are settings that you use throughout your build file. They are set, and then dereferenced in the build file by using the following syntax: ${property}

A build file consists of multiple targets. Targets can be dependent on one another so they are always run in the correct order. For example, you would always need to run an init target that makes directories where your javadoc will go.

A target consists of multiple tasks. Ant contains built-in tasks for some of the following:

It also has optional tasks for things such as:

Ant: A concrete example [8/8]

An example build file

<project name="MyProject" default="dist" basedir="."> <description> simple example build file </description> <!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target> <target name="compile" depends="init" description="compile the source " > <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile" description="generate the distribution" > <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/> </target> <target name="clean" description="clean up" > <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project>


Revised: 2003/09/28