home | tech | misc | code | bookmarks (broken) | contact | README


Ant notes

Basics

Apache Ant is a build system built on the top of Java and it was made to as a replacement of make. More information about it can be found in the Apache Ant Wikipedia article.

Differences from make

The main difference from make is that, while make uses a target-dependency concept and shell commands to use dependencies to create targets, described in a Makefile, Ant uses a file called build.xml with rules written in XML. It also has the target concept, but uses it in a different way.

One reason on why people prefer Ant is that it doesn't depend on shell commands, which are easy to be unportable. All rules in Ant are in XML and Ant implements all tags internally.

See other differences between Ant and make in this link.

Structure of build.xml

A very basic build.xml file looks like this (Adapted from the Apache Ant Manual - Using Apache Ant):

<project name="MyProject" default="dist" basedir=".">
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>

  <!-- Configure the CLASSPATH -->
  <path id="MyClassPath">
    <pathelement location="one_jar_file.jar"/>
    <fileset dir="path_to_directory_containing_jar_files" includes="*.jar" />
  </path>

  <!-- Compiles project -->
  <target name="compile">
    <mkdir dir="${build}"/>
    <javac srcdir="${src}" destdir="${build}" classpathref="MyClassPath"/>
  </target>

  <!-- Makes a jar file -->
  <target name="dist" depends="compile">
    <mkdir dir="${dist}/lib"/>
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <!-- Clean everythin -->
  <target name="clean">
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

The <javac> tag is where all compilation is described. It calls the Java compiler for *.java files. Note that, although the tag is <javac>, other compiles other than javac can be used, with the build.compiler property. See this link for more information.

Important parameters for the javac tag are:

srcdir
The top of your source directory. Normally it is the current one ".".
destdir
Where destination *.class files are delivered.
encoding
Encoding used in the project.
classpathref
The $CLASSPATH reference

Pay attention to the srcdir dir parameter. It normally should point to the root directory of your source tree, not indifividual package directories. Ant does the job of finding Java files walkking down the directory tree, unlike make, you don't need to tell it the path of each file.

Other useful parameters are fork, memoryinitialsize and memorymaximumsize that are useful if you run into memory problems (see the memory troubleshooting below for details.

Troubleshooting

error Cannot run program ".../${aapt}": java.io.IOException: error=2, No such file or directory

Once, when compiling an Android program, I got an error like:

...build.xml:694: Execute failed: java.io.IOException: Cannot run program ".../${aapt}": java.io.IOException: error=2, No such file or directory

There seem to be different reasons for this problem. For some reason appt could not be found. The solution I found Insert the aapt location in ant.properties of the project I was editing file:

aapt=/path/to/aapt

The procedure is the same for any other executable.

error: package ... does not exist

I set up srcdir wrongly.

The system is out of resources. ... java.lang.OutOfMemoryError: Java heap space

This happens because, in some situations, ant needs more memory. The compiler needs more memory. For the javac tag, configure the memoryinitialsize, memorymaximumsize and fork parameters, like:

<javac srcdir="${src}"
       destdir="${classes}"
       fork="true"
       memoryinitialsize="512m"
       memorymaximumsize="1024m" />

For more information see this and this.