Xiao
Xiao

Reputation: 12695

How to copy all contents under one kind of directory in ant?

I'd like to copy files and subdirectories under directory dir_java, but not the dir_java directory itself and all its parent directories. a.k.a I'd like to get all directory dir_com and its subdirectories to todir in ant copy task.

Is there a way to do this?

temp
   \--dir_1
       |
       \--dir_java
           |
           \--dir_com
   \--dir_2
       |
       \--dir_java
           |
           \--dir_com
   \--dir_3
       |
       \--dir_foobar

There is no regularity for dir_1,dir_2,dir_3

I tried below

    <copy todir="to_dir" verbose="true" overwrite="true">
        <fileset dir="temp" includes="*/dir_java/**" />
    </copy>

But I got directories like to_dir/dir_1/dir_java/dir_com.

I want directories like to_dir/dir_com

My best trying was to add a exec task with invoking cp command after above copy task, but only for non windows platform:

    <exec executable="/bin/sh">
        <arg value="-c"/>
        <arg value="cp -R to_dir/*/dir_java/* to_dir"/>
    </exec>

Upvotes: 0

Views: 113

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7041

Are all of the dir_java directories at the same level in the hierarchy? If so, consider using a <cutdirsmapper>...

[cutdirsmapper] strips a configured number of leading directories from the source file name.

After <cutdirsmapper> removes the leading directories, it will preserve whatever remains of the directory structure...

<copy todir="to_dir" verbose="true" overwrite="true">
    <fileset dir="temp" includes="*/dir_java/**" />
    <cutdirsmapper dirs="2" />
</copy>

Before running Ant

$ find . -type f
./temp/dir_1/dir_java/dir_com/1.txt
./temp/dir_2/dir_java/dir_com/2.txt
./temp/dir_3/dir_foobar/3.txt

After running Ant

$ find . -type f
./temp/dir_1/dir_java/dir_com/1.txt
./temp/dir_2/dir_java/dir_com/2.txt
./temp/dir_3/dir_foobar/3.txt
./to_dir/dir_com/1.txt
./to_dir/dir_com/2.txt

Notice how 1.txt and 2.txt are still under dir_com after the mapping has been applied.

Upvotes: 1

Related Questions