Apache Ant

Ant file locations

linux location for ant dependencies (all users)
location for ant dependencies (current user)

Command line

invoke default target on build.xml
invoke specified target on build.xml
ant [target]
invoke a target on a sepecified build file
ant [target] -f [build_file]
set the [name] property to [value]
ant [target] -D[name]=[value]
supply an ant dependency (e.g. ant-junit.jar)
ant -lib [ant]


printing a path
<pathconvert  property="[property]" refid="[id]"/>
passing a path to another ant target (for example, to reuse the compile target)
<ant antfile="${[file]}" target="[target]">
	<property name="compile.path" value="${toString:test.compile.path}" />


configuring a target with all properties in a file
<ant antfile="${[file]}" target="[target]">
	<property file="${file}" />


checking that a directory exists
<available file="${dir}" type="dir" property="myproperty"/>
checking that a particular port is taken / available
<condition property="">
	<socket server="localhost" port="${webserver.port}"/>
<fail if="" message="port ${webserver.port} already taken! Is the webserver already running?" />


create zip file (overwrites any existing zip)
<zip destfile="" basedir="sources" />
create or update zip file
<zip destfile="" basedir="sources" update="true" />
merging zip files
<zip destfile="">
    <zipgroupfileset dir="zips" includes="*.zip"/>
merge libs into a jar we are creating
<jar destfile="${jar}">
	<fileset dir="${build.main.classes.dir}" />
	<zipgroupfileset dir="${project.lib.compile.dir}" includes="*.jar"/>

ant macros

macro with attribute
<macrodef name="hello-world">
	<attribute name="message"/>
		<echo message="@{message}" />
invoking the above macro
<hello-world message="Foo! Bar!" />
attribute with default
<macrodef name="hello-world">
	<attribute name="message" default="I have nothing to say"/>
		<echo message="@{message}" />
invoking the above macro
<hello-world />
macro with element
<macrodef name="identity">
	<element name="body"/>
invoking the above macro
		<echo>Foo! Bar!</echo>
macro for asserting file exists
<macrodef name="require">
	<attribute name="file"/>
	<attribute name="message" default="Error! file does not exist: @{file}"/>
		<fail message="@{message}">
			    <available file="@{file}" />
use like this
<require file="${file}"/>
<require file="${file}" message="optional custom message"/>

Further reading

ant scripting

hello world
<script language="javascript">
	java.lang.System.out.println("hello javascript!");
escape angular brackets
<script language="javascript"> 
invoke a task
<script language="javascript">
	var echo = project.createTask("echo");
	echo.setMessage("hello from echo task");
read a property
<property name="property.key" value="hello xml property"/>
<script language="javascript">
set a property
<script language="javascript">
	project.setProperty('property.key', 'hello javascript property');
iterate the elements of a directory
<property name="property.containing.dir" value="/tmp"/>
<script language="javascript">
		var path = project.getProperty('property.containing.dir');
		var dir = new;
		var children = dir.listFiles();
		for(i=0; i<children.length; i++) {
			var child = children[i];
			if(child.isDirectory()) {