Ant write your Task

Excerpt How to write an apache ant task in Java, and how to use inside your build file
Keywords java, ant, task, apache, build

  1. Write Simple Ant Task – example 01
    1. write simple task in java
    2. clean, compile and package jar
    3. run the task
    4. first example Code and considerations
  2. Write Simple Ant Task – example 02
    1. write ant task in Java
    2. run the task
  3. References

To write and execute your own ant task, you need

  • to write a java class extending org.apache.tools.ant.Task
  • to declare and call your task in your buildfile

Write Simple Ant Task – example 01

As first example we write a simple task that prints an Hello World message.

To write an ant task you have to start writing a java class that contains an execute() method. Then you have to compile / package the class. Finally you have to declare the task with <taskdef ../> and use it.

write simple task in java

Your java task class must contain an execute() method that performs task operation

package test;
public class HelloWorld {
    public void execute() {
        System.out.println("Hello World");
    }
}

clean, compile and package jar

Suppose you have prepared an initialization task, in which are declared properties dir.sources, dir.build, and needed directories and cleaned.

<target name="init"
    	description="initialize properties and classpath">
    <property name="dir.sources" value="src"/>
    <property name="dir.build" value="build"/>
	<delete dir="${dir.build}" includeemptydirs="true" includes="**/*"/>
	<mkdir dir="${dir.build}/classes" />
	<mkdir dir="${dir.build}/jar" />
</target>

First we have to compile the java source code.

<target name="compile" depends="init" 
		description="compiles java classes" >
	<javac srcdir="${dir.sources}" destdir="${dir.build}/classes">
		<!-- if needed put here the <classpath refid="..."/> -->
	</javac>
</target>

Once compiled we package classes in a jar file

<target name="jar" depends="compile" 
		description="compile and build">
	<jar jarfile="${dir.build}/jar/${ant.project.name}.jar">
		<fileset dir="${dir.build}/classes" includes="**" />
		<!-- if needed put here the <manifest/> -->
	</jar>
</target>

run the task

To run helloworld task, simply declare it using taskdef. Then you can use it inside your buildfile.

<target name="use" depends="jar"
	description="Use the custom task">
	<taskdef name="helloworld" classname="test.HelloWorld" 
		classpath="${dir.build}/jar/${ant.project.name}.jar"/>
	<helloworld/>
</target>

first example Code and considerations

In this case the integration between our class and the Ant core is made via a naming convention. What really happens behind the scene, is that our class is wrapped by Ant’s aor.apache.tools.ant.TaskAdapter

You can download source code of first example

Write Simple Ant Task – example 02

As second example we write a task that executes depending on parameters received as xml attributes from the buildfile.

In this case we are going to write a Java class extending Ant’s Task class. The class takes parameters from xml attributes by exposing public setters.
Note that since we extend the Task class, we need to add Ant’s libraries to our compile path.

write ant task in Java

First, download the ant archive, extract contents and add the ant jar to your build path.

Then you can write a java class extending org.apache.tools.ant.Task.

public class MyExtendedTask extends Task {

	private String message;
	public void setMessage(String message) {
		this.message = message;
	}
	
	@Override
	public void execute() throws BuildException {
		System.out.println( "MyExtendedTask. message=" + message );
		super.execute();
	}
	
}

Note if you compile from an IDE, you need to add ant.jar to your build path. Otherwise, if you perform compilation inside Ant’s buildfile, you can not include the library.

run the task

Since we need no changes to the uild path; we simply rewrite the use task, by declaring MyExtendedTask and invoking it.

<target name="use" depends="jar"
	description="Use the custom task">
	<taskdef name="mytask" classname="test.MyExtendedTask" 
		classpath="${dir.build}/jar/${ant.project.name}.jar"/>
	<mytask message="message in a bottle"/>
</target>

See resulting output

<code>use:
   [mytask] MyExtendedTask. message=message in a bottle
build:
BUILD SUCCESSFUL

References