Ant 查找文件所在的所有目录,以便该文件包含搜索字符串
我有一个目录树,我需要如下处理:Ant 查找文件所在的所有目录,以便该文件包含搜索字符串,ant,Ant,我有一个目录树,我需要如下处理: 我有一个特定的文件,需要复制到选定的几个子目录 感兴趣的子目录包含一个文件,我可以在其中正则表达式匹配已知的搜索字符串 理想情况下,我希望: 对目录中的所有文件执行正则表达式匹配 如果正则表达式匹配,则将文件复制到该目录 问题是我对ANT还很陌生,我很难找到自己的路。我在文档中找不到任何关于基于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可以从