Platinum Partner
java,build automation

My Latest Refcard: Apache Ant

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.

 
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}