copying multiple directories (and contents) in one shot

13,260

You can specify multiple inclusion and exclusion rules in a fileset. If you don't specify an inclusion rule, the default is everything is included, except anything that is excluded at least once by an exclude rule.

Here's an inclusive example:

<property name="src.dir" value="example" />
<property name="dest.dir" value="myInstance" />

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

Note the ** wildcard that will bring in the full directory tree under each of the three 'leading-edge' sub-directories specified. Alternatively, if you want to specifically exclude a few directories, but copy over all others, you might omit inclusion (and thereby get the default all-inclusive behaviour) and supply a list of exclusions:

<copy todir="${dest.dir}">
    <fileset dir="${src.dir}">
        <exclude name="ignoreThisDir*/" />
        <exclude name="ignoreThisOne*/" />
    </fileset>
</copy>

You could further boil the particular example you gave down to one exclusion pattern:

<exclude name="ignore*/" />
Share:
13,260

Related videos on Youtube

user331465
Author by

user331465

Updated on June 04, 2022

Comments

  • user331465
    user331465 almost 2 years

    I've been using ant for nearly a decade, but every so often I need to do something beyond my-ordinary experience. This one lacked an obvious answer (and the intuitive approaches led to dead ends)

    Problem:

    Copy several subdirectories (and their contents) in directory "example" to new directory "myInstance". To clarify, copy some, but not all subdirectories in the source directory.

    Source directory:

    example/
      ignoreThisDirectory/
      ignoreThisOneAlso/
      lib
      etc/
      webapps/
    

    Attempt: Dead End This attempt at first appeared to work. It created the subdirectories lib, etc,webapps. However 'copy' did not copy their contents; i was left with empty subdirectories.

    <copy todir="myInstance" >
      <dirset dir="example" includes="lib etc webapps"/>      
    </copy>    
    

    Successful But Verbose In the end, I had to copy each directory individually, which seem verbose and non-DRY:

       <copy todir="myInstance/etc">
         <fileset dir="example/etc"/>
        </copy>    
       <copy todir="myInstance/lib">
         <fileset dir="example/lib" />
        </copy>    
       <copy todir="myInstance/webapps">
         <fileset dir="example/webapps" />
        </copy>    
    

    thanks in advance

  • JasonMArcher
    JasonMArcher over 13 years
    I would instead use includes to list what you want to copy.

Related