Skip to content
UoL CS Notes

Ant Structured Build

COMP285 Lectures

Standard Directory Names

Directory Name Description
src Source Files
build/classes or bin Intermediate Output
dist Distributeable Files

All files apart from src are generated and can be deleted.

Packages

This is a method of grouping files together with their scope. The package is named using a reverse fully qualified name:

uk.bweston.utils

This will create a directory structure like so:

uk/bweston/utils

To let java know about the package we can declare it in our source files:

// src/uk/bweston/utils/Main.java

package uk.bweston.utils
public class Main {
	public static void main(String args[]) {
		// do stuff
	}
}

The package declaration says where to put the file in the build/classes folder.

We can compile packages like so:

javac -d ./build/classes src/uk/bweston/utils/Main.java

This saves the generated files in ./build/classes.

Ant & Structured Build

Ant will use the package declaration to complete dependency checking. This saves time by not recompiling unchanged files.

This won’t check for changed parent or imported classes.

We can tell ant that source files depend on each other by using the <depend> tag.

Structured build.xml

We can put this all together in the following build.xml:

<?xml version="1.0" ?>
<project name="structured" default="archive">
	<target name="init">
		<mkdir dir="build/classes"/>
		<mkdir dir="dist"/>
	</target>
	<target name="compile" depends="init">
		<javac srcdir="src"
			destdir="build/classes"
			includeAntRuntime="no"/>
	</target>
	<target name="archive" depends="compile">
		<jar destfile="dist/project.jar"
			basedir="build/classes"/>
	</target>
	<target name="clean" depends="init">
		<delete dir="build"/>
		<delete dir="dist"/>
	</target>
</project>

default sets the default target.

clean depents on init as files must exist in order to delete them.

To run targets other than the default we can name the target we want:

ant clean

We can also name multiple targets in a space separated list.

You can have multiple dependencies on a target:

<target name="all" depends="archive,clean"/>

This will execute the dependencies in order.

Executing Using build.xml

We can write a target like so to run the compiled program:

<target name="execute" depends="compile">
	<java
		classname="uk.bweston.util.Main"
		classpath="build/classes">
		<arg value="a"/>
		<arg value="b"/>
		<arg file="."/>
	</java>
</target>

This is the same as running:

java -cp build/classes uk.bweston.util.Main a b .