Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

How to copy a directory using Ant

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?

Upvotes: 112

Views: 103536

Answers (11)

ery
ery

Reputation: 1331

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

Upvotes: 131

Omnipresent
Omnipresent

Reputation: 30384

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

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

Upvotes: 127

Andrei
Andrei

Reputation: 7607

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>

Upvotes: 1

Raman B
Raman B

Reputation: 339

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>

Upvotes: 2

sartoris
sartoris

Reputation: 931

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.

Upvotes: 2

Dilip Rajkumar
Dilip Rajkumar

Reputation: 7074

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.

Upvotes: 0

user506069
user506069

Reputation: 781

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>

Upvotes: 0

cmcginty
cmcginty

Reputation: 117018

Copy contents including the directory itself.

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

Upvotes: 17

Jess
Jess

Reputation: 25079

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.

Upvotes: 2

s1n
s1n

Reputation: 1454

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.

Upvotes: 26

NawaMan
NawaMan

Reputation: 25687

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.

Upvotes: 1

Related Questions