Ant:将相同的文件集复制到多个位置

时间:2009-07-22 11:28:54

标签: ant

我需要一个将一个文件夹复制到其他几个地方的Ant脚本。作为一个优秀的顺从程序员,我不想重复自己。有没有办法采取这样的文件集:

<copy todir="${target}/path/to/target/1">
    <fileset dir="${src}">
        <exclude name='**/*svn' />
    </fileset>
</copy>

fileset存储在变量中以便重新使用?

3 个答案:

答案 0 :(得分:40)

在文件集上声明一个id属性,然后在每个复制任务中引用它。

例如:

<project name="foo">
  <fileset id="myFileSet" dir="${src}">
    <exclude name='**/*svn' />
  </fileset>
  ...
  <target name="copy1">
    <copy todir="${target}/path/to/target/1">
      <fileset refid="myFileSet"/>
    </copy>
  </target>
  <target name="copy2">
    <copy todir="${target}/path/to/target/2">
      <fileset refid="myFileSet"/>
    </copy>
  </target>
</project>

答案 1 :(得分:29)

Rich's answer可能更适合您的具体问题,但在Ant中重用代码的一般方法是<macrodef>

<macrodef name="copythings">
  <attribute name="todir"/>
  <sequential>
    <copy todir="@{todir}">
      <fileset dir="${src}">
        <exclude name='**/*svn' />
      </fileset>
    </copy>
  </sequential>
</macrodef>

<copythings todir="/path/to/target1"/>
<copythings todir="/path/to/target2"/>

答案 2 :(得分:0)

已经提出了第一个答案,但您也可以使用映射器复制到多个目的地。

相关问题