Skip to content
Calvin edited this page Nov 13, 2013 · 41 revisions

##Overview Sonar -- an open platform to manage code quality. 是SCM, CI, Issue Tracker和Wiki外,另一个必不可少的项目管理软件.

什么maven site, 什么Jenkins插件,和它比都弱爆了。

Configuration

每次升级Sonar版本,总要做的几样配置,Sonar backup只能备份所有profile的rule和alert,以及Global Settings。

  • DashBoard: 在首页点击edit filter, 删除Versions,Links,BuildDates,加入Critical/Major/Minor violations,Coverage。其他如Skip UnitTest,Duplication可以根据需要添加。
    另外,对于Multi-Module的项目,可以新建一个Share的Filter, 选择显示subproject。
  • Widgets: 在任一项目页点击Configure widgets, 删除LCOM4, Complexity和Descritpion。再把Events移到右边。再添加TimeLine(加入如Coverage,Rules compliance的Metrics)
  • 调整分值: 直接加大2倍:在General Settings->General中,INFO=0;MINOR=3;MAJOR=9;CRITICAL=15;BLOCKER=30
  • 在UpdateCenter安装SCM插件, 并依照Guides设置,能方便看出是谁造成的violations。
  • 在UpdateCenter安装TagList插件, 统计代码中遗留的//TODO等标签。按照Guide设置,需要把checkstyle里的默认值"TODO:"改为"TODO",否则会有Null Point Exception。最后在DashBoard里增加Tag的值,在Configure widgets中增加taglist的wigget。

SpringSide Profile

SpringSide的Profile在Soanr 3.2 的Sonar way with Findbugs规则上进行修改。 规则已导出到support/sonar目录里,既有可用来整体导入的springside-way.xml,也有可用于其他checkstyle/pmd/findbugs工具的独立配置文件。

###Checkstyle changes

  • Enable "JavaNCSS", classMaximum from 1500 to 500, fileMaximum from 2000 to 600-- 排除注释后的每个方法/类的最大代码行数。methodMaximum保持50的默认值。
  • Enable "Nested For Depth", max from 1 to 3 -- if/else/for/while等嵌套代码的层数。
  • Enalbe "Parameter Number", max from 7 to 5 -- 长长的函数参数列表从来都最让人烦,从默认的最多7个进一步降到5个。
  • Change "Magic Number" from Minor to Info --本意很好,但无奈太多各种可允许的例外情况,降到Info级别,有空的时候可以看一眼。
  • Disable "Hide Utility Class Constructor" -- 本意很好,只提供静态方法的工具类不应该能实例化。但无论是把类设为abstract还是添加private的构造函数都不够优雅,都是静态函数的也不一定是工具类。还是相信现在的程序员不会new一个变量来调用static方法算了。

可选修改:

  • Enable "Declaration Order", from Info to Minor -- 按Java编程规范,变量定义先public再protected再private变量再构造函数再普通函数是个挺好的实践,所以提升到Minor级别。
  • Enalbe "One Statement Per Line" -- 只有恐怖的C++才允许一行里面写两句话,如 x = 1; y = 2; Multiple Variable Declarations
  • Change "Redundant Throws" allow uncheked=true -- 在throws里把可能抛出来的unchecked exception也列出来绝对是个很Nice的行为,不应该被惩罚。
  • Disable "Visibility Modifier" -- 成员变量只能是private/protected,这条规则的唯一作用是提醒那些忘记写private,变成了package share level的变量,但其实也无关痛痒。而且个人更喜欢在POJO里直接public field,取消掉getter/setter。
  • Disable "Final Class" -- "class which has only private constructors is declared as final", 作用不大干脆不要了。
  • Disable "Trailing Comment" -- 在同一样里用//写注释,有时也不是问题。

###PMD changes

  • Disable "Avoid Throwing Raw Exception Types" -- 不允许直接throw Exception/RunTimeException,只能throw它们的子类,特烦Java这些exception设计,所以取消掉。
  • Disable "Signature Declare Throws Exception" -- 不允许定义throws Exception, 理由同上。

可选修改:

  • Enable "Add Empty String" -- 禁止通过""+i,将i转为String这种不高效的做法。
  • Enalbe "Assignment In Operand" -- 最怕C++那种一行代码又比较又赋值的神级代码。
  • Enable "Avoid Multiple Unary Operators" --同上,不要有超过一个的符号,还是那种C++气息的 int i = ~-2;
  • Enable "Misplaced Null Check" -- 智能判断if (object1!=null && object2.equals(object1)) 这种错误的null check代码。
  • Enalbe "Package case" -- package名应该全小写。
  • Enable "Use Proper Class Loader" --在JavaEE环境,应该用Thread.currentThread().getContextClassLoader() 来取得classloader
  • Enable "Unnecessary Return" -- 不必要的return函数。
  • Disable "Unnecessary Local Before Return" -- 有时候多一个本地变量是为了代码更加好读。

###FindBugs changes

  • Disable "Bad practice - Method invoked that should be only be invoked inside a doPrivileged block" -- 反射的时候很少会这么特殊处理,除非你是在一个SecurityManager严格的环境。
  • Disable "Correctness - Field not initialized in constructor" -- 太严格了,想象不到理由。
  • Disable "Reliance on default encoding" --不是每个项目都需要不停考虑encoding的问题。

可选修改:

  • Disable "Malicious code vulnerability - May expose internal representation by incorporating reference to mutable object" -- 从函数里面返回一个数组之类的就被说了,严格来说可能存在问题,但一般代码不用太管它吧。
  • Disable "Malicious code vulnerability - May expose internal representation by returning reference to mutable object" -- 同上。
  • Diable "Dodgy - Exception is caught when Exception is not thrown", 又一条没什么用的异常规则

###Sonar Rules changes

  • Enable "Avoid use of deprecated method"

###告警阀值

  • Rules compliance : 95% warning, 90% error
  • Coverage : 75% warning, 50% error

##Tips

  1. 现在30天/15天/最近新增的violations是最有用的功能。 特别是对付有海量遗留问题的时候,可以只关注最近新增的violations, 保证不会引入新的violations。

  2. 对于不想计入覆盖率统计的,可以修改项目pom.xml中的cobertura插件的配置,比如我个人就不喜欢计算Entity与Controller。

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>cobertura-maven-plugin</artifactId>
	<configuration>
		<instrumentation>
			<excludes>
				<exclude>**/entity/*.class</exclude>
				<exclude>**/*Controller.class</exclude>
			</excludes>
		</instrumentation>
	</configuration>
</plugin>