Maven集成Findbugs至compile阶段(excludeFilterFile 属性必须配置在 plugin 节点下)

最近在对代码作检测,使用到了findbug插件。每次检测都是手动检测,而且都是事后想起来才检测一下。
后来,想想有没有强制每个在提交代码的时候,使用findbug检测一下呢。
由于项目是使用maven打包编译的,所以,自然而然的就想到,findbug应该也有maven的插件吧。
于是,查了一下,的确有,: )。先直接上配置吧。

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.4</version>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>findbugs-maven-plugin</artifactId>
            <configuration>
                <effort>default</effort>
                <failOnError>true</failOnError
                 <excludeFilterFile>${project.basedir}/etc/findbug/excludeFilter.xml</excludeFilterFile>
            </configuration>
            <executions>
                <execution>
                    <id>check</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

下面,稍微解释一下:

  • 先在pluginManagement声明一下findbug的插件,这样,所有子模块可以使用同一个版本的插件(即子模块使用时,不用指定version的)
  • 添加一个 execution,指定其 phasecompilegoalcheck,意思是将 findbug 插件的 check 这个动作应用到 maven 生命期的 compile 之后,这样一来,当我们使用mvn compile这样的命令之后,会自动执行 findbug 插件的 check 这个goal。

2017-03-21 修正

原来的描述

在这个execution中,我们可以添加一个 configuration 子元素来配置一些配置项。

更正后的描述

在这个plugin中,我们可以添加一个 configuration 子元素来配置一些配置项。请务必注意,只能在 plugin 节点下配置,如果配置到 execution 节点下的话,那么,excludeFilterFile 属性将无效

  • 像我上面的这个配置里,就配置了三个:effort、failOnError、excludeFilterFile。其中,前两个配置的值其实就是 findbug 插件里的默认值,这里把它们列出来,只是为了以后可能需要进行改动。最后一个配置项,是用来配置过滤掉那些不需要进行check的代码的,至于这份 filter 文件怎么写,可以参考我的另一篇blog:findbug的filter如何编写?

  • PS:我上面将 findbug 绑定到了 compile 上,所以,就自动过滤掉了所有 test 代码。至于为什么,请自行查看 maven 的生命周期:maven生命周期

最后,提一下这个 findbug 插件的官网:findbug的maven插件的官网。然后,我想说,这个插件虽然提供了不少便利,但是官网文档做得不是很好,特别是:

  • 各个goal的具体使用方式(只有粗略的使用说j)
  • 各个goal之间的区别(特别是 checkfindbug)
  • 各个goal的 configuration 中可以配置哪些参数,这些参数的意思是什么

下面我本机的一个编译过程输出。我们可以看到,check 这个 goal 是依赖 findbugs 这个 goal 的,所以,会在 check 这个 goal 之前,会先运行 findbugs 这个 goals

[wz@test test-prj]$ mvn clean compile
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building test-prj 0.9.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ test-prj ---
[INFO] Deleting /home/wz/idea_workspace/test-prj/target
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ test-prj ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] Copying 36 resources to com/cm/test/persistence
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ test-prj ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 402 source files to /home/wz/idea_workspace/test-prj/target/classes
[INFO] 
[INFO] >>> findbugs-maven-plugin:3.0.4:check (check) > :findbugs @ test-prj >>>
[INFO] 
[INFO] --- findbugs-maven-plugin:3.0.4:findbugs (findbugs) @ test-prj ---
[INFO] Fork Value is true
[INFO] Done FindBugs Analysis....
[INFO] 
[INFO] <<< findbugs-maven-plugin:3.0.4:check (check) < :findbugs @ test-prj <<<
[INFO] 
[INFO] --- findbugs-maven-plugin:3.0.4:check (check) @ test-prj ---
[INFO] BugInstance size is 0
[INFO] Error size is 0
[INFO] No errors/warnings found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:31 min
[INFO] Finished at: 2017-01-06T14:11:53+08:00
[INFO] Final Memory: 38M/430M
[INFO] ------------------------------------------------------------------------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值