Task Dependencies: ant

Steven J Zeil

Last modified: Mar 19, 2014

ant is a build manager based upon a task dependency graph expressed in an XML file

What’s Wrong with make?

ant is actually an acronym for Another Neat Tool.

But why do we need “another” tool for build management?

Other Criticisms

1. The ant Command

ant Options

Some useful options:

-k, -keep-going
“Keep going.” Don’t stop the build at the first failue, but continue building any required targets that do not depend on the one whose construction has failed.
-f filename
Use filename instead of the default build.xml. Also -file or -buildfile
-Dproperty=value
Sets a property (similar to make’s variables)

2. Build Files

The ant build file is an XML file.

<project name="382Website" default="deploy">
     <description>
        Extract Metadata Extractor - top level
    </description>
  ⋮
</project>

2.1 Targets

At its heart, a build file is a collection of targets.

ant targets correspond, roughly, to make’s “artificial targets”.

Example of Targets

simplebuild.xml.listing

The project has a name and default target
A basic target. It is named “compile” and has a description (which may be picked up by some IDEs)
This target has 3 tasks. It creates a directory, compiles Java source code, and prints a message when completed.

This target illustrates both a dependency and a condition.

  • The tasks within this target would not be executed if I invoked ant like this:

    ant -Dtest.skip=1
    
  • However, the unittest task would still be considered to have succeeded, in the sense that tasks that depend on it would be allowed to run.

Task versus File Dependencies

ant targets correspond, roughly, to make’s “artificial targets”.

So this build file

simplebuild.xml.listing

is roughly equivalent to this makefile

simplemake.listing

though a “real” makefile author would probably write this:

simplemake2.listing

Make Efficiency

If we do

make
make

The second command does not actually perform any steps.

Ant Efficiency

What happens if we do

ant
ant -Dskip.test=1

2.2 Properties

Properties are named string values.

The <property Task

Two basic modes:

Additional <property Variants

2.3 File Sets and Lists

File Sets

<fileset file="src/main.cpp"/>
<fileset dir="src" 
    includes="main.cpp utility.h utility.cpp"/>
<fileset dir="src" includes="*.cpp,*.h"/>

File Lists

<filelist dir="src" 
    files="main.cpp utilities.h utilities.cpp"/>

Mappers

would map a compiled unit test file project/package/TestADT.class to project.package.TestADT

Selectors

Selectors provide more options for selecting file than simple include/exclude based on the names.

<fileset id="unitTestSrc" dir="src">
   <include name="**/Test*.java"/>
   <contains text="@Test" casesensitive="no"/>
</fileset>

(Our previous examples assumed that unit tests woule be identified by file name. Here we look instead for the JUnit4 @Test annotation.)

2.4 Path Sets

Used to specify a sequence of paths, usually to be searched.

<classpath>
    <pathelement path="${env.CLASSPATH}"/>
    <fileset dir="target/classes">
       <include name="**/*.class"/>
    </fileset>
    <filelist refid="third-party_jars"/>
</classpath>

Referencing Path Sets

2.5 Filters

Filters are used to modify the outputs of some commands by performing various substitutions:

<copy file="../../templates/@{format}.tex" 
      tofile="${doc}-@{format}.ltx">
  <filterset>
    <filter token="doc" value="${doc}"/>
    <filter token="relPath" value="${relPath}"/>
    <filter token="format" value="@{format}"/>
  </filterset>
</copy>

A filter set replaces tokens like @doc@ by a string, in this case the value of the property ${doc}

Filter Chains

Filter chains offer a variety of more powerful options, e.g.,

<loadfile property="doctitle" srcfile="${doc}.info.tex">
  <filterchain>
    <linecontains>
      <contains value="\title{"/>
    </linecontains>
    <tokenfilter>
      <replaceregex pattern=" *\\title[{]([^}]*)[}]"
                    replace="\1"/>
    </tokenfilter>
  </filterchain>
</loadfile>

2.6 Tasks

The Ant Manual has a good breakdown on these.

Extending Ant

<project name="code2html" default="build">

  <taskdef classpath="JFlex.jar" 
           classname="JFlex.anttask.JFlexTask" 
           name="jflex" />
     ⋮

  <target name="generateSource">
    <mkdir dir="src/main/java"/>
    <jflex file="src/main/jflex/code2html.flex"
       destdir="src/main/java"/>
    <jflex file="src/main/jflex/code2tex.flex"
           destdir="src/main/java"/>
       ⋮

Finding Extensions

3. Case Studies

3.1 Code Annotation

The steps involved in building this tool are:

1.Run the program jflex on each file in src/main/jflex, generating a pair of .java files that get placed in src/main/java

  1. Compile the Java files in src/main/java, placing the results in target/classes

  2. Compile the Java files in src/test/java (using the target/classes compilation results, placing the results in target/test-classes.

  3. Run the JUnit tests in target/test-classes.

  4. If all tests pass, package the compiled classes in target/classes into a .jar file.

The Code Annotation Tool Build

codeAnnotation.build.listing

This is a fairly “stock” build for a Java project.

Nonetheless, there are multiple steps that would not be handled by typical IDE build managers.

build-Linux.paths.listing

build-Windows7.paths.listing

3.2 Projects with Multiple Sub-Projects

Managing Subprojects with ant

Typical key ideas:

A Top-Level Build

topProject-build.listing

3.3 Extend or Exec?

As we move further from common Java project structures, the problem arises of how to issue commands for which we have no readily available ant task.

Mitigating Factors

Although the community has contributed numerous extension tasks,

This can drive a project to use exec/apply even if tasks purportedly exist

Generating Course Website PDFs

Each document is a subproject with a build file like this:


<project name="docs" default="build">

  <import file="../../commonBuild.xml"/>

  <target name="makeSources" depends="properties">
    <docformat format="slides"/>
    <docformat format="web"/>
    <docformat format="printable"/>
    <docformat format="10-4x3"/>
    <docformat format="10-16x10"/>
    <docformat format="7-4x3"/>
    <docformat format="7-16x10"/>
  </target>


</project>


The “good stuff” is in the included commonBuild.xml

commonBuild.listing

4. Eclipse/Ant Integration

Limitations of Eclipse Builder

Project Dependencies

Eclipse supports the idea of projects that depend on other projects, so you could do

Eclipse/Ant Integration

Eclipse is generally ant-friendly.

Eclipse Builders

Eclipse supports multiple, plug-able builders.