Ant 对象和引用:引用 ID 的范围是什么? [英] Ant objects and references: what is the scope of a reference's ID?

查看:25
本文介绍了Ant 对象和引用:引用 ID 的范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有关于它的文档似乎很奇怪(至少我知道没有文档;我很乐意接受纠正).

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

当我这样做时:

<fileset id="my.fs" dir="..."/>

ID my.fs 的范围是什么?

What is the scope of the ID my.fs?

  • 整个 Ant 执行周期?
  • 当前目标(以及任何依赖当前目标的目标)?
  • The entire Ant execution cycle?
  • The current target (and any target that depends on the current target)?

最后,如果多个线程(使用 parallel 任务产生)尝试定义具有相同 ID 的文件集会发生什么?

And, lastly, what happens if multiple threads (spawned using the parallel task) attempt to define filesets with the same ID?

推荐答案

引用在定义它们的项目中可见.例如,如果 <fileset id="my.fs" dir="..."/> 放置在任何目标之外,它将对构建文件中的所有目标可见.如果它在目标A中定义,那么如果B依赖于A,那么它将在目标B中可见:

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:

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

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

但是,如果您正在调用子项目,例如使用 antantcall 任务,默认情况下子项目将继承父项目中定义的引用(与 Ant 属性不同).要继承它们,您可以在调用子项目时将 inheritrefs 属性设置为 true:

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:

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

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

如果您在 parallel 任务中执行并行任务,并且两者都定义了相同的引用 ID,则根据执行顺序,最后一个完成的任务将覆盖另一个任务的引用.

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>

这篇关于Ant 对象和引用:引用 ID 的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆