wiki:sonar

Sonar

  •  官方网站
  • Sonar是代码质量管理的开放平台,结合测试工具、代码分析工具。
  • 主要作用是通过各种量化的分析数据,不仅仅是静态bug分析,更多是代码整体结构和设计方面,长期对项目进行分析和监控,方便大家更加直观去评估和持续改善代码质量。
  • 图书资料:Sonar Code Quality Testing Essentials.rar Download

Install

  1. 下载及安装  http://docs.codehaus.org/display/SONAR/Install+Sonar
  2. 修改/conf/sonar.properties:
    sonar.jdbc.username:                       sonar
    sonar.jdbc.password:                       sonar
    sonar.jdbc.url:                            jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
    sonar.jdbc.driverClassName:                com.mysql.jdbc.Driver
    
  3. 修改maven配置settings.xml
            <profile>
                <id>sonar</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <!-- EXAMPLE FOR MYSQL -->
                    <sonar.jdbc.url>
                      jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
                    </sonar.jdbc.url>
                    <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>
                    <sonar.jdbc.username>sonar</sonar.jdbc.username>
                    <sonar.jdbc.password>sonar</sonar.jdbc.password>
    
                    <!-- optional URL to server. Default value is http://localhost:9000 -->
                    <sonar.host.url>
                      http://localhost:9000
                    </sonar.host.url>
                </properties>
            </profile>
    
  4. 添加项目到sonar:
    mvn sonar:sonar
    

Install Plugin

  1. 下载插件jar包并复制到 /extensions/plugins/ 目录
  2. 重启sonar

Upgrade Sonar

 http://docs.codehaus.org/display/SONAR/Upgrade+guide

  1. 下载新版本,解压到新目录
  2. 停止sonar服务
  3. 复制原配置文件去新目录 sonar.properties and wrapper.conf
  4. 复制插件和自定义规则去新目录 extensions/plugins and extensions/rules
  5. 如果用了自己的jdbc驱动,也复制去新目录 /extensions/jdbc-driver/
  6. 启动新版本,打开 http://localhost:9000/setup 完成剩余步骤

Review

  1. 建议安装2.14版本,因为从此版本开始可以在任意代码段发起review,之前版本只支持在扫描出问题的代码段review。
  2. 如果在general settings中配置了邮箱,并且其他用户也配置自己的邮箱,在分发review的时候可以发邮件通知对方。
  3. review必须指定一个原因,可以自定义。
  4. 毕竟代码审核不是sonar的重点功能,使用起来不顺手,特别是没法针对具体的svn提交来审核,推荐别的代码审核工具例如电脑组正在使用的: ReviewBoard

JPA Related

JPA单元测试手动执行mvn test都是成功的,但是集成在sonar中总是会报错

Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: cn.com.pconline.enterprise.kuaisou.ProductSearcher
cn.com.pconline.enterprise.service.BuildIndexService.productSearcher;
nested exception is java.lang.IllegalArgumentException:
Can not set cn.com.pconline.enterprise.kuaisou.ProductSearcher field
cn.com.pconline.enterprise.service.BuildIndexService.productSearcher to $Proxy22

经过查证发现是cobertura-maven-plugin插件的影响,spring的@Autowire是按类型注入的,而cobertura 在Instrumentation步骤中会对类型有修改,导致spring的注入有问题,对于商城这样复杂的注入情况来说暂时没有好的办法,只能先去掉代 码覆盖率的检测步骤。  http://blog.anorakgirl.co.uk/2010/01/maven-junit-cobertura-and-beancreationexception/

具体处理:

  1. pom.xml里不要指定build阶段的cobertura-maven-plugin插件。
  2. sonar的具体JPA项目settings中,sonar.core.codeCoveragePlugin设为false,缺省用是cobertura

LCOM4

  1. Lack of Cohesion Among Method of Class
  2. 帮助分析类的职责,数值越大表示该类可拆分的部分越多。

Design

  1. 包的依赖关系图
  2. 目标是单向依赖
  3. 避免依赖循环

Violations

  1. 代码质量分析
  2. findbug、pmd等可配置插件
  3. 自定义rule
  4. 严重等级
  5. 发起review

Unit Test

  1. 自动运行unit test
  2. 自动分析测试覆盖率
  3. 可以手动指定测试报告

SCM Plugin

  1. 需要scm插件
  2. 配置scm
  3. 按时间统计提交数
  4. 浏览代码时显示提交人和时间

Attachments