【问题标题】:Ant read files in a directory and regexAnt读取目录中的文件和正则表达式
【发布时间】:2013-08-14 06:16:29
【问题描述】:

我正在尝试读取目录中一组文件的名称,并将正则表达式应用于这些名称并获取带有逗号分隔值的列表。文件的名称格式为 build_level1_D1.properties、build_level1.D2.properties、build_level2.D1.properties 等... 我需要读取所有文件名并应用正则表达式并解析名称以获得 level1_D1、level1_D2、level2_D1 等。我需要它的格式为 property name="build.levels" value="level1_D1,level1_D2,level2_D1" 这个是我试过的。需要一些指导和帮助。

    <target name="build-levels-all">
            <for param="program">
                <path><fileset dir="${root.build.path}/build" includes="*"/>
                </path>
                 <sequential>
                    <propertyregex override="yes" property="file" input="@{program}" regexp="build\_([^\.]*)" select="\1" />
                    <echo>${file}</echo>
                 </sequential> 
            </for>
            <echo>${program}</echo>
<-- This prints the files regexed Level1_D1, level2_D2 etc....But i need to capture it in the format of <property name="build.levels" value="level1_D1,level1_D2,level2_D1" /> -->
            </target>

【问题讨论】:

    标签: ant ant-contrib


    【解决方案1】:

    尝试使用嵌入式脚本语言,例如groovy,来完成这种复杂的逻辑。

       <target name="process-files">
          <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
    
          <groovy>
             def list = []
    
             new File('build').eachFile() {
                def matcher = it.name =~ /(build_level\d_D\d).properties/
                list.add matcher[0][1]
             }
    
             properties."build.levels" = list.join(",")
          </groovy>
       </target>
    
       <target name="doSomething" depends="process-files">
          <echo>${build.levels}</echo>
       </target>
    

    就像 ant-contrib 一样,groovy 需要一个额外的 jar。我通常包含一个“引导”目标来安装它:

       <target name="bootstrap">
          <mkdir dir="${user.home}/.ant/lib"/>
          <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.6/groovy-all-2.1.6.jar"/>
       </target>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      相关资源
      最近更新 更多