Download O`Reilly - Ant The Definitive Guide

Transcript
Ant: The Definitive Guide
4. Call init( ).
The init( ) method in the task object is now called. Remember that task attributes
are not available at this time. In addition, information your task needs from nested
elements is also unavailable. As a side note, many of the distributed tasks don't
implement this method.
5. Nested elements are parsed and processed using addXXX( ), addConfiguredXXX( ),
and createXXX( ) methods.
This is probably the most important (and most difficult) step to understand in the
entire life cycle. Intuitively, you might think that Ant defines and processes task
attributes during parse-time, but this is not the case. Ant does not look at task
attributes until runtime. This also means that the inclusion of unsupported attributes in
the buildfile goes unnoticed until runtime. However, Ant processes nested elements
during parse-time. Therefore, it catches the use of unsupported elements before it
catches the use of any unsupported attributes.
So
how does Ant process nested elements? It calls createXXX( ),
addConfiguredXXX( ), or addXXX( ) on your task, where XXX is the capitalized
name of the nested element. What is the difference between the createXXX( ),
addConfiguredXXX( ), and addXXX( ) methods? It depends on how you plan to use
the nested element and the nature of the element's corresponding object. If your task
needs to instantiate the element object itself, or, if the object has no default
constructor, then use create; think of it as "your task creates the nested object." If
your task needs a reference to an already instantiated object, then use add; think of this
as "Ant adds the object reference to your object." If you need Ant to fully process the
element before it passes the references, use addConfigured; think of this as "Ant adds
the configured object reference to your task object." Review existing task
implementations if these differences still confuse you. Incidentally, Ant calls
createXXX( ) methods first. If you implement more than one method for a particular
element type, Ant calls them all. The consequences of doing this can be dire, so try not
to do it!
5.3.2 The Runtime Phase
The runtime phase is the moment of reckoning for a task. It begins when the parse-time phase
of the task is successfully complete. Other targets and tasks may have already run successfully
by the time your task enters the runtime phase. While you may wish to rely upon certain
expected behaviors and states set prior to your task's runtime step, resist the temptation! Your
task should operate atomically, and be capable of running as the first or last task of a build.
Here's a list of what happens at task runtime:
1. All the task's attributes are set.
Think of attributes as properties for a task. Ant delivers the values to a task object by
calling the setXXX( ) method for every attribute, where XXX is the capitalized name
of the attribute. If a set method is missing, Ant errors out and the task and build fail.
90