如何使用ant count task countfilter

时间:2012-07-31 03:03:38

标签: regex ant

我试图计算文件中“这个构建列表是否需要编译:true ”行的出现次数。我尝试了以下代码,但它没有获取计数

<concat>
<fileset file="@{logPathInTestSetup}" />
    <filterchain>
        <tokenfilter>
            <ct:countfilter property="noOfCompiledBuildlists" contains="Does this buildlist need compile:\s*(true)" override="true">
            <ct:counteach propertyprefix="count." select="\1" />
            </ct:countfilter>
         </tokenfilter>
         <ct:stopfilter />
      </filterchain>
</concat>

当我尝试计算“这个构建列表是否需要编译:”时,计数器工作,我得到正确的值。所以正则表达式确实存在问题。有人可以帮忙吗? 谢谢, Aarthi

1 个答案:

答案 0 :(得分:3)

您不需要额外的任务,但作为<concat>的Ant 1.7.1+必须是资源 以下是来自ant manual resourcecount的略微改编的示例 给定输入,foobar.txt:

Does this buildlist need compile: true
whatever
Does this buildlist need compile: false
The quick brown fox
jumps over the lazy dog
Does this buildlist need compile: true
Does this buildlist need compile: false
Does this buildlist need compile: true
foo
blablabla
Does this buildlist need compile: true

示例蚂蚁脚本:

<project>
 <property name="file" value="foobar.txt"/>
  <resourcecount property="file.lines">
   <tokens>
    <concat>
     <filterchain>
      <linecontainsregexp>
       <regexp pattern="Does this buildlist need compile:\s*(true)"/>
      </linecontainsregexp>
     </filterchain>
     <fileset file="${file}"/>
    </concat>
   </tokens>
  </resourcecount>
  <echo>The file '${file}' has ${file.lines} lines.</echo>
</project>

输出:

[echo] The file 'foobar.txt' has 4 lines.


修改
要获得不匹配的行,意味着否定正则表达式,只需使用:

</linecontainsregexp negate="true">