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

时间:2015-06-16 11:29:52

标签: java ant macros

我有以下问题,如何设置变量" SERVERNAME"在path元素中,作为宏(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

1 个答案:

答案 0 :(得分:1)

我不是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>