Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant: copy the same fileset to multiple places

Tags:

ant

I need an Ant script that will copy one folder to several other places. As a good obedient programmer, I want not to repeat myself. Is there any way of taking a fileset like this:

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

And storing the fileset in a variable so it can be re-used?

like image 397
Marcus Downing Avatar asked Jul 22 '09 11:07

Marcus Downing


2 Answers

Declare an id attribute on the fileset and then reference it in each copy task.

For example:

<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>
like image 177
Rich Seller Avatar answered Oct 04 '22 05:10

Rich Seller


Rich's answer is probably better for your specific problem, but the generic way of reusing code in Ant is a <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"/>
like image 29
Dave Webb Avatar answered Oct 04 '22 06:10

Dave Webb