3

それぞれがタスクを実行するための特定のパターンセットを含んでいることを除いて、それぞれが本質的に同じことを行う一連のターゲットがあります。これらのターゲットを単一の「再利用可能な」ターゲットに集約し、代わりに「パラメーターとして」ファイルのセットを取得したいと思います。

たとえば、これ

<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で回答されたように拡張されていないことがわかりました。この機能しないコードでは、が呼び出されると、その要素はtestという名前のパターンではなく、文字通り${sourcefiles}という名前のパターンセットを参照します。echopatternset

さまざまなファイルのセットで動作する再利用可能なNAntターゲットをどのように作成しますか?カスタムタスクを作成せずに、NAntでこれをそのまま行う方法はありますか?

4

2 に答える 2

6

私はついにこれを思いついた、それは私の目的に役立つ。ボーナスとして、これはターゲットを動的に呼び出すことも示しています。

<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>
于 2011-03-08T16:54:05.123 に答える
0

あなたが達成しようとしていることを完全に理解したかどうかはわかりませんが、タスクの属性dynamicそのproperty仕事をするべきではありませんか?

<target name="filesettest">
  <property name="sourcefiles" value="test" dynamic="true" />
  <!-- ... -->
</target>
于 2010-10-04T08:55:36.117 に答える