Using mapper & fileset to copy files into a different subdirectory?

馋奶兔 提交于 2019-12-01 16:05:00

You could use a 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've used hard-coded file.separators to shorten. Basically, you split the path to the input file (from) into directory and filename (capture \1 and \2) and then insert the \x extra element between them (to).

I'm not clear on your example - it looks like you want to match 'bar.pdf' and rename it to 'foo.pdf', as well as changing the directory. If you need to do that, you might consider chaining a couple of simpler regexp mappers, rather than trying to cook up 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 a glob mapper, you need to specify one wildcard * in the from field:

Both to and from are required and define patterns that may contain at most one *. For each source file that matches the from pattern, a target file name will be constructed from the to pattern by substituting the * in the to pattern with the text that matches the * in the from pattern. Source file names that don't match the from pattern will be ignored.

So something like this might work:

<mapper type="glob" from="*/foo.pdf" to="*/x/foo.pdf" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!