-->

How to emulate if-elseif-else in Ant without using

2019-04-15 01:33发布

问题:

I need an if-elseif-else conditional statement in Ant.

I do not want to use Ant-contrib.

I tried the solution here

    <target name="condition.check">
    <input message="Please enter something: " addproperty="somethingProp"/>
    <condition property="allIsWellBool">
        <not>
            <equals arg1="${somethingProp}" arg2="" trim="true"/>
        </not>
    </condition>
</target>
<target name="if" depends="condition.check, else" if="allIsWellBool">
    <echo message="if condition executes here"/>
</target>
<target name="else" depends="condition.check" unless="allIsWellBool">
    <echo message="else condition executes here"/>
</target>

But I will have to set properties inside the if and else targets which will not be visible in the calling target.

Is there any other way out using conditions?

回答1:

Move the dependencies out of if and else into a new target that depends on all of the other targets:

<project name="ant-if-else" default="newTarget">
    <target name="newTarget" depends="condition.check, if, else"/>

    <target name="condition.check">
        <input message="Please enter something: " addproperty="somethingProp"/>
        <condition property="allIsWellBool">
            <not>
                <equals arg1="${somethingProp}" arg2="" trim="true"/>
            </not>
        </condition>
    </target>

    <target name="if" if="allIsWellBool">
        <echo message="if condition executes here"/>
    </target>
    <target name="else" unless="allIsWellBool">
        <echo message="else condition executes here"/>
    </target>
</project>