Created at:

Modified at:

Ant notes



Apache Ant is a build system built on the top of Java and it was made to as a replacement of "make".

Apache Ant


Wikipedia Apache Ant 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.

Why is no one using make for java - Stack Overflow

Structure of build.xml

A very basic build.xml file looks like this:

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

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

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

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

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.

Setting the target version of java in Ant - javac - Stack Overflow

Important parameters for the javac tag are:

The top of your source directory. Normally it is the current one ".".
Where destination *.class files are delivered.
Encoding used in the project.
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 below).


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

Once, when compiling an Android program, I got an error like:: Execute failed: Cannot run program ".../${aapt}": 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 of the project I was editing file::


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}"
           memorymaximumsize="1024m" />

"java.lang.OutOfMemoryError: Java heap space" while generating reports - Stack Overflow

Build failed java.lang.OutOfMemoryError: Java heap space" - Stack Overflow