使用纯Ant来实现,如果其他条件(检查命令行输入)(Use pure Ant to implemen

2019-07-21 12:49发布

我在蚂蚁一个新手。 我的ant脚本接收命令行命名为“ENV”用户输入变量:

例如ant doIt -Denv=test

所述用户输入的值可以是“ 测试 ”,“dev的 ‘或’ ”。

我也有“ doIt ”目标:

<target name="doIt">
  //What to do here?
</target>

在我的目标,我想创建以下如果我的ant脚本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"

这就是要检查哪个值用户已经从命令行输入,则相应地打印的消息。

我知道有蚂蚁的Contrib,我可以写与Ant脚本<if> <else>对于我的项目,我想用纯蚂蚁,如果其他条件来实现。 大概,我应该使用<condition> ? 但我不知道如何使用<condition>我的逻辑。 能有人帮帮我好吗?

Answer 1:

您可以创建几个目标和使用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>

与目标-前缀是私人所以用户将无法从命令行中运行它们。



Answer 2:

如果你们需要一个普通的if / else条件(无ELSEIF); 然后使用下面:

在这里,我依赖于环境变量DMAPM_BUILD_VER,但它可能会发生这个变量不会在ENV设置。 所以我需要有机制默认为本地值。

    <!-- Read build.version value from env variable DMAPM_BUILD_VER. If it is not set, take default.build.version. -->
    <property name="default.build.version" value="0.1.0.0" />
    <condition property="build.version" value="${env.DMAPM_BUILD_VER}" else="${default.build.version}">
        <isset property="env.DMAPM_BUILD_VER"/>
    </condition>


文章来源: Use pure Ant to implement if else condition (check command line input)
标签: ant