Import Custom Tasks in Ant Scripts

I recently had to import custom ant tasks in an ant build.xml file. Custom ant tasks are usually shipped in a JAR file. Two options were available in the project’s context to make a custom ant task defined in a JAR file available in the build file. Those options are described below along the example of the antcontrib ant library.

Option 1: JAR file in %ANT_HOME%/lib

If you put the antcontrib.jar file in the lib directory of your ant installation, the library will be available by default. All you have to do is to load the ant tasks from this library:

<taskdef resource="net/sf/antcontrib/antlib.xml"/>

From this point on, all ant tasks defined in antcontrib.jar are available in your ant build file.

Option 2: JAR file anywhere

In most cases you don’t want to “dirty” your ant installation by putting a custom library in the lib directory. So, you have to specify the location of the JAR file (which would be in some sub directory of your project):

<taskdef resource="net/sf/antcontrib/antlib.xml"

Using namespaces in Ant

To keep your custom ant tasks distinct from default ant tasks, you can add namespaces to each custom library. Here is a toy example:

<project name="MyProject" xmlns:antcontrib="net/sf/antcontrib"/>
<taskdef uri="net/sf/antcontrib"

<!-- now we have to use the prefix "antcontrib:" to access
  the antcontrib tasks -->
<antcontrib:foreach ... />

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s