使用映射器 &文件集将文件复制到不同的子目录? [英] Using mapper & fileset to copy files into a different subdirectory?

查看:37
本文介绍了使用映射器 &文件集将文件复制到不同的子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 Ant 目标,将目录中的文件复制到具有相同文件夹结构的目标目录,并附加一个子文件夹.

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

例如,来源是:

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

我希望目的地是:

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

这是我目前的目标,但它似乎没有做任何事情:

Here is my target so far, but it doesn't appear 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>

我错过了什么?

推荐答案

你可以使用 regexp 映射器:

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>

我使用硬编码的 file.separators 来缩短.基本上,您将输入文件的路径(来自)拆分为目录和文件名(捕获 \1\2),然后插入 \x它们之间的额外元素(到).

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).

我不清楚您的示例 - 看起来您想匹配bar.pdf"并将其重命名为foo.pdf",以及更改目录.如果您需要这样做,您可以考虑链接几个更简单的正则表达式映射器,而不是尝试编写一个复杂的映射器:

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>

使用 glob mapper,你需要在from字段中指定一个通配符*:

to 和 from 都是必需的,并且定义可能包含在的模式最多一个*.对于每个源文件匹配 from 模式,目标文件名将从通过替换 * 在模式带有文本的 to 模式匹配 from 模式中的 *.不匹配的源文件名from 模式将被忽略.

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" />

这篇关于使用映射器 &amp;文件集将文件复制到不同的子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆