Ant:路径中的可变部分作为宏的属性

Ant: Variable part in path as attribute of macro

我有以下问题,如何在 path 元素中设置变量 "SERVERNAME" 作为宏 (myCompile) 的参数?

<path id="myClasspath"  >
  <fileset>
    <include name="{??SERVERNAME??}/my.jar" />
  </fileset>
</path>

<macrodef name="myCompile">
  <attribute name="classPath" />
  <attribute name="server" />
  <sequential>
     <javac destdir="dest" classpathref="@{classPath}" srcdir="./src" />
  </sequential>
</macrodef>

<target name="Build_server1">
  <!-- as {??Servername??} in path should be used "server1" -->
  <myCompile classPath="myClasspath" server="server1"/>
</target>

<target name="Build_server2">
  <!-- as {??Servername??} in path should be used "server2"-->
  <myCompile classPath="myClasspath" server="server2"/>
</target>

编辑:如果将<path>移至宏,则可以使用宏的属性。 但是不可能在另一个地方重用定义的路径。(参见编辑 2)

<macrodef name="myCompile">
  <attribute name="classPath" />
  <attribute name="server" />
  <sequential>
    <path id="myClasspath"  >
      <fileset>
        <include name="@{server}/my.jar" />
      </fileset>
    </path>
     <javac destdir="dest" classpathref="@{classPath}" srcdir="./src" />
  </sequential>
</macrodef>

编辑 2 可以在宏中定义后重用 path

我不是 ANT 专家,但我认为以下内容可以为您提供一个不错的起点:

<macrodef name="myCompile">
  <attribute name="classPath" />
  <sequential>
     <javac destdir="dest" classpathref="@{classPath}" srcdir="./src" />
  </sequential>
</macrodef>

<target name="Build_server1">
  <property name="server" value="server1"/>
  <antcall target="setMyClassPath"/>
</target>

<target name="Build_server2">
  <property name="server" value="server2"/>
  <antcall target="setMyClassPath"/>
</target>

<target name="setMyClasspath">
  <path id="myClasspath">
    <fileset>
      <include name="${server}/my.jar" />
    </fileset>
  </path>
  <myCompile classPath="myClasspath"/> << Add here and removed from above
</target>