【问题标题】:Call a <macrodef> for each <pathelement> in a <path> in Ant在 Ant 中为 <path> 中的每个 <pathelement> 调用 <macrodef>
【发布时间】:2016-11-02 13:33:34
【问题描述】:

我有一个path,如下:

<path refid="myjarlocation"/>

现在我想遍历这个路径,并且对于路径中存在的每个值,我想调用一个宏定义,将该值作为要在宏代码定义中使用的属性之一。

如果是目标,我们可以很容易地做到这一点:

<foreach target="mytarget" param="jarloc">
    <path refid="myjarlocation"/>
</foreach>

我不能为每个使用,因为我需要传递多个参数,所以我使用的是宏定义。因此,问题是如何遍历路径并调用宏定义而不是目标。

【问题讨论】:

  • iterate over this path 是什么意思?
  • 这就是我的路径的样子 通过迭代,我需要检索每个位置,即“1.jar”、“2.jar”等

标签: ant ant-contrib


【解决方案1】:

我通过使用 ant-contrib 的 for 任务迭代路径并将路径元素传递给宏定义来完成类似的工作。

首先在您的项目中获取 ant-contrib - 请参阅 http://ant-contrib.sourceforge.net/

接下来,在您的 ant 构建中定义您的宏定义,但是您希望包含一些将采用您的路径元素的属性。例如:

<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} 来引用循环参数!

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多