java 如何使用 ant 中的条件元素设置另一个属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6927607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use the condition element in ant to set another property?
提问by Pete855217
I have a build.xml to use with ant, and I'm trying to put a condition within a target:
我有一个 build.xml 与 ant 一起使用,我试图在目标中放置一个条件:
First I set the property here which works OK:
首先,我在这里设置了可以正常工作的属性:
<condition property="isWindows">
<os family="windows"/>
</condition>
Then I try to use it in the target:
然后我尝试在目标中使用它:
<target name="-post-jar">
<condition property="isWindows" value="true">
<!-- set this property, only if isWindows set -->
<property name="launch4j.dir" location="launch4j" />
</condition>
<!-- Continue doing things, regardless of property -->
<move file="${dist.jar.dir}" tofile="myFile"/>
<!-- etc -->
</target>
I'm getting an error: "condition doesn't support the nested "property" element." The questions are : How do I correctly put a condition inside a target and why is the error referring to a 'nested' property?
我收到错误消息:“条件不支持嵌套的“属性”元素。” 问题是:我如何正确地将条件放入目标中,为什么错误指的是“嵌套”属性?
采纳答案by JB Nizet
condition
is used to define a property, but not to execute some actions based on the value of a property.
condition
用于定义一个属性,而不是根据一个属性的值来执行一些动作。
Use a target with if
or unless
attributeto execute some tasks based on the value of the property.
使用带有if
或unless
属性的目标根据属性的值执行一些任务。
回答by Mads Hansen
The criteria for the conditionis nested inside of the condition
element.
为标准状态嵌套在内部condition
元件。
You specify the property that you want set using the property
attribute, and the value when the condition is met using the value
attribute on the condition
element. Additionally, you can set a value for the property the condition is not met with the else
attribute.
您可以使用属性指定要设置的property
属性,并使用元素value
上的属性指定满足条件时的值condition
。此外,您可以为该属性不满足条件的属性设置一个值else
。
To check whether or not a property is set as criteria for a condition
, use isset
要检查属性是否设置为 a 的条件condition
,请使用isset
<condition property="isWindows">
<os family="windows"/>
</condition>
<target name="-post-jar">
<!--Only set property if isWindows -->
<condition property="launch4j.dir" value="launch4j">
<isset property="isWindows"/>
</condition>
<!-- Continue doing things, regardless of property -->
<move file="${dist.jar.dir}" tofile="myFile"/>
<!-- etc -->
</target>