7

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?

martin clayton
  • 76,436
  • 32
  • 213
  • 198
defoo
  • 5,159
  • 11
  • 34
  • 39

4 Answers4

14

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.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
zeroDivisible
  • 4,041
  • 8
  • 40
  • 62
7

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.

Community
  • 1
  • 1
leonbloy
  • 73,180
  • 20
  • 142
  • 190
1

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

martin clayton
  • 76,436
  • 32
  • 213
  • 198
SimonC
  • 6,590
  • 1
  • 23
  • 40
1

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

<copy todir="../new/dir">
  <fileset dir="src_dir"/>
</copy>
martin clayton
  • 76,436
  • 32
  • 213
  • 198
leedm777
  • 23,444
  • 10
  • 58
  • 87