You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Jay Glanville <di...@nortelnetworks.com> on 2000/11/20 18:51:53 UTC

how do I call a task from within a listener.

If I want to call a task from within a listener, how do I do it?

For example, I have a Notify listener, where in the buildFinished() method,
I want to call the <mail> task to broadcast the results of the build.

Do I simply create an instance of Mail, call the necessary setXxx() methods,
and then call execute()?  I'm afraid to do that because I might miss other
methods I'm supposed to call (like initializers, etc).

Jay

PS:  the documentation for the <mail> task is very minimal.  What are the
requirements for this task?  Do I need to have the JavaMail package in my
classpath?  Do I need a UNIX-like "mail" command in my path?  Just how
useful/available to me is the mail task?

----------------------------------------------------------------------------
-----
Jay Dickon Glanville
P068 - SiteManager Development, Nortel Networks
613-765-1144 (ESN 395-1144)
MS: 045/55/A05
E-Mail: dickon@nortelnetworks.com


Re: how do I call a task from within a listener.

Posted by Stefan Bodewig <bo...@apache.org>.
Take whatever I say about <mail> with a grain of salt as I've neither
read the source code for the mail task nor its documentation.

Jay Glanville <di...@nortelnetworks.com> wrote:

> If I want to call a task from within a listener, how do I do it?

Take a look at the description of the life cycle of tasks under
Writing your own task in the Ant documentation. Here you can see what
actually happens with a Task (and what a given task might rely upon).

(1) Use Project.createTask(taskName) to create your task. You can get
an instance of Project from the BuildEvent you've received.

This will set the project for the task as well, there is no reasonable
location attached to your task, so don't set it. Same for target.

(2) Call Task.init() on your instance (might do nothing at all).

(3) If you need to create nested child elements of you task, do so
now.

(4) Set the attributes of your task instance. 

(5) Add any text via Task.addText() if needed.

(6) Set the attributes of the nested child elements (if any) after
that, starting with the first child you've created.

(7) Add any text to the nested children via addText() if
needed. Again using the order you've defined them.

(8) Call Task.execute

(9) Catch any exceptions and do something reasonable with
them. Throwing a BuildException is no good choice as the invocation of
the BuildListener might not be enclosed in a try block.

> PS: the documentation for the <mail> task is very minimal.  What are
> the requirements for this task?  Do I need to have the JavaMail
> package in my classpath?  Do I need a UNIX-like "mail" command in my
> path?

<mail> doesn't use JavaMail at all, it uses a "simple" layer on top of
SMTP written by Jason Hunter. You can find it in
org.apache.tools.mail. So <mail> is a stand alone task that doesn't
rely on any code/utility that doesn't ship with Ant.

Stefan