2

我需要 Ant 中的 if-elseif-else 条件语句。

我不想使用 Ant-contrib。

我在这里尝试了解决方案

    <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>

但是我必须在 if 和 else 目标中设置属性,这些属性在调用目标中不可见。

使用条件还有其他出路吗?

4

1 回答 1

4

将依赖项移出ifelse移入依赖于所有其他目标的新目标:

<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>
于 2013-04-10T19:56:17.217 回答