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

☆樱花仙子☆ 提交于 2019-12-21 21:44:05

问题


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>


来源:https://stackoverflow.com/questions/15912976/how-to-emulate-if-elseif-else-in-ant-without-using-ant-contrib

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!