我基本上尝试在Ant (v1.9.4)中做以下事情:
我有一个固定字符串列表,如{a,b,c,d} ->首先我应该如何在Ant中声明它?然后我有一个输入参数,如${mystring},我想检查变量值是否在我的列表中。也就是说,在这个例子中,如果变量值等于a或b或c或d。如果是,则返回true,否则返回false(或0和1类似)。
有简单的方法吗?
谢谢,
蒂亚戈
使用ant属性任务来声明stringlist
使用ant contains条件检查list是否包含特定项。
例如:
<project>
<!-- your stringlist -->
<property name="csvprop" value="foo,bar,foobar"/>
<!-- fail if 'foobaz' is missing -->
<fail message="foobaz not in List => [${csvprop}]">
<condition>
<not>
<contains string="${csvprop}" substring="foobaz"/>
</not>
</condition>
</fail>
</project>
<project>
<!-- your stringlist -->
<property name="csvprop" value="foo,bar,foobar"/>
<!-- create macrodef -->
<macrodef name="listcontains">
<attribute name="list"/>
<attribute name="item"/>
<sequential>
<fail message="@{item} not in List => [@{list}]">
<condition>
<not>
<contains string="${csvprop}" substring="foobaz"/>
</not>
</condition>
</fail>
</sequential>
</macrodef>
<!-- use macrodef -->
<listcontains item="foobaz" list="${csvprop}"/>
</project>
—EDIT—
从ant手动条件:
If the condition holds true, the property value is set to true by default; otherwise, the property is not set. You can set the value to something other than the default by specifying the value attribute.
因此,只需使用一个条件来创建一个属性,该属性要么为真,要么为未设置,例如结合Ant 1.9.1引入的if/unless新特性:<project
xmlns:if="ant:if"
xmlns:unless="ant:unless"
>
<!-- your stringlist -->
<property name="csvprop" value="foo,bar,foobar"/>
<!-- create macrodef -->
<macrodef name="listcontains">
<attribute name="list"/>
<attribute name="item"/>
<sequential>
<condition property="itemfound">
<contains string="${csvprop}" substring="foobaz"/>
</condition>
<!-- echo as example only instead of
your real stuff -->
<echo if:true="${itemfound}">Item @{item} found => OK !!</echo>
<echo unless:true="${itemfound}">Warning => Item @{item} not found !!</echo>
</sequential>
</macrodef>
<!-- use macrodef -->
<listcontains item="foobaz" list="${csvprop}"/>
</project>
输出:
[echo] Warning => Item foobaz not found !!
注意,您需要名称空间声明来激活if/unless特性。