蚂蚁的contrib:在“resourcecount”嵌套上述&lt得到计数结果;对于/>循环 [英] ant-contrib: get count result in 'resourcecount' nested in <for/> loop

查看:250
本文介绍了蚂蚁的contrib:在“resourcecount”嵌套上述&lt得到计数结果;对于/>循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要搜索的文件集以下ANT似乎做的工作多searchStrings。

To search for multiple 'searchStrings' in a fileset the following ANT seems to do the job.

  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="/usr/share/java/ant-contrib.jar"/>
    </classpath>
  </taskdef>

  <loadfile property="list"  srcFile="searchStrings.txt"/>
  <echo>.:${list}:.</echo>

  <target name="main">
    <for list="${list}" param="search4"  delimiter="${line.separator}">

      <sequential>

        <fileset id="existing" dir="../src">
          <patternset id="files">
            <include name="**/*.xul"/>
          </patternset>
        </fileset>

        <resourcecount property="count">
          <fileset id="matches" dir="../src">
            <patternset refid="files" />
            <contains text="@{search4};" />
          </fileset>
        </resourcecount>

        <echo message="Found '@{search4}' in files : '${count}'"/>
      </sequential>

    </for>
  </target>

但是,对于每个单独的searchString的我需要出现的次数。回声消息发现......只给出了第一个结果,更糟糕的是,它重复所有文件数量。

But for each individual searchString I need the number of occurrences. The echo message "Found ..." only gives the very first result and more worse, it repeats that number for all files.

尝试添加该resourcecount块内的回声,但失败了。

Tried to add an echo inside the resourcecount block, but that fails.

如何才能实现这一修改,以获取所有searchStrings列表?

How can this be modified to get a list for all searchStrings?

推荐答案

ANT是不是一个脚本语言,我会建议嵌入类似的常规

ANT is not a scripting language, I would recommend embedding something like groovy

这个例子计算的搜索字词occurances,存储在searchStrings.txt的文件,包含在src目录下存储的文件。

This example counts the occurances of search terms, stored in the "searchStrings.txt" file, contained files stored under the "src" directory.

├── build.xml
├── searchStrings.txt
└── src
    ├── test1.xul
    ├── test2.xul
    └── test3.xul

运行如下:

$ ant

search:
   [groovy] Found hello in files : 3
   [groovy] Found world in files : 2
   [groovy] Found test in files : 1

的build.xml

<project name="demo" default="search">

   <target name="bootstrap" description="Used to install the ivy task jar">
      <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.9/groovy-all-2.1.9.jar"/>
   </target>

   <target name="search">
      <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

      <fileset id="existing" dir="src" includes="**/*.xul"/>

      <groovy>
         new File("searchStrings.txt").eachLine { term ->
            def files = project.references.existing.findAll{
               new File(it.toString()).text.contains(term)
            } 
            println "Found ${term} in files : ${files.size}"
         }
      </groovy>
   </target>

</project>

searchStrings.txt

hello
world
test

的src /测试* .xul

虚拟数据文件:

$ cat src/test1.xul
hello world test

$ cat src/test2.xul
hello world

$ cat src/test3.xul
hello

更新1

若要使用Eclipse这项工作,改变的taskdef包括将下载的常规-all.jar在。

Update 1

To make this work with Eclipse, alter the taskdef to include the path to the downloaded "groovy-all.jar".

  <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy">
     <classpath>
        <fileset dir="${user.home}/.ant/lib" includes="*.jar"/>
     </classpath>
  </taskdef>

请注意:


  • 这个解决方案并不需要蚂蚁的contrib。随着嵌入式常规您可以有更多的灵活性。

该溶液可容易地增强,以写在搜索结果到一个文件:

The solution can be easily enhanced to write the search results to a file:

<groovy>
  new File("searchResults.txt").withWriter { writer ->
    new File("searchStrings.txt").eachLine { term ->
      def files = project.references.existing.findAll{
        new File(it.toString()).text.contains(term)
      }
      writer.println "Found ${term} in files : ${files.size}"
    }
  }
</groovy>

Groovy是强大的。

Groovy is powerful.

这篇关于蚂蚁的contrib:在“resourcecount”嵌套上述&lt得到计数结果;对于/&GT;循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆