如何检查属性在 Ant 中是否有价值 [英] How to check if a property has value in Ant
问题描述
我有一个用于构建的 Ant XML 文件.
I have an Ant XML file which I use for build.
我有 3 个属性.如果这些属性不包含任何值,我想中断构建.如果值为空,我也想中断构建.
I have 3 properties. I want to break the build if these properties does not contain any value. Also I want to break the build if the value is empty.
如何在 Ant 中执行此操作?
How can I do this in Ant?
我使用 Ant 而不是 Ant-contrib.
I a using Ant and not Ant-contrib.
推荐答案
您可以通过
任务使用条件:
You can use conditions using the <fail>
task:
<fail message="Property "foo" needs to be set to a value">
<condition>
<or>
<equals arg1="${foo}" arg2=""/>
<not>
<isset property="foo"/>
</not>
</or>
</condition>
这相当于说if (not set ${foo} or ${foo} = "")
是伪代码.您必须从内到外读取 XML 条件.
This is equivalent to saying if (not set ${foo} or ${foo} = "")
is pseudocode. You have to read the XML conditions from the inside out.
如果您只关心是否设置了变量,而不关心它是否已设置,则您可以在 <fail>
任务中使用
You could have used the <unless>
clause on the <fail>
task if you only cared whether or not the variable was set, and not whether it has an actual value.
<fail message="Property "foo" needs to be set"
unless="foo"/>
但是,如果设置了属性,但没有值,这不会失败.
However, this won't fail if the property is set, but has no value.
有一个技巧可以使这更简单
There's a trick that can make this simpler
<!-- Won't change the value of `${foo}` if it's already defined -->
<property name="foo" value=""/>
<fail message="Property "foo" has no value">
<condition>
<equals arg1="${foo}" arg2=""/>
</condition>
</fail>
请记住,我无法重置属性!如果 ${foo}
已经有一个值,上面的
任务将不会做任何事情.这样,我可以消除
条件.这可能很好,因为您拥有三个属性:
Remember that I can't reset a property! If ${foo}
already has a value, the <property>
task above won't do anything. This way, I can eliminate the <isset>
condition. It might be nice since you have three properties:
<property name="foo" value=""/>
<property name="bar" value=""/>
<property name="fubar" value=""/>
<fail message="You broke the build, you dufus">
<condition>
<or>
<equals arg1="${foo}" arg2=""/>
<equals arg1="${bar}" arg2=""/>
<equals arg1="${fubar}" arg2=""/>
</or>
</condition>
</fail>
这篇关于如何检查属性在 Ant 中是否有价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!