Even though the Ant has plenty of available tasks out of the box and a wealth of 3rd party tasks already defined, you still may wish to improve your build process by creating your own custom tasks. This is an especially useful approach to tackling some of the unique quirks you may encounter with regards to your specific build process, and at the very least, this approach can yield a much cleaner build script— after all, doesn't everyone get a headache when pawing through XML code?
So, say you’ve got a class which performs some functionality and you wish to create an Ant task with the same functionality. Take this class below, as an example:
public class QuickTIFFConvert {
public static void main( String args[] ) {
if( args.length == 2 ) {
String input = args[0];
String output = args[1];
Snowbnd s = new Snowbnd();
int pageCount = s.IMGLOW_get_pages( input );
for( int i = 0 ; i < pageCount ; i++ ) {
s.IMG_decompress_bitmap( input, i );
s.IMG_save_bitmap( output, Defines.TIFF_G4_FAX );
}
else
System.out.println("Invalid arguments!");
}
}
QuickTIFFConvert takes in an input and output file name, converts each page of the input file to TIFF Group4 using three simple calls to Snowbound Software’s RasterMaster SDK, and then saves the result as the output file. This class can easily be turned into an Ant task by creating a new class which extends org.apache.tools.Ant.Task and implementing your own execute() methods along with setters for your input parameters, which in this case would be setInput() and setOutput().
public class QuickTIFFConvert extends Task {
private String input, output;
public void execute() throws BuildException {
Snowbnd s = new Snowbnd();
int pageCount = s.IMGLOW_get_pages( input );
for( int i = 0 ; i < pageCount ; i++ ) {
s.IMG_decompress_bitmap( input , i );
s.IMG_save_bitmap( output , Defines.TIFF_G4_FAX );
}
}
public void setInput( String input ) {
this.input = input;
}
public void setOutput( String output ) {
this.output = output;
}
}
After you’ve built your newly defined task with the required dependencies, created a JARchive and included it on the classpath in your build script, you can then define a target like so:
<property name="input.file"
value="/tmp/imgs/document.pdf"/>
<property name="output.file"
value="${input.file}.tiff"/>
<taskdef name="convertTIFF"
classname="org.custom.QuickTIFFConvert"/>
<target name="convert">
<convertTIFF
input="${input.file}" output="${output.file}"/>
</target>
Keep in mind that the Ant expects parameters defined in build scripts identical to how they are defined in the corresponding task.
Now, even if it is unlikely that your build process would include any image conversion, you can still leverage the flexibility of custom tasks and once you get the hang out of it, creating your own tasks can be essential to your build process. Custom tasks could range from regex processing of strings in your source code to advanced testing routines or to custom error handling. Think twice before spawning an <exec> and instead consider leveraging the flexibility built into Ant using it to improve your build process. And remember, if you can create a Java class to do it, then you can create an Ant task to do it.
-Andy
Additional
references:
http://ant.apache.org/manual/develop.html
http://www.onjava.com/pub/a/onjava/2004/06/02/anttask.html
I'm hoping that what you described is just an example, since what you described is easily done with an ant macrodef. If you want to be more sophisticated, you can include the macrodef in your jar file, without writing any additional Java code to implement a custom ant task.
Posted by: Sualeh Fatehi | December 13, 2006 at 12:58 PM