I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

My Latest Refcard: Apache Ant

06.28.2010
| 8551 views |
  • submit to reddit

Today DZone are launching my latest refcard where I cover Apache Ant. The purpose of the card is to be a pure reference for the tasks available. This was an interesting refcard to write as I have been using Ant for so long, that I took the range of functions that it can perform for granted. While I'm sure a lot of you have moved on from Ant, it's still a widely used technology in a lot of software development organisations. It's present in all Java IDEs, and I still use it in my day job.

There are a few things that I didn't have the space in the card to elaborate on. One was examples for each of the tasks, which I will provide in a later example. The second was to show how to create your own tasks for us in Ant.

When it comes to creating your own tasks, it couldn't be easier. To create your own task you just need to extend org.apache.tools.ant.Task. There are also a number of other base classes that you can extend such as JDBCTask or MatchingTask. Every attribute in your task will need to have a corresponding setter method in the Task. And finally, for execution of the task you'll need to put the implementation in the public void execute() method, which throw a BuildException in case anything goes wrong.
Here's a short example of a task implementation:

package com.dzone.ant.tasks;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class CustomTask extends Task
{
private String attribute1;
private boolean attribute2;

public void setAttribute1(String s)
{
attribute1 = s;
}
public void setAttribute2(boolean b)
{
attribute2 = b;
}

public void execute() throws BuildException
{
//task logic here
}
}

To use the task in your Ant script, you'll need to create a taskdef to point to it.

<taskdef name="dzonetask" classname="com.dzone.ant.tasks.CustomTask"/>

And you can now use the task later on in your xml, using the value of the name attribute for the task:

<dzonetask attribute1="attvalue" attribute2="true"/>

Finally, if you want to provide your task as if it was one of the default tasks in Ant, you can add the task to the default.properties file in org.apache.tools.ant.taskdefs package.

I hope you find that this card is useful to you. As always, any feedback is welcome, so that we can ensure the refcard is as helpful as possible.

 

Comments

Riccardo Cossu replied on Mon, 2010/06/28 - 10:09am

 Thanks for the refcard, I will definitely download it :-)  I'm one of those who' s still faithful to ant, just recently added Ivy for dependency management.  
I love the freedom it gives you over your build and after all a lot of people still uses make and it works like a charm :-)

Simon Iddings replied on Wed, 2011/11/02 - 4:29am

Great refcard. Though there has been changes and updates to apache ant, this information was valuable nonetheless. It creates a system that is easy to follow and also simple to implement. Thanks.

 Simon - ID Printers

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.