我基本上是在Ant(v1.9.4)中尝试做以下事情:
我有一个固定字符串列表,如{a,b,c,d} - >首先,我应该如何在Ant中声明这一点? 然后我有一个输入参数,如$ {mystring},我想检查变量值是否在我的列表中。这意味着在此示例中,如果变量值等于a或b或c或d。 如果是这样,则返回true,否则返回false(或0和1之类的东西)。
有一种简单的方法吗?
谢谢,
蒂亚戈
答案 0 :(得分:3)
使用ant property task声明您的字符串列表 使用ant contains condition检查列表是否包含特定项目 像:
<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>
或者将其换成macrodef以便重新使用:
<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>
- 编辑 -
来自ant manual condition:
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.
所以简单地使用一个条件来创建一个true或not set的属性,f.e。结合the new if/unless feature introduced with Ant 1.9.1:
<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功能。