tags:

views:

43

answers:

1

Using just Ant, I want to copy subdirectories of some top-level directories but the names of top-level directories can change so I need Ant to programatically determine which directories to copy.

In other words, given the sample directory structure below, copy the contents of each ./<projectX>/bin directory to ./bin.

bin
project1
   \-- bin
        \-- com
             \-- name
                  \-- dir1
                        \-- file1.class
                        \-- file2.class
                        \-- file3.class
                  \-- dir2
                        \-- file4.class
                        \-- file5.class
project2
   \-- bin
        \-- com
             \-- name
                  \-- dir3
                        \-- file6.class
                        \-- file7.class
                        \-- file8.class
project3
   \-- bin
        \-- com
             \-- name
                  \-- dir4
                        \-- file9.class
                  \-- dir5
                        \-- file10.class
                        \-- file11.class

And the end result would be a bin directory that looks like this:

bin
  \-- com
       \-- name
            \-- dir1
                  \-- file1.class
                  \-- file2.class
                  \-- file3.class
            \-- dir2
                  \-- file4.class
                  \-- file5.class
            \-- dir3
                  \-- file6.class
                  \-- file7.class
                  \-- file8.class
            \-- dir4
                  \-- file9.class
            \-- dir5
                  \-- file10.class
                  \-- file11.class

I've tried

    <copy todir="${basedir}/bin">
        <fileset dir="${basedir}">
            <include name="**/bin/*"/>
            <exclude name="bin"/>
        </fileset>
    </copy>

but that includes the projectX/bin directories underneath the top-level bin directory.

+1  A: 

You can use a mapper to generate the destination paths from the sources, for example:

<copy todir="${basedir}/bin">
    <fileset dir="${basedir}">
        <include name="*/bin/**"/>
    </fileset>
    <regexpmapper from=".*/bin/(.*)" to="\1" />
</copy>
martin clayton
Brilliant! Never would have come up with this on my own. One caveat though, I'm on Windows so I needed to add the 'handledirsep="true" ' attribute to the <regexpmapper> task. Thank you.
Scrubbie