【问题标题】:how to call multiple ant targets for foreach如何为foreach调用多个蚂蚁目标
【发布时间】:2013-01-30 03:55:15
【问题描述】:

这是我想要做的,我想在构建期间从我的大量属性文件中替换名称和地址,但不幸的是我不能这样做,有没有更好的方法来做到这一点而不必复制粘贴 foreach两次。有人可以帮忙吗?

<target name="replace" >
<foreach target="replace.name,replace.address" param="foreach.file" inheritall="true">
    <path>
                <fileset dir="${build.tmp.dir}/resource">
                <!-- some complicated conditions go here -->
    </path>
</foreach>
</target>

<target name="replace.address">
    <echo>replacing #Address# for ${foreach.file}</echo>
    <replace file="${foreach.file}" token="#Address#" value="${address}" />
</target>

<target name="replace.name">
    <echo>replacing #Name# for ${foreach.file}</echo>
    <replace file="${foreach.file}" token="#Name#" value="${Name}" />
</target>

.properties 文件看起来像

name=#Name#
address=#Address#

【问题讨论】:

    标签: ant for-loop build ant-contrib


    【解决方案1】:

    target of foreach 的设计目的不是采用多个目标名称。它只遍历提供的列表,而不是提供的目标。

    为了使实现更加 DRY,您可以

    • 使用for 循环而不是foreach 和两个antcalls;
    • 使用 macrodeffor 循环 -- macrodef 可以将几个 ant xml 代码打包成一个类似任务的东西

    实际上,对于replace.addressreplace.name 这两个目标,您确定要从命令行调用它们吗? 如果没有,请将它们命名为 -replace.address-replace.name 或使用 macrodef -- 暴露 foreach 的迭代体不是一个好习惯。

    【讨论】:

      猜你喜欢
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      相关资源
      最近更新 更多