我无法弄清楚如何设置一个Ant属性,条件是它尚未设置(即它没有在属性文件中定义,应该自动默认).
到目前为止,我只有以下代码:
<condition property="core.bin" value="../bin">
<isset property="core.bin"/>
</condition>
Run Code Online (Sandbox Code Playgroud)
但是,只有在<property>
标签中定义了值时,这似乎才有效.
如果当前未设置,有没有人知道如何有条件地设置属性?
Mne*_*nth 113
您只需使用property-task设置属性即可.如果已设置该属性,则该值不会更改,因为属性是不可变的.
但你也可以在你的情况中加入'not':
<condition property="core.bin" value="../bin">
<not>
<isset property="core.bin"/>
</not>
</condition>
Run Code Online (Sandbox Code Playgroud)
And*_*nch 63
Ant默认执行此操作; 如果该物业已经设定; 再次设置无效:
<project name="demo" default="demo">
<target name="demo" >
<property name="aProperty" value="foo" />
<property name="aProperty" value="bar" /> <!-- already defined; no effect -->
<echo message="Property value is '${aProperty}'" /> <!-- Displays 'foo' -->
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
给
/c/scratch> ant -f build.xml
Buildfile: build.xml
demo:
[echo] Property value is '${aProperty}'
BUILD SUCCESSFUL
Total time: 0 seconds
/c/scratch> ant -f build.xml
Buildfile: build.xml
demo:
[echo] Property value is 'foo'
BUILD SUCCESSFUL
Run Code Online (Sandbox Code Playgroud)
属性不能重新定义; 要做到这一点,你需要使用像ant-contrib这样的变量任务.
小智 6
做你想做的最简单的方法:
<if>
<not>
<isset property="your.property"/>
</not>
<then>
<property name="your.property" value="your.value"/>
</then>
</if>
Run Code Online (Sandbox Code Playgroud)
小智 5
支持在https://ant.apache.org/manual/Tasks/condition.html中使用“else”来满足您的确切目的。
别的
The value to set the property to if the condition evaluates to false. By default the property will remain unset. Since Apache Ant 1.6.3
Run Code Online (Sandbox Code Playgroud)
所以更改为:
<condition property="core.bin" else="../bin">
<isset property="core.bin"/>
</condition>
Run Code Online (Sandbox Code Playgroud)