使用纯Ant实现if else条件(检查命令行输入)

joh*_*123 15 ant

我是Ant的新手.我的ant脚本从命令行接收名为" env " 的用户输入变量:

例如 ant doIt -Denv=test

用户输入值可以是" test "," dev "或" prod ".

我也有" doIt"目标:

<target name="doIt">
  //What to do here?
</target>
Run Code Online (Sandbox Code Playgroud)

在我的目标中,我想为我的ant脚本创建以下if else条件:

if(env == "test")
  echo "test"
else if(env == "prod")
  echo "prod"
else if(env == "dev")
  echo "dev"
else
  echo "You have to input env"
Run Code Online (Sandbox Code Playgroud)

那是检查用户从命令行输入的值,然后相应地打印一条消息.

我知道使用ant-Contrib,我可以编写ant脚本<if> <else>.但对于我的项目,我想使用纯Ant来实现if else条件.可能,我应该使用<condition>?? 但我不确定如何使用<condition>我的逻辑.有人可以帮我吗?

pep*_*uch 43

您可以创建一些目标并使用if/ unless标记.

<project name="if.test" default="doIt">

    <target name="doIt" depends="-doIt.init, -test, -prod, -dev, -else"></target>

    <target name="-doIt.init">
        <condition property="do.test">
            <equals arg1="${env}" arg2="test" />
        </condition>
        <condition property="do.prod">
            <equals arg1="${env}" arg2="prod" />
        </condition>
        <condition property="do.dev">
            <equals arg1="${env}" arg2="dev" />
        </condition>
        <condition property="do.else">
            <not>
                <or>
                <equals arg1="${env}" arg2="test" />
                <equals arg1="${env}" arg2="prod" />
                <equals arg1="${env}" arg2="dev" />
                </or>
            </not>
        </condition>
    </target>

    <target name="-test" if="do.test">
        <echo>this target will be called only when property $${do.test} is set</echo>
    </target>

    <target name="-prod" if="do.prod">
        <echo>this target will be called only when property $${do.prod} is set</echo>
    </target>

    <target name="-dev" if="do.dev">
        <echo>this target will be called only when property $${do.dev} is set</echo>
    </target>

    <target name="-else" if="do.else">
        <echo>this target will be called only when property $${env} does not equal test/prod/dev</echo>
    </target>

</project>
Run Code Online (Sandbox Code Playgroud)

带-前缀的目标是私有的,因此用户将无法从命令行运行它们.