defoo
defoo

Reputation: 5297

How do I use Ant to copy a folder?

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?

Upvotes: 7

Views: 12523

Answers (4)

leonbloy
leonbloy

Reputation: 75896

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.

Upvotes: 7

zeroDivisible
zeroDivisible

Reputation: 4130

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.

Upvotes: 14

SimonC
SimonC

Reputation: 6718

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

Upvotes: 1

leedm777
leedm777

Reputation: 24032

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

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

Upvotes: 1

Related Questions