ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominique Devienne <>
Subject <condition> inside tasks!!!
Date Mon, 22 Jul 2002 00:09:44 GMT
I guess it must be my hacking weekend... How many times do you wish there
was an <if> task to simplify your build files? Well, one can use the <if>
container task from ant-contrib I guess. Or one rolls one's own. How to do
that so all (most) tasks support conditional execution? Well, simply add to
the Task base class a <condition> datatype (or <conditional> to avoid
confusion with the <condition> task), and check that condition in
Task.perform() to not execute (Task.execute) the task when the condition
evaluates to false. It's trivial to implement (I just did). And all tasks
deriving from Task get it for free.

IMHO, it's not scripty, but very declarative. It's similar to all those
if/unless/os attributes manually added to many tasks (but not all!), except
it's cleaner and general (so has to be documented only once too).


#1: I guess some custom tasks could already have a 'condition' or
'conditional' sub-element. None of the core ant task do. The ones from
optional I compile don't either. I think the benefits outweigh potential
custom task out there conflicting with it.

#2: Tasks using addText(String) get more lines because of the nested
<condition> sub-element. That could be alleviated by addition of a standard
'conditionref' attribute to Task, and the <condition> be defined outside the
task, and simply referenced.

The current <condition> task should have been a datatype in the first place,
and used as a sub-element of <property> to set a property when condition is
satisfied, instead of the other way around.

Personally, I think all tasks and maybe even datatypes should support a
<condition> sub-element.

Below is a little example:

Buildfile: build.xml


Total time: 0 seconds
P:\org_apache\jakarta-ant-ANT_15_BRANCH\dist>ant -Drunecho=true
Buildfile: build.xml

     [echo] task = echo

Total time: 0 seconds
P:\org_apache\jakarta-ant-ANT_15_BRANCH\dist>type build.xml
<?xml version="1.0"?>

<!-- ANT build file to test a specific feature or bug of ANT.
     Dominique Devienne <>       April 2002
<project name="antx" default="test" basedir=".">

  <target name="test">
    <echo message="task = echo">
        <isset property="runecho"/>



Somehow I don't think this is ever gonna make it to Ant, even though it
allows writing Ant build files which look less like spaghetti-target/code...

View raw message