Download O`Reilly - Ant The Definitive Guide

Transcript
Ant: The Definitive Guide
that, for example, compile mutually exclusive sets of code or create unrelated directories can
benefit from threading these operations so that they are run simultaneously. Users with
multiple-CPU systems see performance benefits from parallelizing such tasks. Another
benefit of parallelization is for those people who wish to run unit tests against application
servers. Their application servers and tests must run simultaneously, which was not easily
done in Ant before Version 1.4. Unfortunately, for those who write or have written custom
build listeners, parallelization can break their previously working code.
Some build event listeners rely upon certain events occurring in a particular order. For
example, if a listener expects to see a taskFinished( ) event after the taskStarted( )
event for the javac task, the listener would fail or act strangely if two javac tasks were run in
parallel. The second javac may end before the first. Listener code, while watching for the
event saying Ant is finished with the second javac task, may prematurely trigger operations
intended for the first javac task, or vice versa. Consequently, the output from, or operations
of, the listener would be wrong, possibly leading to further problems. If you're ever given a
buildfile using the parallel task, it's best to test your custom listeners to see whether
nonsequential behavior is okay.
XmlLogger is a good example of a listener that handles tasks run in parallel. Let's look at an
execution flow in which XmlLogger listens to the following set of operations from a buildfile:
<parallel>
<copy todir="test">
<fileset dir=".\irssibot-1.0.4" includes="**/*.java"/>
</copy>
<mkdir dir="testxml"/>
<mkdir dir="testxml2"/>
<copy todir="test">
<fileset dir=".\oak-0.99.17" includes="**/*.java"/>
</copy>
<mkdir dir="testxml3"/>
</parallel>
Let's assume that the engine, being multithreaded, executes the tasks such that they complete
in the following order:
1.
2.
3.
4.
5.
MKDIR(TESTXML)
MKDIR(TESTXML2)
MKDIR(TESTXML3)
COPY(irssibot)
COPY(oak)
Because it was written to handle out-of-order events, XmlLogger's resulting XML markup
does not output any elements out of order. The tasks' markup appears in the order listed
above, with their nested elements intact. While there is no "right" way to write a
multithreaded aware listener, XmlLogger shows that some clever foresight in design can
thwart future catastrophes. This foresight makes a listener long-lived, even with the
possibility of future dramatic changes in the task library.
116