Agenda
- Ant, and why to use it
- Ant terminology and concepts
- Installing and executing Ant
- Core Ant tasks
- A working build script
- Ant techniques and patterns
Agenda - What is Ant?
- Ant, and why to use it
- Ant overview
- What Can Ant Do?
- Yeah, But My IDE Already Does That!
- Build Automation
- Ant terminology and concepts
- Installing and executing Ant
- Core Ant tasks
- A working build script
- Ant techniques and patterns
Ant Overview
- Ant is the ubiquitous build tool in Java environments
- Ant is implemented in Java
- Ant is Open Source, maintained by Apache
- Ant is cross platform and portable
- Ant is well suited to a large number of automation and scripting tasks.
- Ant is not a programming language
What Can Ant Do? (Part 1 of 2)
- Ant can get source code from version control
- CVS, Subversion, Synergy, Perforce, ClearCase and many more
- Ant can compile source code
- Ant can run unit tests
- JUnit3, JUnit4, TestNG, or any arbitrary test application
- Ant can package compiled code and resources
- jars, wars, ears, tars, zips, whatever
What Can Ant Do? (Part 2 of 2)
- Ant can do much more...
- Ant comes with over 80 core tasks, and 60 optional tasks
- There are over 100
third-party tools and tasks written for Ant
- Ant extensions can be easily written for any unique problem
- Ant can handle dependencies between targets
- More details on all these later...
Yeah, But My IDE Already Does That!
- IDE's are hard to automate
- IDE's are harder to setup
- IDE's are not ubiquitous
- Not everyone likes the same IDE
- All IDE's provide Ant integration, some even use Ant internally
- Ant is the least common denominator, it can be relied upon to work in
all scenarios
Build Automation
- Automated build procedures are a best practice
- Manual procedures are mistake prone
- Automated builds are self documenting
- Automated builds improve productivity
- Automated builds can be triggered by other tools
- Nightly builds using cron
- Continous integration using CruiseControl
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
- Ant, and why to use it
- Ant terminology and concepts
- Project
- Target
- Tasks
- Properties
- Paths
- Installing and executing Ant
- Core Ant tasks
- A working build script
- Ant techniques and patterns
Project
- <project> is the top level element in an Ant script
- <project> has three optional attributes:
- name
- the name of the project
- default
- the default target to use when no target is supplied
- basedir
- the base directory from which all path calculations are done
Target
- Each project defines zero or more targets
- A target is a set of tasks you want to be executed
- When starting Ant, you can select which target(s) you want to have executed
- When no target is given, the project's default is used
- Targets can be conditionally executed (using if/unless)
- A target can depend on other targets
- Target dependencies are transitive
Target Example
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="A"/>
<target name="D" depends="B,C"/>
- Suppose we want to execute target D, which depends upon B and C
- C depends on A
- B depends on A
- so first A is executed, then B, then C, and finally D
Tasks
- A task is a piece of code that can be executed
- A task can have multiple attributes (a.k.a arguments)
- The value of an attribute might use the value of a property.
- Ant comes with over 80 core tasks, and 60 optional tasks
- There are over 100 third-party
tools and tasks written for Ant
- Ant task extensions can be easily written for any unique problem
Properties
- A property has a name and a value; the name is case-sensitive
- Properties may be used in the value of task attributes
- This is done by placing the property name between "${" and "}"
in the attribute value
- For example, if there is a "builddir" property with the value
"build", then this could be used in an attribute like this:
${builddir}/classes. This is resolved at run-time as
build/classes
- Properties are immutable:
- whoever sets a property first freezes it for the rest of the
build
- they are most definitely not variable
- they are comparable to string constants in other languages
Setting Properties - Precedence
- When the same property is set in multiple places, the first definition
of the property freezes the value, subsequent definition attempts fail. The
following rules are used to determine precedence:
- Properties defined on the command line:
-Dname=value
<property> elements under the
<project> element in their written order
<property> elements under
<target> elements in their executued order
Setting Properties - Examples
- Set the property
foo.dist to the value "dist":
<property name="foo.dist" value="dist"/>
- Read a set of properties from a file called "foo.properties":
<property file="foo.properties"/>
- Read a set of properties from a classpath resource called "foo.properties":
<property resource="foo.properties"/>
- Read the system environment variables and store them in properties,
prefixed with "env":
<property environment="env"/>
Built-in Properties (Part 1 of 3)
- Ant provides access to all system properties as if they had been defined
using a <property> task.
Built-in Properties (Part 2 of 3)
- In addition, Ant has some built-in properties:
- basedir
- the absolute path of the project's basedir (as set with the
basedir attribute of <project>)
- ant.file
- the absolute path of the buildfile
- ant.version
- the version of Ant
Built-in Properties (Part 3 of 3)
- More Ant built-in properties:
- ant.project.name
- the name of the project that is currently executing; it is set
in the name attribute of <project>
- ant.java.version
- the JVM version Ant detected; currently it can hold the values
"1.1", "1.2", "1.3", "1.4", "1.5" (and "1.6" soon).
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, and why to use it
- Ant terminology and concepts
- Installing and Executing Ant
- Ant Environments
- Setup Ant
- Ant Project Help
- Ant Command Line
- Core Ant tasks
- A working build script
- Ant techniques and patterns
Ant Environments
WSAD / RAD / Eclipse
- Ant is build into these IDEs
- All have a specialised "Ant View"
- Command line
- Main automation environment (CruiseControl)
- Requires JDK to be installed
- The rest of this talk will be assuming that the command line is
being used, but everything will still work in an IDE
Setup Ant
- Download latest stable zip file from http://ant.apache.org/bindownload.cgi
- Unzip downloaded file into a directory
- Setup Environment Variables
- Define
ANT_HOME to be the location where Ant was
unzipped
- Define
JAVA_HOME to be the location where the JDK
is installed
- Add
%ANT_HOME%\bin to the PATH
Ant Project Help
ant -projecthelp will list descriptions and targets for a
build file.
%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
ant [options] [target-names]
- runs targets with specified names, preceded by targets on which
they depend
- can specify multiple target names separated by spaces
- omit target-name to run the default target
-D option specifies a property that can be used by
targets and tasks.
e.g. -Dproperty-name=property-value
ant -help
- lists other command line options
Agenda - Core Ant Tasks
- Ant, and why to use it
- Ant terminology and concepts
- Installing and executing Ant
- Core Ant tasks
- File Tasks
- Compile Tasks
- Archive Tasks
- Testing Tasks
- Property Tasks
- Miscellaneous Tasks
- A working build script
- Ant techniques and patterns
File Tasks
<copy>
<concat>
<delete>
<filter>
<fixcrlf>
<get>
<mkdir>
<move>
<replace>
<sync>
<tempfile>
<touch>
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
- Ant, and why to use it
- Ant terminology and concepts
- Installing and executing Ant
- Core Ant tasks
- A working build script
- Example Scenario
- init target
- clean target
- compile target
- dist target
- Output of the Script
- Ant techniques and patterns
Example Scenario
- Top level directory called
antexample
build.xml lives in the top level directory.
- Code is kept in the
src subdirectory
- Single source code file called
src/HelloWorld.java
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
- Ant, and why to use it
- Ant terminology and concepts
- Installing and executing Ant
- Core Ant tasks
- A working build script
- Ant techniques and patterns
- Use Simple Targets
- Use Standard Targets
- Use Properties for Configurability
Use Simple Targets
- Each target should do a single well defined job
- Targets that do muliple jobs should be split up into multiple targets
with dependencies between them
- Targets that do too much make the build harder to maintain
- Library scripts need to allow targets to be overridden. This is easier
when the targets perform a single job
Use Standard Targets
- It is easier for people to use a standard set of commands
- Developers can switch from one project to another and immediately
understand how the build script works
- Standard targets can be easily overriden in a project build file
Use Properties for Configurability
- Properties should be defined for:
- Any piece of information that needs to be configured
- Any piece of information that might change
- Any piece of information that is used in more than one place
- Properties should be defined at the top of the buildfile or in a
standalone properties file
- Properties explicitly define the configurable portion of the build.
You can provide a different version of this properties file for different:
- platforms
- environments
- operating systems
- developers
The End