Hi all,

I suppose that there have been a lot of discussion about conditional structures, but I would like to open another one.
For now, I notice two solutions :

>> solution 1 : ant approach, using if and unless parameters.

   /<target name="todo" depends="ok,ko"/>/
   /<target name="ok" if="my-property-is-set"/>/
   /<target name="ko" unless="my-property-is-NOT-set"/>/

Disadvantages :
- not as simple as it should be
- property evaluation is not possible (or I don't know how to do this)

>> solution 2 : ant-contrib

   /<if>/
   /    <equals arg1="..." arg2="..."/>/
   /</if>/

Disadvantages :
- verbose solution
- project still maintain ?
- solution not integrated in the ant project

>> solution 3
I would like to discuss about another solution, based upon <antcall> et <condition> tasks.

   /<antcall target="todo">/
   /    <condition>/
   /        <equals arg1="${val1}" arg2="//${val2}//"/>/
   /    </condition>/
   /</antcall>/

Target would be executed if condition succeeded.

Advantages :
+ use an existing task : <antcall> by adding a nested element
=> quiet easy to implement (see attached file)
+ use all <condition> possibilities

What do you think about this idea ?

Best regards,
Benjamin
package net.sourceforge.ant4hg.contrib;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.CallTarget;
import org.apache.tools.ant.taskdefs.condition.Condition;
import org.apache.tools.ant.taskdefs.condition.ConditionBase;

public class CallTarget2 extends CallTarget {

    // //////////////////////////////////////////////
    // INNER CLASS
    // //////////////////////////////////////////////
    /**
     * @see org.apache.tools.ant.taskdefs.Exit
     */
    private static class NestedCondition extends ConditionBase implements Condition {
        public boolean eval() {
            if (countConditions() != 1) {
                throw new BuildException("A single nested condition is required.");
            }
            return ((Condition) (getConditions().nextElement())).eval();
        }
    }

    // //////////////////////////////////////////////
    // ATTRIBUTES
    // //////////////////////////////////////////////
    /**
     * @see org.apache.tools.ant.taskdefs.Exit
     */
    private NestedCondition nestedCondition;

    /**
     * @see org.apache.tools.ant.taskdefs.Exit
     */
    public ConditionBase createCondition() {
        if (nestedCondition != null) {
            throw new BuildException("Only one nested condition is allowed.");
        }
        nestedCondition = new NestedCondition();
        return nestedCondition;
    }

    // //////////////////////////////////////////////
    // CONSTRUCTORS
    // //////////////////////////////////////////////
    public CallTarget2() {
        super();
        setTaskName("antcall");
    }

    // //////////////////////////////////////////////
    // OVERRIDEN METHODS
    // //////////////////////////////////////////////
    public void execute() throws BuildException {
        if (!nestedCondition.eval()) {
            return;
        }
        super.execute();
    }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@ant.apache.org
For additional commands, e-mail: dev-h...@ant.apache.org

Reply via email to