使用Ant Script将单个文件复制到某个目录下的所有子目录


Copy a single file to all sub directories under a certain directory using Ant Script

如何使用Ant制作脚本,将单个文件index.html复制到目录的所有子目录中。

我正在尝试构建一个Ant脚本,以便在所有子目录上复制index.html。如果该文件存在于特定的子目录中,则应替换该文件。

我的初始结构:

Directory A
   Sub-Directory AA
       file1
       file2
       ...
   Sub-Directory AB
       file1
       file2
       index.html
       ...
      Sub-Directory ABA
          file1
          file2
          ...
    ....

我想实现这个:

Directory A
   Sub-Directory AA
       file1
       file2
       index.html  <- file copied
       ...
   Sub-Directory AB
       file1
       file2
       index.html  <- file replaced
       ...
      Sub-Directory ABA
          file1
          file2
          index.html  <- file copied
          ...
    ....

提前谢谢。当做

以下示例使用groovy ANT任务。

示例

├── build.xml
├── lib
│   └── groovy-all-2.1.6.jar
├── src
│   └── index.html
└── target
    └── DirectoryA
        ├── index.html
        ├── SubdirectoryAA
        │   └── index.html
        └── SubdirectoryAB
            ├── index.html
            └── SubdirectoryABA
                └── index.html

build.xml

<project name="demo" default="copy">
   <path id="build.path">
      <pathelement location="lib/groovy-all-2.1.6.jar"/>
   </path>
   <target name="copy">
      <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
      <dirset id="trgDirs" dir="target/DirectoryA"/>
      <groovy>
         project.references.trgDirs.each {
            ant.copy(file:"src/index.html", todir:it, overwrite:true, verbose:true)
         }
      </groovy>
   </target>
</project>

注:

  • groovy脚本能够循环遍历ANT目录集中包含的目录
  • Groovy可以访问正常的ANT任务。请注意复制操作上的"覆盖"标志