Ant 删除与另一目录树中的占位符匹配的文件

Ant 删除与另一目录树中的占位符匹配的文件,ant,Ant,我有两个目录树: source/aaa/bbb/ccc/file01.txt source/aaa/bbb/file02.txt source/aaa/bbb/file03.txt source/aaa/ddd/file03.txt source/file01.txt 及 使用Ant,我想做三件事。首先,我想将任何文件从“模板”复制到“源”中,以便替换所有不以“删除-”开头的文件。例如,“source/aaa/bbb/ccc/file01.txt”将被替换。这一点很简单: <copy t

我有两个目录树:

source/aaa/bbb/ccc/file01.txt
source/aaa/bbb/file02.txt
source/aaa/bbb/file03.txt
source/aaa/ddd/file03.txt
source/file01.txt

使用Ant,我想做三件事。首先,我想将任何文件从“模板”复制到“源”中,以便替换所有不以“删除-”开头的文件。例如,“source/aaa/bbb/ccc/file01.txt”将被替换。这一点很简单:

<copy todir="source" verbose="true" overwrite="true">
    <fileset dir="template">
        <exclude name="**/DELETE-*"/>
    </fileset>
</copy>

其次,我想删除“source”树中的所有文件,这些文件的名称与“template”树对应目录中的“delete-”文件匹配。例如,“source/aaa/bbb/file03.txt”和“source/file01.txt”都将被删除。我通过以下方式实现了这一目标:

<delete verbose="true">
    <fileset dir="source">
        <present present="both" targetdir="template">
            <mapper type="regexp" from="(.*[/\\])?([^/\\]+)" to="\1DELETE-\2"/>
        </present>
    </fileset>
</delete>   

第三,我想删除任何名称以相同方式匹配的目录(空或不空)。例如,“模板/aaa/删除ddd”及其下的所有文件都将被删除。我不知道如何构造一个与“源”树中的目录(及其下的所有文件)匹配的文件集,该目录在“模板”树中有一个DELETE-*文件


使用Ant(1.7.1)是否可以执行第三个任务?我更愿意在不编写任何自定义ant任务/选择器的情况下执行此操作。

要删除目录及其内容,请使用带嵌套文件集的
删除,即:

 <delete includeemptydirs="true">
  <fileset dir="your/root/directory" defaultexcludes="false">
   <include name="**/DELETE-*/**" />
  </fileset>
 </delete>


使用属性
includeemptydirs=“true”
也将删除目录。

要删除目录及其内容,请使用
删除嵌套文件集,即:

 <delete includeemptydirs="true">
  <fileset dir="your/root/directory" defaultexcludes="false">
   <include name="**/DELETE-*/**" />
  </fileset>
 </delete>


使用attribute
includeemptydirs=“true”
时,目录也将被删除。

使这一点变得困难的根本问题似乎是ant基于文件集的目标目录中找到的文件驱动选择器/文件集。但是,通常情况下,您会希望从DELETE-*标记文件列表中驱动内容

到目前为止,我找到的最佳解决方案确实需要一些自定义代码。我选择了
任务,但也可以使用

要点:创建一个文件集,使用groovy添加一系列排除项,用DELETE-*标记跳过文件和目录,然后执行复制。这完成了我问题的第二和第三个任务

<fileset id="source_files" dir="source"/>

<!-- add exclude patterns to fileset that will skip any files with a DELETE-* marker -->
<groovy><![CDATA[
    def excludes = []
    new File( "template" ).eachFileRecurse(){ File templateFile ->
        if( templateFile.name =~ /DELETE-*/ ){
            // file path relative to template dir
            def relativeFile = templateFile.toString().substring( "template".length() )
            // filename with DELETE- prefix removed
            def withoutPrefix = relativeFile.replaceFirst( "DELETE-", "")
            // add wildcard to match all files under directories
            def exclude = withoutPrefix + "/**"
            excludes << exclude
        }
    }
    def fileSet = project.getReference("source_files")
    fileSet.appendExcludes(excludes as String[])
]]></groovy>

<!-- create a baseline copy, excluding files with DELETE-* markers in the template directories -->
<copy todir="target">
    <fileset refid="source_files"/>
</copy>

if(templateFile.name=~/DELETE-*/){
//相对于模板目录的文件路径
def relativeFile=templateFile.toString().substring(“template”.length())
//删除前缀的文件名-已删除
def withoutPrefix=relativeFile.replaceFirst(“删除-”,“”)
//添加通配符以匹配目录下的所有文件
def exclude=withoutPrefix+“/**”
排除

似乎让这一点变得困难的根本问题是ant基于文件集的目标目录中找到的文件驱动选择器/文件集。但是,通常情况下,人们希望从DELETE-*标记文件列表中驱动内容

到目前为止,我找到的最佳解决方案确实需要一些自定义代码。我选择了
任务,但也可以使用

要点:创建一个文件集,使用groovy添加一系列排除项,这些排除项使用DELETE-*标记跳过文件和目录,然后执行复制。这完成了我问题中的第二个和第三个任务

<fileset id="source_files" dir="source"/>

<!-- add exclude patterns to fileset that will skip any files with a DELETE-* marker -->
<groovy><![CDATA[
    def excludes = []
    new File( "template" ).eachFileRecurse(){ File templateFile ->
        if( templateFile.name =~ /DELETE-*/ ){
            // file path relative to template dir
            def relativeFile = templateFile.toString().substring( "template".length() )
            // filename with DELETE- prefix removed
            def withoutPrefix = relativeFile.replaceFirst( "DELETE-", "")
            // add wildcard to match all files under directories
            def exclude = withoutPrefix + "/**"
            excludes << exclude
        }
    }
    def fileSet = project.getReference("source_files")
    fileSet.appendExcludes(excludes as String[])
]]></groovy>

<!-- create a baseline copy, excluding files with DELETE-* markers in the template directories -->
<copy todir="target">
    <fileset refid="source_files"/>
</copy>

if(templateFile.name=~/DELETE-*/){
//相对于模板目录的文件路径
def relativeFile=templateFile.toString().substring(“template”.length())
//删除前缀的文件名-已删除
def withoutPrefix=relativeFile.replaceFirst(“删除-”,“”)
//添加通配符以匹配目录下的所有文件
def exclude=withoutPrefix+“/**”
排除

我想你误解了我的问题。我想从中删除文件/目录(源)的树与包含delete-*文件(模板)的树不同。我想你误解了我的问题。我想从中删除文件/目录(源)的树与包含delete-*文件的树不同(模板)。