使用纯 Ant 实现 if else 条件(检查命令行输入) [英] Use pure Ant to implement if else condition (check command line input)

查看:27
本文介绍了使用纯 Ant 实现 if else 条件(检查命令行输入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a newbie in Ant. My ant script receives a user input variable named "env" from command line:

例如ant doIt -Denv=test

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

The user input value could be "test","dev" or "prod".

我也有doIt"目标:

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

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

In my target, I would like to create the following if else condition for my ant script:

if(env == "test")
  echo "test"
else if(env == "prod")
  echo "prod"
else if(env == "dev")
  echo "dev"
else
  echo "You have to input env"

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

That's to check which value user has inputted from command line, then, print a message accordingly.

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

I know with ant-Contrib, I can write ant script with <if> <else> . But for my project, I would like to use pure Ant to implement if else condition. Probably, I should use <condition> ?? But I am not sure how to use <condition> for my logic. Could some one help me please?

推荐答案

您可以创建很少的目标并使用 if/unless 标签.

You can create few targets and use if/unless tags.

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

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

Targets with - prefix are private so user won't be able to run them from command line.

这篇关于使用纯 Ant 实现 if else 条件(检查命令行输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆