Section 12.1. Creating a Simple Custom Ant Task


12.1. 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.

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" and "org.apache.tools.ant.Task", respectively).


Table 12-1. The taskdef task's attributes

Attribute

Description

Required

Default

adapter

Specifies the adapter, which adapts the defined class to another interface/class.

No

org.apache.tools.ant.TaskAdapter

adaptto

Specifies the interface/class to which to adapt. Used with the adapter attribute.

No

org.apache.tools.ant.Task

classname

Specifies the classname that will support/perform the type or task.

Yes, unless file or resource have been specified.

 

classpath

Specifies the classpath you want to use when searching for classname.

No

 

file

Specifies the name of the file from which to load definitions, if any.

No

 

format

Specifies the format of the file or resource pointed to by the file or resource attributes. Possible values are properties or xml.

No

properties

loaderRef

Specifies the loader that you want to use to load the class.

No

 

name

Specifies the name of the datatype or task you're creating.

Yes, unless the file or resource type attributes have been specified.

 

onerror

Specifies what to do if there is an error while defining a type. Possible values are:


fail

Causes a build exception.


report

Outputs a warning.


ignore

Does nothing.

Since Ant 1.6.

No

fail

resource

Specifies the name of the resource from which you want to load definitions.

No

 

uri

Specifies the URI at which this type/task definition should be found. Since Ant 1.6.

No

 


The taskdef task's classpath attribute is a path-like structure and can be set with a nested classpath element.

The build file in Example 12-2 builds the simple Ant task and JARs it in greeting.jar. The taskdef task in the same build file retrieves the task from that JARfile and executes the task, which should print out the "No worries." message.

Example 12-2. Build file for a simple Ant task (ch12/greetingtask/build.xml)
<?xml version="1.0"?> <project default="main">     <property name="src" location="src"/>     <property name="output" location="output"/>     <target name="main" depends="jar">         <taskdef name="greeting" classname="Greeting" classpath="greeting.jar"/>         <greeting/>     </target>     <target name="jar" depends="compile">         <jar destfile="greeting.jar" basedir="${output}"/>     </target>     <target name="compile">         <mkdir dir="${output}"/>         <javac srcdir="${src}" destdir="${output}"/>     </target> </project>

Here's what you see when the build file runs. The execute( ) method of the task's code was indeed called, displaying the expected message:

%ant Buildfile: build.xml compile:     [mkdir] Created dir: /home/steven/ant/ch12/greetingtask/output     [javac] Compiling 2 source files to /home/steven/ant/ch12/greetingtask/output jar:       [jar] Building jar: /home/steven/ant/ch12/greetingtask/greeting.jar main:  [greeting] No worries. BUILD SUCCESSFUL Total time: 3 seconds



    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net