Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ant 查找文件所在的所有目录,以便该文件包含搜索字符串_Ant - Fatal编程技术网

Ant 查找文件所在的所有目录,以便该文件包含搜索字符串

Ant 查找文件所在的所有目录,以便该文件包含搜索字符串,ant,Ant,我有一个目录树,我需要如下处理: 我有一个特定的文件,需要复制到选定的几个子目录 感兴趣的子目录包含一个文件,我可以在其中正则表达式匹配已知的搜索字符串 理想情况下,我希望: 对目录中的所有文件执行正则表达式匹配 如果正则表达式匹配,则将文件复制到该目录 问题是我对ANT还很陌生,我很难找到自己的路。我在文档中找不到任何关于基于regex搜索的每个目录操作的任务。我找到的最接近的东西是一个正则表达式替换任务(),它可以搜索和替换文件中的模式 这可能吗?我真的很想有一个样品开始。我很抱歉请

我有一个目录树,我需要如下处理:

  • 我有一个特定的文件,需要复制到选定的几个子目录
  • 感兴趣的子目录包含一个文件,我可以在其中正则表达式匹配已知的搜索字符串
理想情况下,我希望:

  • 对目录中的所有文件执行正则表达式匹配
  • 如果正则表达式匹配,则将文件复制到该目录
问题是我对ANT还很陌生,我很难找到自己的路。我在文档中找不到任何关于基于regex搜索的每个目录操作的任务。我找到的最接近的东西是一个正则表达式替换任务(),它可以搜索和替换文件中的模式

这可能吗?我真的很想有一个样品开始。我很抱歉请求代码-我只是不知道如何开始组合任务来实现这一点

或者,我可以选择对每个目录的所有复制操作进行硬编码,但这意味着随着项目的增长,手动保持所有操作的同步。理想情况下,我希望基于我描述的regex搜索/复制方法实现自动化


谢谢

将复制任务与文件集和正则表达式选择器一起使用:

 <copy todir="your/target/dir">
  <fileset dir="rootdir/of/your/directorytree" includes="**/*.txt">
   <containsregexp expression="[4-6]\.[0-9]"/>
  </fileset>
 </copy>

这个例子是简单的,稍加修改。
意味着选择所有扩展名为.txt的文件,这些文件位于rootdir/of/your/directorytree之外与正则表达式匹配的任何位置(后面有一个4、5或6,后跟一个句点和一个从0到9的数字),并将它们复制到/target/dir。

只需根据您的需要进行调整。

使用带有文件集和正则表达式选择器的复制任务:

 <copy todir="your/target/dir">
  <fileset dir="rootdir/of/your/directorytree" includes="**/*.txt">
   <containsregexp expression="[4-6]\.[0-9]"/>
  </fileset>
 </copy>

这个例子是简单的,稍加修改。
意味着选择所有扩展名为.txt的文件,这些文件位于rootdir/of/your/directorytree之外与正则表达式匹配的任何位置(后面有一个4、5或6,后跟一个句点和一个从0到9的数字),并将它们复制到/target/dir。

只需根据您的需要进行调整。

您的需求有点不标准,因此我使用定制解决方案

下面是一个工作示例:

<project name="find-files" default="copy-files">

    <!--
    ======================
    Groovy task dependency
    ======================
    -->
    <path id="build.path">
        <pathelement location="jars/groovy-all-1.8.6.jar"/>
    </path>
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>


    <!--
    =========================
    Search for matching files
    =========================
    -->
    <target name="search-files">
        <fileset id="filesContainingSearchString" dir="src">
            <include name="**/*.txt"/>
            <containsregexp expression="[4-6]\.[0-9]"/>
        </fileset>
    </target>

    <!--
    ===================================
    Copy file into each directory found
    ===================================
    -->
    <target name="copy-files" depends="search-files">
        <groovy>
        project.references.filesContainingSearchString.each { file ->
            def dir = new File(file.toString()).parent

            ant.copy(file:"fileToBeCopied.txt", toDir:dir)
        }
        </groovy>
    </target>

</project>

project.references.filesContainingSearchString.each{file->
def dir=新文件(File.toString()).parent
复制(文件:“fileToBeCopied.txt”,toDir:dir)
}
注意事项:

  • Groovy jar可以从

您的要求有点不标准,因此我已使用定制解决方案

下面是一个工作示例:

<project name="find-files" default="copy-files">

    <!--
    ======================
    Groovy task dependency
    ======================
    -->
    <path id="build.path">
        <pathelement location="jars/groovy-all-1.8.6.jar"/>
    </path>
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>


    <!--
    =========================
    Search for matching files
    =========================
    -->
    <target name="search-files">
        <fileset id="filesContainingSearchString" dir="src">
            <include name="**/*.txt"/>
            <containsregexp expression="[4-6]\.[0-9]"/>
        </fileset>
    </target>

    <!--
    ===================================
    Copy file into each directory found
    ===================================
    -->
    <target name="copy-files" depends="search-files">
        <groovy>
        project.references.filesContainingSearchString.each { file ->
            def dir = new File(file.toString()).parent

            ant.copy(file:"fileToBeCopied.txt", toDir:dir)
        }
        </groovy>
    </target>

</project>

project.references.filesContainingSearchString.each{file->
def dir=新文件(File.toString()).parent
复制(文件:“fileToBeCopied.txt”,toDir:dir)
}
注意事项:

  • Groovy jar可以从

那很好,但不完全是我想要的。我需要将一个文件复制到包含与正则表达式匹配的文件的每个目录。看起来是个开始。很好,但不是我想要的。我需要将一个文件复制到包含与正则表达式匹配的文件的每个目录。这似乎是一个开始。谢谢马克,我为我的目的改编了你的剧本,效果很好。谢谢马克,我为我的目的改编了你的剧本,效果很好。