扩展现有的Ant路径标签

4

在Ant构建文件中,是否可以扩展现有的路径标签?例如,我想要扩展:

<path id="project.classpath">
  <pathelement path="build/classes" />
</path>

使用新的path=module/lib参数。这样的结果等同于:

<path id="project.classpath">
  <pathelement path="build/classes" />
  <pathelement path="module/lib" />
</path>
1个回答

1

您不能直接扩展现有路径,如果尝试以下操作:

<path id="project.classpath">
  <pathelement path="build/classes" />
</path>

<path id="project.classpath">
  <pathelement path="${ant.refid:project.classpath}" />
  <pathelement path="module1/lib" />
</path>

由于存在“循环依赖”,它将失败,您正在尝试同时读取和设置路径。您可以通过添加额外的步骤来打破这个循环。在每次设置路径之前,将当前值存储在<string>资源中即可实现您想要的操作。请保留HTML标签。
<path id="cp">
  <pathelement path="build/classes" />
</path>
<echo message="${ant.refid:cp}" />

<string id="cps" value="${toString:cp}" />
<path id="cp">
  <pathelement path="${ant.refid:cps}" />
  <pathelement path="module1/lib" />
</path>
<echo message="${ant.refid:cp}" />

<string id="cps" value="${toString:cp}" />
<path id="cp">
  <pathelement path="${ant.refid:cps}" />
  <pathelement path="module2/lib" />
</path>
<echo message="${ant.refid:cp}" />

运行时会产生类似以下的结果:
[echo] /ant/path/build/classes
[echo] /ant/path/build/classes:/ant/path/module1/lib
[echo] /ant/path/build/classes:/ant/path/module1/lib:/ant/path/module2/lib

您每次都将id重新分配给不同的路径。通常这是一个坏主意,因为您无法确定在构建的每个点上使用了哪个路径:因此请谨慎使用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接