问题
Seems odd that there is no documentation about it (at least no documentation that I'm aware of; and I'll be happy to stand corrected).
When I do this:
<fileset id="my.fs" dir="..."/>
What is the scope of the ID my.fs
?
- The entire Ant execution cycle?
- The current target (and any target that
depends
on the current target)?
And, lastly, what happens if multiple threads (spawned using the parallel
task) attempt to define filesets with the same ID?
回答1:
References are visible across the project in which they are defined. For example, if <fileset id="my.fs" dir="..."/>
is placed outside any target, it will be visible for all targets in the buildfile. If it is defined in target A
, then it will be visible in target B
if B
depends on A
:
Example 1:
<project name="Project1" default="doIt">
<fileset id="my.fs" dir="some_dir"/>
...
<target name="doIt">
<copy todir="some_dir_copy">
<fileset refid="my.fs" /> <!-- this will work -->
</copy>
</target>
</project>
Example 2:
<project name="Project1" default="doIt">
<target name="prepare">
<fileset id="my.fs" dir="some_dir"/>
</target>
<target name="doIt" depends="prepare">
<copy todir="some_dir_copy">
<fileset refid="my.fs" /> <!-- this will work -->
</copy>
</target>
</project>
However, if you are invoking a subproject, e.g. using the ant
or antcall
tasks, the subproject by default will not inherit the references defined in the parent project (unlike Ant properties). To inherit them, you can set the inheritrefs
attribute to true when invoking the subproject:
Example 3:
<project name="Project1" default="doIt">
<target name="doIt">
<fileset id="my.fs" dir="some_dir"/>
<ant antfile="./build.xml" target="run" />
</target>
<target name="run">
<copy todir="some_dir_copy">
<fileset refid="my.fs" /> <!-- this will fail -->
</copy>
</target>
</project>
Example 4:
<project name="Project1" default="doIt">
<target name="doIt">
<fileset id="my.fs" dir="some_dir"/>
<ant antfile="./build.xml" target="run" inheritrefs="true" />
</target>
<target name="run">
<copy todir="some_dir_copy">
<fileset refid="my.fs" /> <!-- this will work -->
</copy>
</target>
</project>
In case you have parallel tasks executing inside a parallel
task, and both defined the same reference ID, then depending on the order of execution, the last one to finish will override the other task's reference.
<parallel>
<fileset id="my.fs" dir="some_dir"/>
<fileset id="my.fs" dir="another_dir"/>
</parallel>
...
<target name="doIt">
<copy todir="some_dir_copy">
<fileset refid="my.fs" /> <!-- this may copy either some_dir or another_dir, depending on which parallel task finished last -->
</copy>
</target>
来源:https://stackoverflow.com/questions/24448993/ant-objects-and-references-what-is-the-scope-of-a-references-id