SpotBugs安装及使用


SpotBugs是Findbugs的继任者,Findbugs已经不再维护,不过SpotBugs继承了绝大部分Fingbugs的代码结构,他们的原理基本相同,同样利用了BCEL字节码程序设计库,这个设计库专门用于分析java字节码,且都是基于观察者模式并给予实现。

介绍

官网网站

文档

SpotBugs是Findbugs的继任者(Findbugs已经不再维护),具体介绍可以参见Findbugs的安装及使用一文。

SpotBugs能够分析使用java1.0到1.9编译的程序,具备良好的扩展性,可以自定义bug pattern。

安装及使用

安装

  1. 下载二进制发行版本tarzip
  2. 解压并配置环境变量
  3. 直接运行bin目录下的可执行程序

使用

官方使用描述

SpotBugs非插件版本支持gui和命令行两种使用方式,默认为gui,具体参数及使用说明参见官方使用描述。

注意:要分析的文件放在命令行的最后。

例:spotbugs -textui -low -html -output result.html /path/to/src

扩展

SpotBugs具备良好的扩展性,将自定义的检测器工程打成jar包放置在plugin文件夹下便可以直接使用。

比较流行的插件:fb-contrib
Find Security Bugs

也可以自定义插件

  1. 使用spotbugs-archetype创建maven工程,填写groupid,artifitid,package name以及initial version

    1
    2
    3
    4
    $ mvn archetype:generate \
    -DarchetypeArtifactId=spotbugs-archetype \
    -DarchetypeGroupId=com.github.spotbugs \
    -DarchetypeVersion=0.2.0
  2. 运行完上述命令便会创建好一个maven工程,这时候可以在src/test/java文件夹下添加自定义的检测器并进行测试

  3. 在src/main/resources目录下添加fingbugs.xml文件说明:

    1
    2
    3
    4
    <!-- fingbugs.xml -->
    <Detector class="com.github.plugin.MyDetector" reports="MY_BUG" speed="fast" />

    <BugPattern type="MY_BUG" category="CORRECTNESS" />
  4. 在src/main/resources目录下添加messages.xml文件说明:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <!-- messages.xml -->
    <Detector class="com.github.plugin.MyDetector">
    <Details>
    Original detector to detect MY_BUG bug pattern.
    </Details>
    </Detector>


    <BugPattern type="MY_BUG">
    <ShortDescription>Explain bug pattern shortly.</ShortDescription>
    <LongDescription>
    Explain existing problem in code, and how developer should improve their implementation.
    </LongDescription>
    <Details>
    <![CDATA[
    <p>Explain existing problem in code, and how developer should improve their implementation.</p>
    ]]>
    </Details>
    </BugPattern>
  5. 打jar包,参考
    命令行mvn打包
    ,将target文件夹下的jar包放入spotbugs根目录下的plugin文件夹即可。

-------------本文结束感谢您的阅读-------------

本文标题:SpotBugs安装及使用

文章作者:ChengXiao

发布时间:2018年07月06日 - 09:07

最后更新:2019年09月06日 - 22:09

原始链接:http://chengxiao19961022.github.io/2018/07/06/SpotBugs安装及使用/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

你的鼓励是我前进的动力~