5

I am looking to copy a directory from one location to another. However, after looking at "copy" and "copyDir" (which is deprecated) it seems that Ant, by default, only copies the content of one location to another, not the actual directory (and everything in it).

So, as an example I have the following:

./Foo/test.txt

And I apply the following piece of Ant:

 <copy todir="./build">
    <fileset dir="./Foo"/>
 </copy>

The result looks like this:

./build/test.txt

Whereas I would like it to be:

./build/Foo/test.txt

Hope that makes sense. How can I do that?

martin clayton
  • 76,436
  • 32
  • 213
  • 198
sjwb
  • 91
  • 2
  • 4

2 Answers2

3

To preserve the structure you would need to be copying the parent folder. There is a flatten option in the copy command, but it defaults to false. You can also make the directory structure you want, then copy at the file level straight into your fresh folder.

Here is the relevant line from the reference:

When a <fileset> is used to select files to copy, the todir attribute must be set. Files that are located under the base directory of the <fileset> will be copied to a directory under the destination directory matching the path relative to the base directory of the <fileset>, unless the flatten attribute is set to true.

The problem lies in that you are picking up the Foo folder, so you only get the files under it, you need to pick the folder above Foo to get the fileset where the ~/Foo/file.txt structure exists.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Mark Dickinson
  • 6,573
  • 4
  • 29
  • 41
3

What about this:

<copy todir="./build">
  <fileset dir=".">
    <include name="Foo/**"/> 
  </fileset>
</copy>
Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94