110

I have used copydir to copy a directory tree but it is deprecated. My directory contains some sub-directories, and some of those contain files and others contain more sub-directories.

How can I copy the entire tree?

malana
  • 5,045
  • 3
  • 28
  • 41
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • 5
    Sounds like you want to do a recursive copy. And that's the solution that Omnipresent gave. More people may find this question if the word "recursive" appeared in the question. – Jason Apr 26 '12 at 18:28

11 Answers11

129

Copy contents including the directory itself.

<copy todir="${dest.dir}" >  
    <fileset dir="${src.dir.parent}">  
        <include name="${src.dir}/**"/>
    </fileset>
</copy>

Note: ${src.dir} is relative to ${src.dir.parent}, and not a full path

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
ery
  • 1,311
  • 1
  • 8
  • 5
126
<copy todir="${dest.dir}" >  
    <fileset dir="${src.dir}" includes="**"/>  
</copy> 

believe that will do what you want... (Recursive copy done)

isapir
  • 21,295
  • 13
  • 115
  • 116
Omnipresent
  • 29,434
  • 47
  • 142
  • 186
  • 4
    apparently, the `includes` is not necessary when you want everything (see answer by user *s1n*) – Abel Aug 17 '10 at 07:13
  • 44
    This copies the contents of {src.dir}, but not the actual directory – cmcginty Jan 26 '13 at 00:42
  • Copies not a did but it's content. – A-Live May 30 '13 at 19:15
  • Since you are the accepted answer, you might correct it to become the correct answer (ery's answer is correct) ;-) – Christian Fries Sep 08 '13 at 19:42
  • I gather that the idea of SO is collective curation, which is why anyone can edit someone else's question (though you need a lot of rep to skip the edit review). – Andrew Spencer Oct 10 '13 at 11:23
  • The accepted answer isn't supposed to be the correct answer but merely the answer that helped OP. Changing the essence of an accepted isn't necessarily a good idea. Answer that receives most points is supposed to be the right answer. – Lindlof May 16 '14 at 13:18
26

You should only have to specify the directory (sans the includes property):

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

See the manual for more details and examples.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
s1n
  • 1,434
  • 11
  • 10
  • 13
    @s1n This commands only copies all the contents of src_dir to ../new/dir and not the src_dir. How do we copy src_dir (directory) to another location? – Pipalayan Nayak Dec 03 '11 at 19:24
17

Copy contents including the directory itself.

<copy todir="${dest.dir}" >  
  <fileset dir="${src.dir.parent}" includes="${src.dir}/**"/>
</copy>
cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • 2
    This is the most succinct option. See also http://ant.apache.org/manual/Types/fileset.html. – Jess Apr 09 '14 at 03:29
2

A fine point: ant will only copy the sub-directories if the source files are newer than the destination files. [1] In my case, the sub-dirs were not being copied (I am using verbose="true"), since there were no changes and they were already in the destination. You can use "overwrite" to force it, or touch some of the files in your source sub-dirs.

Jess
  • 23,901
  • 21
  • 124
  • 145
2

I used include tags as shown in below code snippet in my build.xml file to copy individul jar files inside a directory.

<target name="devInstall" depends="generateXsl" description="testing">
<copy flatten="true" todir="${test}/WEB-INF/lib" overwrite="${overwrite}">
                <fileset refid="buildJars"/>
                <fileset dir="lib">
                    <include name="commons-collections-*.jar"/>
                    <include name="commons-io-*.jar"/>              
                    <include name="kodo/*.jar"/>
                    <include name="mail*.jar"/>    
                    <include name="activation*.jar"/>               
                    <include name="guava*.jar"/>
                    <include name="jna*.jar"/>                          
                </fileset>          
            </copy>
</target>
Raman B
  • 331
  • 4
  • 5
1

From the example here, you can write a simple Ant file using copy task.

<project name="MyProject" default="copy" basedir=".">
    <target name="copy">
        <copy todir="./new/dir">
           <fileset dir="src_dir"/>
        </copy>
    </target>
</project>

This should copy everything inside src_dir (excluding it) to new/dir.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
NawaMan
  • 25,129
  • 10
  • 51
  • 77
1

I'm adding a more generic pattern to copy all subfolders.

<copy todir="${dest.dir}" >  
  <fileset dir="${src.dir}" includes="**/*"/>
</copy>

See Patterns for details.

sartoris
  • 816
  • 1
  • 7
  • 21
  • what should i replace dest.dir and src.dir with. i have nearly 6 directories to be copied to dest dir and make a war file and deply it in tomcat server – deepakl.2000 Aug 11 '22 at 16:48
  • @deepakl.2000 - note the ${...} construct indicates we're working with an Ant variable you define in your script elsewhere. see https://ant.apache.org/manual/using.html#properties – sartoris Aug 11 '22 at 22:47
1

Another ant task is Copydir. The key part here is to include the name of the directory you want to copy after the dest directory. The sub-directories and files will be copied automatically.

<target name="-post-jar">
    <copydir src="config" dest="${dist.dir}/config/"/>
</target>
Andrei
  • 7,509
  • 7
  • 32
  • 63
0

This code should copy the folder as well as its contents. It also uses the basename task to avoid having to do any manual path parsing.

<project name="Build" default="doCopy">
  <property name="source.dir" value="SourceDirPathGoesHere"/>
  <property name="dest.dir" value="DestinationDirPathGoesHere"/>
  <target name="doCopy">
    <basename property="source.dir.base.name" file="${source.dir}"/>
    <copy todir="${dest.dir}">
      <fileset dir="${source.dir}/.." includes="${source.dir.base.name}/**"/>
    </copy>
  </target>
</project>
user506069
  • 771
  • 1
  • 6
  • 14
0

I finally copied using following code

<copy todir="${root.dir}/dist/src">  
                <fileset dir="${root.dir}/build/src" includes="**"/>  
            </copy>

This will copy the src folder from dist to build.

Hope this helps someone.

Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76