crappish
crappish

Reputation: 2708

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.

Upvotes: 8

Views: 2800

Answers (2)

Piotr Galas
Piotr Galas

Reputation: 11

In case you would like to remove something from the middle of path, during copy, you can use filtermapper that is described here.

<copy todir="C:/targetdir/" overwrite="false">
    <fileset dir="C:/sourcedir/" includes="**/*.*" />
    <filtermapper>
      <replacestring from="LibraryX/A/" to="" />
    </filtermapper>
</copy>

Upvotes: 0

ChrisH
ChrisH

Reputation: 4826

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.

Upvotes: 8

Related Questions