Chapter 12. Extending Ant

There's more to Ant than what comes out of the box because you can extend Ant in various ways. The most common way of extending Ant is by creating your own tasks, and this chapter covers how to do that. You'll learn how to create new tasks, handle task attributes, access property values, work with nested text and elements, make builds fail, work with filesets, use custom tasks as wrappers for external programs, and more.

Besides creating new tasks, you can extend Ant in other ways, such as using scripting languages such as JavaScript, which I'll explain. You can even create listeners that respond to build file events by executing Java code, and create loggers that log data as a build progresses. And you can create custom filters and selectors, which you can use with some Ant tasks such as copy.

Creating a Simple Custom Ant Task

Creating new Ant tasks is simple since all you need is an execute( ) method. Example 12-1 is a Java class named Greeting that displays the text "No worries.".

Example 12-1. A simple Ant task (ch12/greetingtask/src/Greeting java)

public class Greeting 
{
    public void execute( ) 
    {
        System.out.println("No worries.");
    }
}

To install this class as a new Ant task, you compile this code and use the taskdef task to declare it in Ant. The attributes of the taskdef task are shown in Table 12-1.

Tip

The taskdef task is based on the typedef task, except that the values of two attributes, adapter and adapto, are preset to fixed values ("org.apache.tools.ant.TaskAdapter" ...

Get Ant: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.