ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Denner <m...@evtechnology.com>
Subject conditional logic and making it nicely extensible
Date Thu, 06 Sep 2001 11:20:06 GMT
Dear all,

my first mail to this so if i'm out of order or in the wrong place then bash
me know :)

i have been given the task of maintaining and upgrading our Ant build system.
after spending a few days separating out generic tasks into nice files and
writing a few new Ant tasks i have come to a point where i really want to
get down into the Ant code and make (what i think are) some valuable changes.

i have written a couple of neat
org.apache.tools.ant.taskdefs.condition.Condition derived classes:

* IsPropertySet (that will return true from eval() if the property is set)
* IsTargetDefined (that returns true from eval() if the target is defined
   in the project).

around these i've written a class called ConditionWrapper that allows me to
extend the condition logic in Ant without having to change the code.  so, in
my project i might write:

<conditionwrapper class="com.evtechnology.ant.IsPropertySet">
   <param name="property" value="build.this.now"/>
</conditionwrapper>

to get a condition that, if the 'build.this.now' property is set, will do
something.  i even updated the ConditionBase class to access an instance of
ConditionWrapper so you can do things like <not><conditionwrapper...></not>.

i then had a look at CallTarget (which is the <antcall> element) and made
that have a conditional bit so that i could do:

<antcall target="build.something">
   <conditionwrapper class="com.evtechnology.ant.IsTargetDefined">
     <param name="target" value="build.something"/>
   </conditionwrapper>
</antcall>

so that the call to the 'build.something' target will only occur if the
target exists.

suddenly it dawned on me that what i really wanted was something that would
allow me to execute different tasks based on a generic condition.  something
like:

<conditionaltask>
   <conditionwrapper class="com.evtechnology.ant.IsTargetDefined">
     <param name="target" value="build.something"/>
   </conditionwrapper>
   <success>
     <antcall target="build.something"/>
   </success>
   <failure>
     <echo message="Build target 'build.something' doesn't exist"/>
   </failure>
</conditionaltask>

which would, if the condition passed (which could be any of the Condition
classes) execute the <success> actions, and if it failed would execute the
<failure> actions.  something like:

<conditionaltask>
   <available file="xyz.txt"/>
   <success><property name="file.there" value="true"/></success>
   <failure/>
</conditionaltask>

is exactly the same as doing the simple <available> but would offer the
ability to set the value of 'file.there' to something if the file is
available and something different if it isn't.

[ jees, will this guy ever get to the point? ed :) ]

finally, here's why this whole message was started: rather than have to
create a ConditionalTask class that will have addOr(), addNot(), addXYZ()
methods, i'd rather try to make ant handle an addCondition() generically.
so my ConditionalTask might look like:

public final class ConditionalTask {
   public void addCondition(final Condition condition) ...
}

ant addCondition() would be called even if i wrote:

<conditionaltask>
   <not>...</not>
</conditionaltask>

or:

<conditionaltask>
   <and>...</and>
</conditionaltask>

or any of the org.apache.tools.ant.taskdefs.condition.Condition derived
classes.

AFAIK, right now this isn't possible.  i have managed to hack a bit at the
Ant code to handle this special case but i end up in all sorts of mess and
generally get NullPointerExceptions all over the shop.  is anyone looking
at doing / done similar stuff?  does anyone have any guidance for what bits
of the Ant code i should be looking at if i want to handle generic methods
like addCondition() or addTask()?

i'd appreciate all help.

Cheers,
Matt

p.s. i've got several "bug" fixes and stuff that i'd like to contribute back
      to the source.  who do i talk to about the changes i've made (like
      making <mapper> elements work across different directories)?

-----------------------------------------------------------------------------
Sessami is a trademark of Escape Velocity Technology Mobile Services Limited.
All information contained in this e-mail is confidential and for the use of
the addressee only.  If you receive this message in error please notify.


Mime
View raw message