Short
Short

Reputation: 7837

Java ant help, differing fileset to be used for different targets

I'm new to Ant, so I'm looking for ideas here.

I'm looking for a way to use a different fileset per ANT target, and I'm not finding any luck reading the ANT documentation. To be concrete, here is what I have:

<fileset id="MY-FILESET-ONE" dir="..." />
  <include name="**/*.java />
</fileset>
<fileset id="MY-FILESET-TWO" dir="..." />
  <include name="**/*.other />
</fileset>

<target name="BASETARGET" depends="...">
  <fileset refid="MY-FILESET-ONE" />
</target>

<target name="ANT-TARGET-ONE" depends="BASETARGET">
  <fileset refid="MY-FILESET-ONE" />
</target>

<target name="ANT-TARGET-TWO" depends="BASETARGET" />
  <fileset refid="MY-FILESET-TWO" />
</target>

What I want to do is have the fileset that the target BASETARGET uses be different depending on which target is invoked. If ANT-TARGET-ONE is invoked, use a different fileset, than if ANT-TARGET-TWO is invoked.

Here's something like I envision:

<target name="BASETARGET" depend="...">
  <fileset refid="${myvar} />
</target>

<target name="ANT-TARGET-ONE" depends="BASETARGET">
  <var name="myvar" value="MY-FILESET-ONE" />
</target>

<target name="ANT-TARGET-TWO">
  <var name="myvar" value="MY-FILESET-ONE" />
</target>

How can I achieve this using ant? Basically I want to control which sets of my unit-tests get run depending on the target being invoked? I know properties can only be set once, so I don't think that could possibly work. I looked at the var here: http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html

however, trying to get the value out of 'myvar' like this:

<fileset refid="${myvar} />

results in a error, I'm unsure how to achieve this!

Upvotes: 1

Views: 73

Answers (1)

rlegendi
rlegendi

Reputation: 10606

"What I want to do is have the fileset that the target BASETARGET uses be different depending on which target is invoked."

Here's a small example for that: you simply create the filesets with a given ID and refer them in your base target.

<project name="test" basedir=".">
  <target name="base">
    <copy todir="out">
    <fileset refid="files-to-copy"/>
  </copy>
  </target>

  <target name="def-fs-1" >
    <fileset id="files-to-copy" dir="in">
      <include name="a.txt" />
    </fileset>
  </target>

  <target name="def-fs-2" >
    <fileset id="files-to-copy" dir="in">
      <include name="b.txt" />
    </fileset>
  </target>

  <target name="t1" depends="def-fs-1,base" />
  <target name="t2" depends="def-fs-2,base" />
</project>

Upvotes: 3

Related Questions