文件集/模式集的 refid 属性未扩展。您将如何编写一个对任意文件集进行操作的目标?
-
27-09-2019 - |
题
我有一组目标,每个目标本质上都做相同的事情,除了每个目标都包含一个特定的 模式集 其上执行其任务。我想将这些目标折叠成单个“可重用”目标,该目标将一组文件“作为参数”。
例如,这个
<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>
然而事实证明 refid
没有按照 a 中的回答进行扩展 nant-dev 电子邮件发布 因为模式集和文件集的属性不同。在这段非工作代码中,当 echo
被称为,其 patternset
元素引用一个字面上命名的模式集 ${源文件} 而不是那个名为 测试.
如何编写一个可重用的 NAnt 目标来运行一组不同的文件?有没有办法在 NAnt 中按原样执行此操作,而无需编写自定义任务?
解决方案
我终于想出了这个,这符合我的目的。作为奖励,这还演示了动态调用目标。
<project
name="dynamic-fileset"
default="use"
xmlns="http://nant.sourceforge.net/release/0.86-beta1/nant.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<target name="configs">
<fileset id="files">
<include name="*.config"/>
</fileset>
</target>
<target name="xmls">
<fileset id="files">
<include name="*.xml"/>
</fileset>
</target>
<target name="echo">
<foreach item="File" property="fn">
<in>
<items refid="files"/>
</in>
<do>
<echo message="${fn}" />
</do>
</foreach>
</target>
<target name="use">
<property name="grouplist" value="xmls,configs"/>
<foreach item="String" in="${grouplist}" delim="," property="filegroup">
<do>
<call target="${filegroup}"/>
<call target="echo"/>
</do>
</foreach>
</target>
</project>
其他提示
我不确定我完全理解你想要实现的目标,但不应该归因 dynamic
的 任务 property
做这份工作吗?
<target name="filesettest">
<property name="sourcefiles" value="test" dynamic="true" />
<!-- ... -->
</target>
不隶属于 StackOverflow