我有一组目标,每个目标基本上都做同样的事情,除了每个都包含一个特定的模式集来执行其任务。我想将这些目标折叠成一个“可重用”目标,而不是将一组文件“作为参数”。
例如,这个
<target name="echo1">
<foreach item="File" property="fn">
<in>
<items>
<include name="*.config"/>
</items>
</in>
<do>
<echo message="${fn}" />
</do>
</foreach>
</target>
<target name="echo2">
<foreach item="File" property="fn">
<in>
<items>
<include name="*.xml"/>
</items>
</in>
<do>
<echo message="${fn}" />
</do>
</foreach>
</target>
<target name="use">
<call target="echo1"/>
<call target="echo2"/>
</target>
将被替换为
<patternset id="configs">
<include name="*.config"/>
</patternset>
<patternset id="xmls">
<include name="*.xml"/>
</patternset>
<target name="echo">
<foreach item="File" property="fn">
<in>
<items>
<patternset refid="${sourcefiles}"/>
</items>
</in>
<do>
<echo message="${fn}" />
</do>
</foreach>
</target>
<target name="use">
<property name="sourcefiles" value="configs"/>
<call target="echo"/>
<property name="sourcefiles" value="xmls"/>
<call target="echo"/>
</target>
但是,事实证明,由于模式集和文件集与属性不同,因此并未像nant-dev 电子邮件发布refid
中所回答的那样扩展。在这个非工作代码中,当被调用时,它的元素引用了一个字面上名为${sourcefiles}的模式集,而不是名为test的模式集。echo
patternset
如何编写一个可重用的 NAnt 目标来对一组不同的文件进行操作?有没有办法在 NAnt 中按原样执行此操作而无需编写自定义任务?