Over a million developers have joined DZone.

My Latest Refcard: Apache Ant

· Java Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

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.

 

The Java Zone is brought to you by Stormpath—a complete, pre-built User Management API. Want to learn how to use JWTs to protect microservices from CSRF and more? Check out this on-demand webinar with our Java Developer Evangelist, Micah Silverman.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}