文件集可以使用 nested include/exlucde patternsets 和 if/unless
属性。该模式在设置或不设置命名属性时包含/排除,因此不需要 ant 插件。
一些 sn-p :
<project>
<!-- property that triggers your include/exclude
maybe set via condition in some other target .. -->
<property name="foo" value="bar"/>
<macrodef name="pkgmacro">
<attribute name="myattr" />
<sequential>
<fileset dir="C:/whatever" id="foobar">
<!-- alternatively
<include name="*.bat" unless="@{myattr}"/>
-->
<include name="*.bat" if="@{myattr}"/>
</fileset>
<!-- print fileset contents -->
<echo>${toString:foobar}</echo>
</sequential>
</macrodef>
<pkgmacro myattr="foo"/>
</project>
--评论后编辑--
include name/exclude name 之后的 if/unless attribute 检查给定值是否是在 ant 项目范围内设置(使用 if=".." 时)或未设置(使用 unless=".." 时)的属性- 它不检查特定值。
<include name="*.xml" unless="foo"/>
表示仅当 no 在您的项目中设置了名为 foo 的属性时,include 才有效
<include name="*.xml" if="foo"/>
意味着 include 只有在您的项目中设置了名为 foo 的属性时才处于活动状态
对我来说很好用,使用 Ant 1.7.1,现在没有 Ant 1.8.x:
<project>
<echo>$${ant.version} => ${ant.version}</echo>
<macrodef name="pkgmacro">
<attribute name="myattr"/>
<sequential>
<condition property="pass">
<equals arg1="@{myattr}" arg2="xyz" />
</condition>
<fileset dir="C:/whatever" id="foobar">
<include name="*.bat" if="pass" />
</fileset>
<echo>${toString:foobar}</echo>
</sequential>
</macrodef>
<pkgmacro myattr="xyz"/>
</project>
输出:
[echo] ${ant.version} => Apache Ant version 1.7.1 compiled on June 27 2008
[echo] switchant.bat;foobar.bat;foo.bat
-- 评论后编辑--
使用多个包含模式可以正常工作:
...
<fileset dir="C:/whatever" id="foobar">
<include name="*.xml" if="pass" />
<include name="*.bat" if="pass"/>
<include name="**/*.txt" if="pass"/>
</fileset>
...
也许你使用了错误的模式?
-- 评论后编辑--
Here is the reference 到 <local> 需要使用当前范围内的属性的任务,在本例中为 <sequential>:
<macrodef name="pkgmacro">
<attribute name="myattr"/>
<sequential>
<!-- make property pass mutable -->
<local name="pass"/>
<condition property="pass">
<equals arg1="@{myattr}" arg2="xyz" />
</condition>
<fileset dir="C:/whatever" id="foobar">
<include name="*.xml" if="pass" />
<include name="*.bat" if="pass" />
<include name="**/*.txt" if="pass" />
</fileset>
<!-- print value of property pass and fileset contents -->
<echo>$${pass} = ${pass}${line.separator}${toString:foobar}</echo>
</sequential>
</macrodef>