在 Ant 中为 <path> 中的每个 <pathelement> 调用一个 <macrodef>
Call a <macrodef> for each <pathelement> in a <path> in Ant
我有一个path
如下:
<path refid="myjarlocation"/>
现在我想遍历这条路径,对于路径中存在的每个值,我想调用一个 macrodef,该值作为要在 marcodef 中使用的 属性 之一。
在目标的情况下,我们可以很容易地做到这一点:
<foreach target="mytarget" param="jarloc">
<path refid="myjarlocation"/>
</foreach>
我不能使用 for each 因为我需要传递多个参数,所以我使用的是 macrodef。因此,问题是如何遍历路径并调用 macrodef 而不是目标。
我通过使用 ant-contrib 的 for
任务迭代路径并将路径元素传递给 macrodef 来完成类似的工作。
首先在您的项目中获取 ant-contrib - 请参阅 http://ant-contrib.sourceforge.net/
接下来,在您的 ant 构建中定义您的 macrodef,但是您希望包括一些将采用您的路径元素的属性。例如:
<macrodef name="awesome-macro">
<attribute name="path-to-deal-with"/>
<attribute name="unrelated-attribute"/>
<sequential>
...
</sequential>
</macrodef>
然后,使用 for
任务将路径迭代到路径元素中并调用宏:
<for param="path.element">
<fileset dir="${jars.dir}">
<include name="*.jar"/>
</fileset>
<sequential>
<awesome-macro path-to-deal-with="@{path.element}" unrelated-attribute="whatever"/>
</sequential>
</for>
注意在 for 循环中使用 @{path.element}
而不是 ${path.element}
来引用循环参数!
我有一个path
如下:
<path refid="myjarlocation"/>
现在我想遍历这条路径,对于路径中存在的每个值,我想调用一个 macrodef,该值作为要在 marcodef 中使用的 属性 之一。
在目标的情况下,我们可以很容易地做到这一点:
<foreach target="mytarget" param="jarloc">
<path refid="myjarlocation"/>
</foreach>
我不能使用 for each 因为我需要传递多个参数,所以我使用的是 macrodef。因此,问题是如何遍历路径并调用 macrodef 而不是目标。
我通过使用 ant-contrib 的 for
任务迭代路径并将路径元素传递给 macrodef 来完成类似的工作。
首先在您的项目中获取 ant-contrib - 请参阅 http://ant-contrib.sourceforge.net/
接下来,在您的 ant 构建中定义您的 macrodef,但是您希望包括一些将采用您的路径元素的属性。例如:
<macrodef name="awesome-macro">
<attribute name="path-to-deal-with"/>
<attribute name="unrelated-attribute"/>
<sequential>
...
</sequential>
</macrodef>
然后,使用 for
任务将路径迭代到路径元素中并调用宏:
<for param="path.element">
<fileset dir="${jars.dir}">
<include name="*.jar"/>
</fileset>
<sequential>
<awesome-macro path-to-deal-with="@{path.element}" unrelated-attribute="whatever"/>
</sequential>
</for>
注意在 for 循环中使用 @{path.element}
而不是 ${path.element}
来引用循环参数!