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
verbose mode
ant -v
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]

hello world

<project default="hello">
    <target name="hello">
increase logging verbosity
<script language="javascript">
	var logger = project.getBuildListeners( ).firstElement( );
	logger.setMessageOutputLevel( 9 );


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


detect windows
<condition property="">
	<os family="windows" />
configuring a target with all properties in a file
<ant antfile="${[file]}" target="[target]">
	<property file="${file}" />
downloading a file (wget)
<get src="${url}" dest="${destination}" />
downloading a file only if missing
<get src="${url}" dest="${destination}" skipexisting="true" />


javadoc simple
<javadoc sourcepath="${src}" destdir="${api}" defaultexcludes="yes" version="true" use="true" />


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


running a select query on a database
<project default="mysql">
    <property name="db.jar" value="mysql-connector-java-8.0.22.jar" />
    <property name="db.driver" value="com.mysql.cj.jdbc.Driver" />
    <property name="" value="localhost" />
    <property name="db.database" value="mysql" />
    <property name="db.user" value="root" />
    <property name="db.password" value="secret" />
    <property name="db.url" value="jdbc:mysql://${}/${db.database}" />
    <target name="mysql">
        <sql driver="${db.driver}" url="${db.url}" userid="${db.user}" password="${db.password}" print="true">
                        <pathelement location="${db.jar}"/>
                show tables;

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()) {