[SonarQube]小结

新的项目用了这个,以前从来没用过,配置了一下,看看这个到底是个什么东东.

先学习一下英文单词:

sonar 声纳, qube 方盒子, 连起来应该叫声纳盒, SonarQube一看就是一个监测诊断设备. 官方定义是这样的,原文如下,简单来说就是一个源码分析诊断工具,诊断粒度从产品甚至到一个方法都有.

"The SonarQube® platform is an open source quality management platform, dedicated to continuously analyzing and measuring the technical quality of source code, from project portfolio down to the method level." -From http://docs.sonarqube.org

架构

在进入安装、配置、运行之前,还是有必要了解一下SonarQube(简称SQ)的架构,否则出了问题不好诊断。官方给出了详细的架构,还有2张图画的也很好,链接在此。简单来说,SQ平台本身包括server(内部有2个process,分别为web server和search server), 数据库(用来存储配置以及分析结果),众多Plugin(你可以通过update center来下载安装,或者手动安装),一个或多个Scanner(用来运行在build或者CI)。

注意:这些运行在build(如mvn,ant)上的scanner必须要有相应的plugin支持,如源码是java,则你必须下载安装Java plugin。Scanner不会直接与Database打交道,它会与SQ Server进行交互,将生成报告返回给SQ做进一步分析。注意在mvn的setting中设置了SQ Server的url, ant类似。

代码提交,trigger loadbuild, 进而驱动sonar任务执行(如mvn中是 mvn sonar:sonar),也就是scanner运行,scanner会分析报告交给SQ进行处理, SQ处理完毕呈现,程序员可以查看出现的问题(Technical Debt)。

安装

SonarQube安装很简单, 解压缩就可以,分析的结果需要有数据库的支持,所以需要配置数据库, 另外内嵌了一个web server用于管理和查看分析运行结果. 至于数据库以及web server的一些配置,如数据库用户名密码, JDBC url, web server的端口,地址以及连接池,https,update Center(这个是用来获取各种插件进行分析源码)等等. 具体看看配置文件(conf\sonar.properties)就很清楚了。如下是MySQL的设置:

1 CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
2 CREATE USER 'sonar' IDENTIFIED BY 'sonar';
3 grant all privileges on sonar.* to 'sonar'@'localhost' identified by 'sonar'; 
4 flush privileges; 

Plugin

Plugin, SonarQube有众多的plugin, 下面就那几个例子来说明一下.

Java Plugin

This plugin enables analysis of Java projects. It offers more than 300 rules provided by SonarSource, which span coding conventions, bug detection, and security problems, including coverage of some CWE rules, including several from the SANS TOP 25 Most Dangerous Software Errors, as well as some OWASP Top Ten vulnerabilities. This plugin is CWE Compatible, which means that you can search for rules in the plugin that are related to CWE items. This plugin replaces the majority of Findbugs, PMD, and Checkstyle rules.

-from http://docs.sonarqube.org

能分析Java code是否符合编码规范,潜在的bug和安全问题等,值得注意的是,该插件能替代findbugs和checkstyle中大部分功能。 

FindBugs Plugin

It requires the Java Plugin, and uses FindBugsfb-contrib and Find Security Bugs to provide coding rules.

findbugs是一个很好的工具,用过。不过这次是以SQ插件的形式,需要Java Plugin。

Checkstyle Plugin

顾名思义,code style的check工具,目前已经被java plugin替代,可以不用。

Cobertura Plugin

代码覆盖率,类似JaCoco的东西,一般我们用Junit测试时,设置了Cobertura或者JaCoco,则会统计出代码覆盖率。这个插件可以帮助把代码覆盖率的数据呈现在SQ的web page中。

Clirr Plugin

用来诊断Java Lib的兼容性,如下,可以在SQ Web->Quality Profile中进行配置。

 API Change adds new feature without breaking anything

  • API Change breaks the backward binary compatibility
  • API Change might change runtime expected behavior

关于On new Code

mvn sonar:sonar -Dsonar.projectDate=<yyyy-mm-dd>

NOTE: <yyyy-mm-dd> is the revision date when it is generated, get it from svn log

 

Reference:

Please note that Coverage on new code currently identifies new code based on the date of the previous analysis (and not the commit date of the code previously analyzed), see SONAR-7085

小结

项目中配置了自己的Quality Profile,引入统一的rule for code check.

在每一次提交代码前需要检查一下修改的代码所属的包,又没有引入新的issue;检查一下code coverage, 不能低于原来的值。就这些。

 

posted on 2016-02-14 11:15  bjfarmer  阅读(1677)  评论(0编辑  收藏  举报