文件集/模式集的 refid 属性未扩展。您将如何编写一个对任意文件集进行操作的目标?

发布于 2024-09-25 17:15:22 字数 2099 浏览 6 评论 0 原文

我有一组目标,每个目标基本上执行相同的操作,除了每个目标都包含特定的 patternset 来执行其任务。我想将这些目标折叠成单个“可重用”目标,该目标将一组文件“作为参数”。

例如,这

<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 没有按照 nant-dev 电子邮件发布,因为模式集和文件集与属性不同。在此非工作代码中,当调用 echo 时,其 patternset 元素引用字面上名为 ${sourcefiles} 的模式集,而不是名为测试

如何编写一个可重用的 NAnt 目标来运行一组不同的文件?有没有办法在 NAnt 中按原样执行此操作,而无需编写自定义任务?

I have a set of targets that each do essentially the same thing except each contains a specific patternset on which to perform its tasks. I want to collapse these targets into a single "reusable" target that instead takes a set of files "as a parameter".

For example, this

<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>

would be replaced by

<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>

However, it turns out that refid is not expanded as answered in a nant-dev email posting because patternsets and filesets differ from properties. In this non-working code, when echo is called, its patternset element references a patternset literally named ${sourcefiles} instead of the one named test.

How would one write a re-usable NAnt target that operates on a varying set of files? Is there a way to do this in NAnt as-is without resorting to writing custom tasks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上芳菲 2024-10-02 17:15:22

我终于想出了这个,这符合我的目的。作为奖励,这还演示了动态调用目标。

<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>

I finally came up with this, which serves my purposes. As a bonus, this also demonstrates calling a target dynamically.

<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>
寂寞花火° 2024-10-02 17:15:22

我不确定我完全理解您想要实现的目标,但不应该将 dynamic 属性/tasks/property.html" rel="nofollow">任务 property 完成这项工作吗?

<target name="filesettest">
  <property name="sourcefiles" value="test" dynamic="true" />
  <!-- ... -->
</target>

I'm not sure I completely understood what You're trying to achieve, but shouldn't attribute dynamic of task property do the job?

<target name="filesettest">
  <property name="sourcefiles" value="test" dynamic="true" />
  <!-- ... -->
</target>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文