2013-03-13 31 views
14

我在蚂蚁一个新手。我的ant脚本从命令行接收名为“env”的用户输入变量:使用纯Ant来实现,如果其他条件(检查命令行输入)

例如, ant doIt -Denv=test

用户输入值可以是 “测试”, “dev的 ”或“ PROD”。

我也有 “doIt” 目标:

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

在我的目标,我想创建以下如果其他条件我ant脚本:

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

这是对检查用户从命令行输入的值,然后相应地打印消息。

我知道ant-Contrib,我可以用<if> <else>写蚂蚁脚本。 我的项目,我想用纯蚂蚁,如果其他条件实施。可能,我应该使用<condition> ??但我不知道如何使用<condition>我的逻辑。请问有人能帮我吗?

+1

如果您是Ant的新手,请考虑使用[Gradle](http://www.gradle.org/)。这很容易。 – 2013-03-13 23:59:38

回答

42

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

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

+0

感谢这些有价值的信息(与目标 - 前缀是私人所以用户将无法从命令行中运行它们。) – ipingu 2014-03-21 19:52:52

1

如果您有任何人需要明文if/else condition(without 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>