

No how-to guide would be complete without including a “Hello World” example. In short, your custom task exists as much as a first class citizen as any core Ant task.

Your task also may look up and set project-level properties and emit log messages at any standard log level (warning, debug, and so forth). It should be noted that your task can interact with other, nested Ant tasks such as the standard FileSet task. I will discuss the above points in further detail below. In a standard Ant build.xml file, registering your custom task by using the standard taskdef task.Packaging your class file(s) into a jar file.Creating a Java class that subclasses .Task and overrides the execute() method.The basic procedure for creating a custom task includes:

(As an aside, an IDE makes a great tool to poke a little deeper into the details of the standard tasks, and source code for the tasks is available both online and via the “source distribute” of Ant.) Overview Those of you following along on a command line should take care to include ant.jar into your CLASSPATH system environment variable. Those of you using an IDE should include the “ant.jar” file located in the “lib” folder of your Ant installation. I include commands to demonstrate how this works on a command line. I am working with Ant version 1.6.5 and Java 5 on Mac OS X however, the examples in this article should work with any reasonably modern version of Ant and Java on any platform. This article assumes a basic familiarity with Ant. In this article, I will walk you through the basic steps needed to create a custom task for Ant. Fortunately, it is fairly simple to create your own task handler in Java and connect it to Ant to give you that extra little feature only you need. Ant provides well over a hundred different task handlers, and although this is usually sufficient, you may find yourself needing to step beyond Ant’s current abilities: You may need to run some complex post-build verification checks that you want to hide behind a simple lone element, you may need to use arcane options beyond those the standard tasks provide, or you just may want to rot13 your source files… just because.

It streamlines weekend hobby development, and it brings automated sanity to very complex, commercial endeavors. Running Ant on the above build file produces the following output −į:\tutorialspoint\ant>ant -DcopyFile=trueīuildfile: F:\tutorialspoint\ant\build.xmlį:\tutorialspoint\ant>ant move -DcopyFile=trueį:\tutorialspoint\ant>ant move -DcopyFile=falseīuildfile: F:\tutorialspoint\ant\build.Apache Ant is a great tool to manage your build tasks. ExampleĬreate build.xml with the following content − The variable is to be defined, the value of variable is of no relevance here. We'll be using -Dproperty to pass varible like copyFile to the build task. We can use if statement or unless statement. Ant allows to run targets based on passed conditions.
