Tony
Tony

Reputation: 2406

Ant: Variable part in path as attribute of macro

I have following problem, how to set variable "SERVERNAME" in path element, as parameter of macro (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>

EDIT: If <path> is moved to macro then it is possible to use attributes of macro. But it's not possible to reuse the defined path on another place. (See edit 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>

EDIT 2 it is possible to reuse path after it was defined in macro.

Upvotes: 3

Views: 509

Answers (1)

Brett Walker
Brett Walker

Reputation: 3576

I'm no ANT expert but I think the following will give you a good idea to start with:

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

Upvotes: 1

Related Questions