Skip to content

Ant

Another Neat Tool, that's what it stands for ;)

Since i have more or less chosen for gradle or at least maven the gaps in this document are not likely to be filled soon.

Quick start

Want to get a java project started quickly ? :

build.xml
<?xml version="1.0"?>
    <project name="fax" basedir="." default="build">
       <property name="src.dir" value="src"/>
       <property name="build.dir" value="."/>
       <property name="name" value="project"/>

       <target name="build" description="Compile source tree java files">
          <mkdir dir="${build.dir}"/>
          <javac destdir="${build.dir}" includeantruntime='false'>
             <src path="${src.dir}"/>
          </javac>
       </target>

       <target name="clean" description="Clean output directories">
          <delete>
             <fileset dir="${build.dir}">
                <include name="**/*.class"/>
             </fileset>
          </delete>
       </target>
    </project>

This will just compile anything in the source directory, and setting the build directory to . It means you can run it from there if you match the package name :

For instance no package name results in ./File.class, so this will run :

running
java ./File

If you specify a package like :

with package prefix
package some.other; 

it results in the tree : ./some/other/File.class and so this will run :

with directory
java some.other.File 

Also, this gives you the opportunity to just complete the class for running.

And if you want to know : the includeantruntime is there to get rid of an annoying warning :

includeantruntime
1
2
3
[javac] /bigsafe/kees/projects/finance/transactions/build.xml:9: warning: 
'includeantruntime' was not set, defaulting to build.sysclasspath=last; 
set to false for repeatable builds

From the apache documentation :

Whether to include the Ant run-time libraries in the classpath; defaults to yes, unless build.sysclasspath is set. It is usually best to set this to false so the script's behavior is not sensitive to the environment in which it is run.

So, as many advise on the interwebz... just add that option and forget it ever happened.

just a target

target
1
2
3
4
5
6
<?xml version="1.0"?>
    <project name="Analyze project" default="Analyze">
    <target name="Analyze">
        <echo>Just say hello !</echo>
    </target>
</project>

Very simple one, if you run :

run
ant

You get :

output
1
2
3
4
5
Analyze:
    [echo] Just say hello !

BUILD SUCCESSFUL
Total time: 0 seconds

Because the default target is "Analyze" it will do the same as :

analyse
ant Analyze

some handy variables :

Would produce something like :

output
1
2
3
Analyze:
    [echo] Just say hello !
    [echo] Apache Ant version is Apache Ant(TM) version 1.9.4 compiled on April 29 2014 - You are at ${sitename} 

build.properties

To keep some settings out of the build.xml file, but still be able to use them, use build.properties :

And simply use them in the ant file after including the file :

build.xml
1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
    <project name="Analyze project" default="Analyze">
    <property file="build.properties"/>

        <target name="Analyze">
            <echo>Just say hello !</echo>
            <echo>Apache Ant version is ${ant.version} - You are at ${sitename} </echo>
        </target>
    </project>
Analyze:
    [echo] Just say hello !
    [echo] Apache Ant version is Apache Ant(TM) version 1.9.4 compiled on April 29 2014 - You are at klopt.org 

data types

All data types in ant are :

  • Description type : Describes a project
  • PatternSet : Groups of patterns
  • DirSet : Groups of directories
  • FileSet : Groups of files
  • FileList : Explicit list of files
  • FileMapper : Translates filenames
  • FilterReader : Custom class that filters out files
  • FilterChain : Series of FilterReaders to further filter files
  • FilterSet : Groups of filters
  • Selectors : Provides more control over file selection
  • Class FileSet : A FileSet for class files
  • Path-like structures : A data type resembling a file system path
  • XMLCatalog : Catalog of public XML resources

pattern set

todo:

file set

File sets represent just that, it can us include and exclude patterns :

exclude
1
2
3
4
<fileset dir="${src}" casesensitive="yes">
    <include name="**/*.java"/>
    <exclude name="**/*Stub*"/>
</fileset>