Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copying multiple directories (and contents) in one shot

Tags:

ant

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

like image 289
user331465 Avatar asked Nov 19 '10 21:11

user331465


1 Answers

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*/" />
like image 63
martin clayton Avatar answered Oct 10 '22 20:10

martin clayton