2

这是我想要做的,我想在构建期间从我的大量属性文件中替换名称和地址,但不幸的是我不能这样做,有没有更好的方法来做到这一点而不必复制粘贴 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#
4

1 回答 1

2

targetofforeach并非旨在采用多个目标名称。它只遍历提供的列表,而不是提供的目标。

为了使实施更加干燥,您可以

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

Actually, for the two targets -- replace.address and replace.name, are you sure that you want to call them from the commandline? If not, name them -replace.address and -replace.name or use macrodef -- exposing the iteration body of foreach is not a good practice.

于 2013-02-14T14:42:30.510 回答