我有一个从中读取环境属性的ant任务
myproject.properties
。环境属性值设置为
prod
并显示“Prod条件为真”。我看到
${environment}
变量设置为prod,但如果condition永远不为真。能够
有人解释原因吗?
myproject.properties:
environment=prod
的build.xml:
<project name="my-project" default="run" basedir=".">
<property file="myproject.properties" />
<target name="run">
<echo message="running target run ${environment}"/>
<if>
<equals arg1="${environment}" arg2="prod">
<then>
<echo message="Prod condition is true"/>
<!--do prod environment specific task-->
</then>
</if>
</target>
</project>
答案 0 :(得分:2)
除了你的equals
任务缺少一个结束标记(实际上它应该是一个自动关闭的标签)这一事实,我愿意打赌你有一个隐藏在某个地方的空白。在echo
中,使用撇号或其他内容包围属性的打印输出:
<echo message="running target run '${environment}'"/>
你可能会在值的末尾看到一个空格。这是我能想到的唯一合理的解释。或者,尝试使用-Denvironment=prod
运行,看看会发生什么。
答案 1 :(得分:1)
以下解决方案使用核心ANT。它避免使用ant-contrib扩展名提供的“if”任务。
<project name="my-project" default="run" basedir=".">
<property file="myproject.properties" />
<condition property="prod.set">
<equals arg1="${environment}" arg2="prod"/>
</condition>
<target name="run" if="prod.set">
<echo message="Prod condition is true"/>
</target>
</project>
答案 2 :(得分:0)
希望你能做到这一点,但只是提醒一下,你是否包含了antcontrib jar并在project.xml中放置了相关的taskdef?如果没有,请更正构建文件,并将ant contrib jar复制到类路径
<project name="SampleWS" default="run" basedir=".">
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="lib/ant-contrib-0.6.jar" onerror="ignore"/>
<property file="myproject.properties" />
<target name="run">
<echo message="running target run ${environment}"/>
<if>
<equals arg1="${environment}" arg2="prod"/>
<then>
<echo message="Prod condition is true"/>
<!--do prod environment specific task-->
</then>
</if>
</target>
</project>