FindBugs过滤器文件,用于忽略JUnit测试

时间:2009-04-16 14:56:53

标签: java ant junit findbugs

我需要为我的findbugs ant脚本设置一个过滤器文件,它只扫描src / *文件,而不扫描test / *文件。

检查所有类的语法是什么,而忽略名称中带有'test'的任何文件名或包名?

2 个答案:

答案 0 :(得分:23)

FindBugs实际上是扫描已编译的类文件,而不是sourcePath。如果要将src / *和test / *文件编译到不同的目录,则可以使用嵌套的<class...>元素。

<findbugs home="${findbugs.dir}" output="xml:withMessages" 
    outputFile="${findbugs.report.xml}" jvmargs="-Xmx256M" 
    effort="max" projectName="${ant.project.name}" 
    auxClasspathRef="findbugs.classpath" 
    sourcePath="${src.dir}">
  <class location="${src.classes.dir}"/>
</findbugs> 

如果将src / *和test / *都编译为单个目录,那将无效。在这种情况下,请使用filter file并排除与测试对应的包或类名。

<findbugs home="${findbugs.dir}" output="xml:withMessages" 
    outputFile="${findbugs.report.xml}" jvmargs="-Xmx256M" 
    effort="max" projectName="${ant.project.name}" 
    auxClasspathRef="findbugs.classpath" 
    sourcePath="${src.dir}"
    excludefilter="exclude.xml">
  <class location="${classes.dir}"/>
</findbugs> 

exclude.xml的样子:

<FindBugsFilter>
  <Match>
    <Class name="~.*Test$"/>
  </Match>
  <Match>
    <Package name="~test\..*"/>
  </Match>
</FindBugsFilter>

答案 1 :(得分:-1)

顺便说一下,使用FindBugs覆盖单元测试是一个好主意。没有理由对测试使用较低的质量标准。测试中的错误只是错误。

当然,如果您第一次运行FindBugs,可能会有很多错误报告,但如果您对它们有任何关注,错误计数将会超时。