Search code examples
if-statementantconditional-statements

How to have multiple if cases in a condition statement in Ant


Like in a switch statement, I want to set different values in the different cases

I can have only one condition and set only two different values in this conditional statement:

<condition property="prop" value="1" else="2">
   <equals arg1="${param1}" arg2="one"/>
</condition>

And once set, the prop is immutable so I can't change its value.

What I need is something like this

if (param1 == "one") {
   prop = 1;
} else if (param1 == "two") {
   prop = 2;
} else if (param1 == "three") {
   prop = 3;
} ...

Solution

  • You can use separate conditions and rely on the fact that properties are immutable

    <condition property="prop" value="1">
         <equals arg1="${param1}" arg2="one"/>
    </condition>
    <condition property="prop" value="2">
        <equals arg1="${param1}" arg2="two"/>
    </condition>
    ...
    

    and add a regular property for the final else

    <property name="prop" value="else">