Introduction to Ant

Ant Logo

* Change the marked fields if you are presenting these slides yourself.

Joe Schmetzer**

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike2.5 License.


Agenda - What is Ant?

Ant Overview


What Can Ant Do? (Part 1 of 2)

What Can Ant Do? (Part 2 of 2)


Yeah, But My IDE Already Does That!

Build Automation


  • Automated build procedures are a best practice in software engineering, because they are accurate, consistent and repeatable.
  • Avoiding manual procedures means that there is no risk that something will be missed out or done incorrectly.
  • Build automation reduces the need for documentation, and allows new members of a team immediately be able to generate a build or release without getting into the minutae of build procedures.
  • An automated build should run at the fastest possible speed, freeing up time for more productive activities.
  • CruiseControl ( is a build scheduler that will poll version control and trigger an automated build as soon as a change is checked in.

Agenda - Ant Terminology and Concepts



Target Example

Example Targets with Dependencies
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="A"/>
<target name="D" depends="B,C"/>




Setting Properties - Precedence

Setting Properties - Examples

Built-in Properties (Part 1 of 3)

Built-in Properties (Part 2 of 3)

Built-in Properties (Part 3 of 3)


  <pathelement path="${classpath}"/>
  <fileset dir="lib">
    <include name="**/*.jar"/>
  <pathelement location="classes"/>
  <dirset dir="${build.dir}">
    <include name="apps/**/classes"/>
    <exclude name="apps/**/*Test*"/>
  <filelist refid="third-party_jars"/>

Agenda - Installing and Executing Ant

Ant Environments

Setup Ant

Ant Project Help

%ant -projecthelp
Buildfile: build.xml
        simple example build file
Main targets:
 clean    clean up
 compile  compile the source
 dist     generate the distribution
Default target: dist

Ant Command Line

Agenda - Core Ant Tasks

File Tasks

  • <copy>
  • <concat>
  • <delete>
  • <filter>
  • <fixcrlf>
  • <get>

Compile Tasks

Compiles the specified source file(s) within the running (Ant) VM, or in another VM if the fork attribute is specified.
Runs the annotation processor tool (apt), and then optionally compiles the original code, and any generated source code.
Runs the rmic compiler

Archive Tasks

<zip>, <unzip>
Creates a zipfile.
<jar>, <unjar>
Jars a set of files.
<war>, <unwar>
An extension of the Jar task with special treatment web archive dirs
An extension of the Jar task with special treatment enterprise archive dirs.

Testing Tasks

Runs tests from the JUnit testing framework.
Merges the individual XML files generated by the Junit task and applies a stylesheet on the resulting merged document to provide a browsable report of the testcases results.

Property Tasks

Sets a property to the value excluding the last path element.
Loads a file into a property.
Creates or modifies property files.
Sets a property if a given target file is newer than a set of source files.

Miscellaneous Tasks

Echoes text to System.out or to a file.
Generates code documentation using the javadoc tool.
Executes a series of SQL statements via JDBC to a database. Statements can either be read in from a text file using the src attribute, or from between the enclosing SQL tags.

Agenda - A Working Build Script

Example Scenario

init target

<project name="MyProject" default="dist" basedir=".">
        simple example build file
  <!-- 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 -->
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>

compile target

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>

dist 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/${}-${DSTAMP}.jar" basedir="${build}"/>

clean target

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>

Output of the Script

Buildfile: build.xml

    [mkdir] Created dir: C:\antexample\build

    [javac] Compiling 1 source file to C:\antexample\build

    [mkdir] Created dir: C:\antexample\dist\lib
      [jar] Building jar: C:\antexample\dist\lib\MyProject-20050710.jar

Total time: 5 seconds

C:\antexample>java -cp dist\lib\MyProject-20050711.jar HelloWorld
Hello, world

Agenda - Ant Techniques and Patterns

Use Simple Targets

Use Standard Targets

Use Properties for Configurability

The End

Asking a question