Ant-String包含在字符串数组中
我基本上是在Ant(v1.9.4)中尝试做以下事情: 我有一个固定字符串列表,比如{a,b,c,d}-->首先我应该如何在Ant中声明它? 然后我有一个输入参数,比如${mystring},我想检查变量值是否在我的列表中。这意味着在本例中,如果变量值等于a或b或c或d。 如果是,则返回true,否则返回false(或0和1类似的值) 有没有一个简单的方法可以做到这一点 谢谢 Thiago使用ant声明您的字符串列表。Ant-String包含在字符串数组中,ant,Ant,我基本上是在Ant(v1.9.4)中尝试做以下事情: 我有一个固定字符串列表,比如{a,b,c,d}-->首先我应该如何在Ant中声明它? 然后我有一个输入参数,比如${mystring},我想检查变量值是否在我的列表中。这意味着在本例中,如果变量值等于a或b或c或d。 如果是,则返回true,否则返回false(或0和1类似的值) 有没有一个简单的方法可以做到这一点 谢谢 Thiago使用ant声明您的字符串列表。 使用ant检查列表是否包含特定项。 类似于: <project>
使用ant检查列表是否包含特定项。
类似于:
<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>
因此,只需使用一个条件来创建一个属性,该属性要么为真,要么未设置,例如与:请注意,您需要名称空间声明来激活if/除非功能。非常感谢!我肯定会使用macrodef。但是,如果输入字符串不在列表中,我不希望它失败,而只是返回一个布尔变量。可能吗?非常感谢你!实际上,我想在宏之外使用这个“itemfound”布尔属性。就像在另一个任务中一样,验证它的值,如果为真,则进行一些特殊处理,如果为假,则进行另一个处理。那可能吗?谢谢如果不需要将条件与macrodef中的其他内容组合,可以简单地使用condition standalone。条件设置的属性可用于任何目标。此外,macrodef示例中的属性${itemfound}也可以在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>
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.
<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 !!