How do I use Ant to copy a folder?

11,873

Solution 1

First of all, those are the examples from Ant documentation:

Copy a directory to another directory

<copy todir="../new/dir">
  <fileset dir="src_dir"/>   
</copy>

Copy a set of files to a directory

<copy todir="../dest/dir">
  <fileset dir="src_dir">
    <exclude name="**/*.java"/>
  </fileset>
</copy>

<copy todir="../dest/dir">
  <fileset dir="src_dir" excludes="**/*.java"/>   
</copy>

Copy a set of files to a directory, appending .bak to the file name on the fly

<copy todir="../backup/dir">
  <fileset dir="src_dir"/>
  <globmapper from="*" to="*.bak"/>   
</copy>

Secondly, here is the whole documentation about copy task.

Solution 2

Just because the docs were not very clear to me, and because the time I spent can serve others:

The docs say that this "copies a directory (dir1) to another directory (dest)":

<copy todir="../new/dest">
  <fileset dir="src/dir1"/>   
</copy>

Actually, this does not mean "copy dir1 inside dest", but rather "copy the contents of dir1 inside dest".

(In general, in Ant, the "root dir" of a filesets -as well at the todir attribute- is not considered as being part of the set itself.)

To place the directory dir1 inside dest one has several alternatives (none totally satisfying to me - and I'd imagined that the new DirSet would help here, but no)

<copy todir="../new/dest/dir1">
  <fileset dir="src/dir1"/>   
</copy>

or

<copy todir="../new/dest">
  <fileset dir="src" includes="dir1/**"/>
</copy>

See also here and here.

Solution 3

From http://ant.apache.org/manual/Tasks/copy.html:

<copy todir="../new/dir">
  <fileset dir="src_dir"/>
</copy>

Solution 4

This will do it:

<copy todir="directory/to/copy/to">
    <fileset dir="directory/to/copy/from"/>
</copy>

The ant manual is your friend: Ant Manual, in this case: Copy Task

Share:
11,873
defoo
Author by

defoo

Updated on June 11, 2022

Comments

  • defoo
    defoo almost 2 years

    I'm trying to copy a directory using the Ant copy task.

    I am a newbie at Ant; my current solution is:

    <copy todir="${release_dir}/lib">
       <fileset dir="${libpath}" />
    </copy>
    

    I'm wondering if there is a better and shorter way to accomplish the same thing?