【问题标题】:Using "antcall" in included ant files在包含的 ant 文件中使用“antcall”
【发布时间】:2013-05-31 09:27:24
【问题描述】:

我有一个共享的 ant 脚本 b.ant,它在内部使用 antcall。它计算客户端脚本使用的属性。我使用 include 而不是 import 客户端脚本来避免无意覆盖目标,但这给我的 antcall 带来了问题。

当使用include 时,b 中的所有目标都是前缀,b 中的depends 属性会相应更新。然而,antcall 并非如此。 有没有办法处理这个问题,即让antcall 始终调用“本地”蚂蚁目标?

我可以通过使用import 来解决这个问题,但是我会遇到所有覆盖问题。不能用depends代替antcall。


示例文件

我有两个文件:

a.ant

  <project>
    <include file="b.ant" as="b" />
    <target name="test-depends" depends="b.depend">
      <echo>${calculated-property}</echo>
    </target>
    <target name="test-call" depends="b.call">
      <echo>${calculated-property}</echo>
    </target>
  </project>

b.ant

<project>
  <target name="depend" depends="some-target">
    <property name="calculated-property" value="Hello World"/>
  </target>
  <target name="call">
    <antcall target="some-target" inheritrefs="true"/>
    <property name="calculated-property" value="Hello World"/>
  </target>
  <target name="some-target"/>
</project>

示例输出

调用 test-depend 按预期工作,但 test-call 失败并出现以下输出:

b.call:

BUILD FAILED
D:\ws\rambo2\ws-dobshl\ant-test\b.ant:6: The following error occurred while executing this line:
Target "some-target" does not exist in the project "null". 

Total time: 258 milliseconds

【问题讨论】:

    标签: ant


    【解决方案1】:

    Ant 是一种依赖矩阵规范语言。通常一堆&lt;antcall/&gt;&lt;ant/&gt;&lt;include/&gt;&lt;import/&gt; 是构建脚本编写不佳的标志。这是一个开发人员试图强迫 Ant 表现得像一种编程语言。

    对于开发人员来说,将程序分解为更小的文件是有意义的。甚至 Python 和 Perl 脚本也可以从中受益。但是,分解 Ant 构建脚本通常会导致问题。我们有一位开发人员检查了每个项目并将所有 build.xml 文件分解为六或七个单独的构建文件,以便改进该过程。它基本上打破了整个Ant依赖机制。为了解决这个问题,他随后投入了一堆&lt;ant/&gt; 调用和&lt;include&gt; 任务。最后,这意味着每个目标都被调用了 12 到 20 次。

    不使用&lt;import/&gt;&lt;antcall/&gt; 并不是一个硬性规定。但是,我已经使用 Ant 多年,很少使用这些机制。当我这样做时,它通常用于多个项目将使用的 shared 构建文件(这听起来像你所拥有的),但不是在我的共享构建文件中定义目标,而是定义宏。这消除了您遇到的目标命名空间问题,并且宏工作得更好,因为它们更像 Ant 任务。在 Ant 1.8 中引入 &lt;local/&gt; 时尤其如此。

    看看您是否可以将共享构建文件重组为使用&lt;macrodef/&gt; 而不是目标。这将使包含您的共享构建文件变得更加容易。

    【讨论】:

      【解决方案2】:

      给b.ant中的&lt;project&gt;一个name,然后把&lt;antcall&gt;target改成:

      <project name="b"> <!-- Give the project a name -->
        <target name="depend" depends="some-target">
          <property name="calculated-property" value="In b.depend"/>
        </target>
        <target name="call">
          <!-- Specify the name of the project containing the target -->
          <antcall target="b.some-target" inheritrefs="true"/>
          <property name="calculated-property" value="In b.call"/>
        </target>
        <target name="some-target"/>
      </project>
      

      ant -f a.ant test-call的结果:

      b.call:
      
      b.some-target:
      
      test-call:
           [echo] In b.call
      
      BUILD SUCCESSFUL
      

      随着对 b.ant 的更改,a.ant 中的&lt;include&gt; 可以通过删除as 属性来简化:

      <include file="b.ant" />
      

      【讨论】:

        猜你喜欢
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 2015-02-03
        • 2013-01-06
        • 1970-01-01
        • 2011-02-20
        • 2011-04-15
        • 1970-01-01
        相关资源
        最近更新 更多