一次性复制多个目录(和内容) [英] copying multiple directories (and contents) in one shot
问题描述
我已经使用 ant 近十年了,但我时常需要做一些超出我平常经验的事情.这个缺乏明显的答案(直觉方法导致死胡同)
I've been using ant for nearly a decade, but every so often I need to do something beyond my-ordinary experience. This one lacked an obvious answer (and the intuitive approaches led to dead ends)
问题:
将目录example"中的几个子目录(及其内容)复制到新目录myInstance".澄清一下,复制源目录中的一些子目录,但不是所有子目录.
Copy several subdirectories (and their contents) in directory "example" to new directory "myInstance". To clarify, copy some, but not all subdirectories in the source directory.
源目录:
example/
ignoreThisDirectory/
ignoreThisOneAlso/
lib
etc/
webapps/
尝试:死胡同这种尝试起初似乎奏效.它创建了子目录 lib 等,webapps.然而,'copy' 并没有复制它们的内容;我留下了空的子目录.
Attempt: Dead End This attempt at first appeared to work. It created the subdirectories lib, etc,webapps. However 'copy' did not copy their contents; i was left with empty subdirectories.
<copy todir="myInstance" >
<dirset dir="example" includes="lib etc webapps"/>
</copy>
成功但冗长最后,我不得不单独复制每个目录,这看起来很冗长且非 DRY:
Successful But Verbose In the end, I had to copy each directory individually, which seem verbose and non-DRY:
<copy todir="myInstance/etc">
<fileset dir="example/etc"/>
</copy>
<copy todir="myInstance/lib">
<fileset dir="example/lib" />
</copy>
<copy todir="myInstance/webapps">
<fileset dir="example/webapps" />
</copy>
提前致谢
推荐答案
您可以在文件集中指定多个包含和排除规则.如果您未指定包含规则,则默认为包含所有内容,但被排除规则至少排除一次的内容除外.
You can specify multiple inclusion and exclusion rules in a fileset. If you don't specify an inclusion rule, the default is everything is included, except anything that is excluded at least once by an exclude rule.
这是一个包容性的例子:
Here's an inclusive example:
<property name="src.dir" value="example" />
<property name="dest.dir" value="myInstance" />
<copy todir="${dest.dir}">
<fileset dir="${src.dir}">
<include name="lib/**" />
<include name="etc/**" />
<include name="webapps/**" />
</fileset>
</copy>
注意 **
通配符,它将在指定的三个前沿"子目录中的每一个下引入完整的目录树.或者,如果您想专门排除一些目录,但复制所有其他目录,您可以省略包含(从而获得默认的全包行为)并提供排除列表:
Note the **
wildcard that will bring in the full directory tree under each of the three 'leading-edge' sub-directories specified. Alternatively, if you want to specifically exclude a few directories, but copy over all others, you might omit inclusion (and thereby get the default all-inclusive behaviour) and supply a list of exclusions:
<copy todir="${dest.dir}">
<fileset dir="${src.dir}">
<exclude name="ignoreThisDir*/" />
<exclude name="ignoreThisOne*/" />
</fileset>
</copy>
您可以进一步将您给出的特定示例归结为一种排除模式:
You could further boil the particular example you gave down to one exclusion pattern:
<exclude name="ignore*/" />
这篇关于一次性复制多个目录(和内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!