Introduction to Ant

Ant Logo

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

Joe Schmetzer*

http://www.exubero.com/*

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

Agenda

Agenda - What is Ant?

Ant Overview

Resources

What Can Ant Do? (Part 1 of 2)

What Can Ant Do? (Part 2 of 2)

Resources

Yeah, But My IDE Already Does That!

Build Automation

Notes

  • 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 (http://cruisecontrol.sourceforge.net/) 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

Project

Target

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

Tasks

Resources

Properties

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)

Paths

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

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

<javac>
Compiles the specified source file(s) within the running (Ant) VM, or in another VM if the fork attribute is specified.
<apt>
Runs the annotation processor tool (apt), and then optionally compiles the original code, and any generated source code.
<rmic>
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
<ear>
An extension of the Jar task with special treatment enterprise archive dirs.

Testing Tasks

<junit>
Runs tests from the JUnit testing framework.
<junitreport>
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

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

Miscellaneous Tasks

<echo>
Echoes text to System.out or to a file.
<javadoc>
Generates code documentation using the javadoc tool.
<sql>
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=".">
    <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>

compile target

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

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/${ant.project.name}-${DSTAMP}.jar" basedir="${build}"/>
  </target>

clean target

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

Output of the Script

C:\antexample>ant
Buildfile: build.xml

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

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

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

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

Homework