我在蚂蚁一个新手。 我的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>
我的逻辑。 能有人帮帮我好吗?
您可以创建几个目标和使用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>
与目标-
前缀是私人所以用户将无法从命令行中运行它们。
如果你们需要一个普通的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>