Tuesday, June 29, 2010

Create your own Ant task


Target Audience: Beginners
Apache Ant is one of the most popular and powerful build tool is the industry. Ant script is a xml file which contains the details of the project and its target that need to be used for the building purpose. Some very common targets are compile, clean, build and dist.

One of the reasons that I like Ant is that it allows user to define their own task. Example: if I need to need to backup files as zip format with a file name which contains the time stamp then I can do that. I can do that in a normal way like having a target which called as “backup” which just zips the files that I need to copy and name it with the time stamp and put it in a folder that I specify. In the other way, I can write a custom ant task which will do that for me.

I’m going to create a custom task which just says the first voice of programmers “Hello World”. I can do this as a normal way like the following.


But I try to do it in a different way so that I can learn how to write a custom task.

Before getting started, download and install Ant version 1.6.1 or later on your system, if it is not already there. When you extract the files from the Ant archive (in .zip, .gzip, or .bzip2 format, depending on what you download), you'll notice a bin directory in the distribution. Place the bin directory in your path, and then type ant -version at a command or shell prompt. If you get back a response like
Apache Ant version 1.6.1 compiled on February 12 2004,

Let’s get started!

The following is the custom task

package com.ltol.ant;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class HelloTask extends Task
{
// The method executing the task
public void execute()
throws BuildException {
System.out.println("Hello World");
}
}

If you see the class, there are two classes are imported BuildException and Task classes. BuildException is an important class which will throw the exception when something goes wrong while executing the task. Task is the main class that we need to extend from if we need to create a custom task. The execute() of the Task class is the one will be executed when the target get triggered.

Now we’ll compile
javac -classpath ant.jar com/ltol/ant/HelloTask.java
Once you are successful at compiling HelloTask.java, package HelloTask.class into a .jar file:
jar cvf hello.jar com/ltol/ant/HelloTask.class
The class or classes you use for the task apparently must be in a .jar file to work -- I've tried just using bare classes in a variety of ways with no success. For convenience, place a copy of hello.jar in the class path or to the ant lib directory where all of the Ant .jar files live. With hello.jar there, Ant will be able to find the class in it at build time.
Now we’ll create a build file to execute task that we created. The following is our build.xml file

If you run this build file, you will be getting the output something like this. Make sure that you have the hello.jar file in the class path.


Buildfile: build.xml
sayhello:[hello]
Hello World
BUILD SUCCESSFUL
Total time: 1 second
How simple.

No comments:

Post a Comment