ANT Tutorial

Contact: BuildMaster


What is ANT?

ANT is a tool to help automate the building of your projects. It is similar to Make-like tools without their shell-based constraints and without their annoying formatting rules such as strict tab usage. Unlike Make-like tools that are shell-based, ANT is independent of a specific Operating System and thus can be used on multiple platforms.


How to Download/Install

Follow the instructions under "Download" here: http://ant.apache.org/ to download and install ANT.


How to Write an ANT Buildfile

ANT uses XML format for its buildfiles. Every buildfile must have a project and at least one target (a set of tasks you want to be executed).


> Project

Each project defines one or more targets. A target is a set of tasks you want to be executed. When no target is given, the project's default is used.
A project has 3 attributes:

AttributeDescription Required
name the name of the project. No
default the default target to use when no target is supplied. No; however, since Ant 1.6.0, every project includes an implicit target that contains any and all top-level tasks and/or types. This target will always be executed as part of the project's initialization, even when Ant is run with the -projecthelp option.
basedir the base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. When this is done, it must be omitted in the project tag. If neither the attribute nor the property have been set, the parent directory of the buildfile will be used. No



> Target

A target is a set of tasks you want to execute. It can have dependencies and flow control.

Attribute Description Required
name the name of the target. Yes
depends a comma-separated list of names of targets on which this target depends. No
if the name of the property that must be set in order for this target to execute. No
unless the name of the property that must not be set in order for this target to execute. No
description a short description of this target's function. No


Targets can depend on other targets. For example, the creation of a jar file distributable depends on the project being compiled. Targets are executed only once and when they are needed.

<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>

Here is an example of using a target's flow control to determine which tasks get executed:

<target name="build-module-A" if="module-A-present"/>

<target name="build-own-fake-module-A" unless="module-A-present"/>



> Task

A task is a piece of code that can be executed.

You may use built-in tasks, optional tasks, or write your own task.



> Example ANT Buildfile

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

How to Run ANT

ANT is run from the Command Line. Examples of Command Line Usage and Options are listed below:


> Example ANT Command Line Usage:

runs Ant using the build.xml file in the current directory, on the default target.

    ant

runs Ant using the test.xml file in the current directory, on the default target.

    ant -buildfile test.xml

runs Ant using the test.xml file in the current directory, on the target called dist.

    ant -buildfile test.xml dist

runs Ant using the test.xml file in the current directory, on the target called dist, setting the build property to the value build/classes.

    ant -buildfile test.xml -Dbuild=build/classes dist

runs Ant picking up additional task and support jars from the /home/ant/extras location

    ant -lib /home/ant/extras



> ANT Command Line Options:

ant [options] [target [target2 [target3] ...]]
Options:
  -help, -h              print this message
  -projecthelp, -p       print project help information
  -version               print the version information and exit
  -diagnostics           print information that might be helpful to
                         diagnose or report problems.
  -quiet, -q             be extra quiet
  -verbose, -v           be extra verbose
  -debug, -d             print debugging information
  -emacs, -e             produce logging information without adornments
  -lib <path>            specifies a path to search for jars and classes
  -logfile <file>        use given file for log
    -l     <file>                ''
  -logger <classname>    the class which is to perform logging
  -listener <classname>  add an instance of class as a project listener
  -noinput               do not allow interactive input
  -buildfile <file>      use given buildfile
    -file    <file>              ''
    -f       <file>              ''
  -D<property>=<value>   use value for given property
  -keep-going, -k        execute all targets that do not depend
                         on failed target(s)
  -propertyfile <name>   load all properties from file with -D
                         properties taking precedence
  -inputhandler <class>  the class which will handle input requests
  -find <file>           (s)earch for buildfile towards the root of
    -s  <file>           the filesystem and use it
  -nice  number          A niceness value for the main thread:
                         1 (lowest) to 10 (highest); 5 is the default
  -nouserlib             Run ant without using the jar files from ${user.home}/.ant/lib
  -noclasspath           Run ant without using CLASSPATH
  -autoproxy             Java 1.5+ : use the OS proxies
  -main <class>          override Ant's normal entry point



References

Information was used from
1) http://ant.apache.org/manual/index.html
2) http://ant.apache.org/


Further Information

For more information about ANT or its advanced functionality please refer to http://ant.apache.org/manual/index.html