ant

How to strip one folder during Ant copy


I have a file which has filepaths like "LibraryX/A/Stuff/FileY.txt", which I'm using as includesfile in Ant build. However, I'm in need of removing the "LibraryX/A/" part of the path DURING the copy process: The file gets copied from "LibraryX/A/Stuff/FileY.txt" and lands into "Stuff/FileY.txt". I've looked into few regexpmappers but haven't had any success with them at all. :/

The purpose for this is that the target folder can have custom files in "Stuff/MoreStuff" overwritten, and I want to use the overwrite="false" to keep the disk access into minimum and keeping the custom files intact.

Ant:

<copy todir="C:/targetdir/" overwrite="false">
    <fileset dir="C:/sourcedir/">
        <includesfile name="C:/targetdir/includes.file" />
    </fileset>
</copy>

Includes.file:

LibraryX/A/Stuff/FileA.txt
LibraryX/A/Stuff/FileB.txt
LibraryX/A/Stuff/FileC.txt
LibraryX/A/Stuff/FileY.txt

Sourcedir:

sourcedir/LibraryX/A/Stuff/FileA.txt
sourcedir/LibraryX/A/Stuff/FileB.txt
sourcedir/LibraryX/A/Stuff/FileC.txt
sourcedir/LibraryX/A/Stuff/FileY.txt

Target dir:

targetdir/Stuff/FileY.txt

Now, all the files in Stuff -folder at sourcedir, should end into the Stuff -folder in targetdir. But how?

Bonus: If I move the files from "targetdir/LibraryX/A/Stuff", they will overwrite everything in the "targetdir/Stuff" folder, even with the overwrite="false". Presumably because they are newer files than the ones in the Stuff folder currently.

Note: I could, of course, move the custom files away from the target directory, copy the stuff over and then move the custom files back, overwriting the new ones. But this accesses the disk quite a lot, slowing down the process.


Solution

  • Starting with Ant v1.8.2 you can use the cutdirsmapper to strip some number of leading directories from file paths. See the very bottom of the mapper type docs.

    <copy todir="C:/targetdir/" overwrite="false">
        <fileset dir="C:/sourcedir/">
            <includesfile name="C:/targetdir/includes.file" />
        </fileset>
        <cutdirsmapper dirs="2"/>
    </copy>
    

    Bonus: You could use the touch ant task to make all the files in targetdir newer than all the source files and therefore prevent them from being overwritten.