Using mapper & fileset to copy files to another subdirectory?

I want to create an Ant target that copies files in a directory to a destination directory with the same folder structure, plus another subfolder added.

For example, the source:

a/b/c/foo.pdf d/e/f/bar.pdf 

I want the destination to be:

 a/b/c/x/foo.pdf d/e/f/x/foo.pdf 

Here is my goal so far, but it doesn't seem to be doing anything:

 <copy todir="${dest.dir}"> <fileset dir="${src.dir}" casesensitive="yes"> <include name="**${file.separator}foo.pdf" /> </fileset> <mapper type="glob" from="foo.pdf" to="x${file.separator}foo.pdf" /> </copy> 

What am I missing?

+7
build-process copy ant
source share
1 answer

You can use regexp mapper:

 <copy todir="${dest.dir}"> <fileset dir="${src.dir}" casesensitive="yes"> <include name="**/*.pdf"/> </fileset> <mapper type="regexp" from="^(.*)/(.*\.pdf)" to="\1/x/\2" /> </copy> 

I used compressed file.separators files for shorthand. Basically, you split the path to the input file (from) into the directory and file name (capture \1 and \2 ), and then insert the additional element \x between them (to).

I do not understand, for your example - it looks like you want to map "bar.pdf" and rename it to "foo.pdf", as well as change the directory. If you need to do this, you might think of a chain of a few simple regex cards, instead of trying to put together one complex one:

 <copy todir="${dest.dir}"> <fileset dir="${src.dir}" casesensitive="yes"> <include name="**/*.pdf"/> </fileset> <chainedmapper> <mapper type="regexp" from="^(.*)/(.*\.pdf)" to="\1/x/\2" /> <mapper type="regexp" from="^(.*)/(.*\.pdf)" to="\1/foo.pdf" /> </chainedmapper> </copy> 

When using glob mapper you need to specify one template * in the from field:

Both that and another are required and to define patterns which can contain the very one *. For each source file that matches the from template, target the file name will be built from to the picture, substituting * in the template with text that matches * in the template. Names of source files that do not match from the template will be ignored.

So something like this might work:

 <mapper type="glob" from="*/foo.pdf" to="*/x/foo.pdf" /> 
+11
source share

All Articles