It is very easy to write your own task:
org.apache.tools.ant.Task
.public void
method that takes a single argument. The
name of the method must begin with set
, followed by the
attribute name, with the first character of the name in uppercase, and the rest in
lowercase. The type of the attribute can be:
String
true
,
yes
, or on
)
Class
File
(in which case the value of the attribute is interpreted relative to the
project's basedir)
String
argument
org.apache.tools.ant.types.EnumeratedAttribute
as an argument
to your setter method. See
org/apache/tools/ant/taskdefs/FixCRLF.java
for
an example.public void
addText(String)
method.public
method that takes no arguments and returns
an Object
type. The name of the create method must begin with
create
, followed by the element name. An add method must be
a public void
method that takes a single argument of an
Object
type with a no-argument constructor.
The name of the add method
must begin with add
, followed by the element name.public void execute
method, with no arguments, that
throws a BuildException
. This method implements the task
itself.project
and
location
variables.id
attribute to this task,
the project
registers a reference to this newly created task, at parser
time.target
variable.init()
is called at parser time.createXXX()
methods or
instantiated and added to this task via its addXXX()
methods, at parser time.setXXX
methods, at runtime.addText
method, at runtime.setXXX
methods, at runtime.execute()
is called at runtime. While the above initialization
steps only occur once, the execute() method may be
called more than once, if the task is invoked more than once. For example,
if target1
and target2
both depend
on target3
, then running
'ant target1 target2'
will run all tasks in
target3
twice.Let's write our own task, which prints a message on the
System.out
stream.
The
task has one attribute, called message
.
package com.mydomain; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; public class MyVeryOwnTask extends Task { private String msg; // The method executing the task public void execute() throws BuildException { System.out.println(msg); } // The setter for the "message" attribute public void setMessage(String msg) { this.msg = msg; } }
It's really this simple ;-)
Adding your task to the system is rather simple too:
<taskdef>
element to your project.
This actually adds your task to the system.<?xml version="1.0"?> <project name="OwnTaskExample" default="main" basedir="."> <taskdef name="mytask" classname="com.mydomain.MyVeryOwnTask"/> <target name="main"> <mytask message="Hello World! MyVeryOwnTask works!"/> </target> </project>
Another way to add a task (more permanently), is to add the task name and
implementing class name to the default.properties
file in the
org.apache.tools.ant.taskdefs
package. Then you can use it as if it were a built-in task.
Ant is capable of generating build events as it performs the tasks necessary to build a project. Listeners can be attached to Ant to receive these events. This capability could be used, for example, to connect Ant to a GUI or to integrate Ant with an IDE.
To use build events you need to create an ant Project
object. You can then call the
addBuildListener
method to add your listener to the project. Your listener must implement
the org.apache.tools.antBuildListener
interface. The listener will receive BuildEvents
for the following events
If you wish to attach a listener from the command line you may use the
-listener
option. For example:
ant -listener org.apache.tools.ant.XmlLogger
will run Ant with a listener that generates an XML representation of the build progress. This listener is included with Ant, as is the default listener, which generates the logging to standard output.
Copyright © 2000,2001 Apache Software Foundation. All rights Reserved.