Sonar
- 官方网站
- Sonar是代码质量管理的开放平台,结合测试工具、代码分析工具。
- 主要作用是通过各种量化的分析数据,不仅仅是静态bug分析,更多是代码整体结构和设计方面,长期对项目进行分析和监控,方便大家更加直观去评估和持续改善代码质量。
- 图书资料:Sonar Code Quality Testing Essentials.rar
Install
- 下载及安装 http://docs.codehaus.org/display/SONAR/Install+Sonar
- 修改/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
- 修改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&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> - 添加项目到sonar:
mvn sonar:sonar
Install Plugin
- 下载插件jar包并复制到 /extensions/plugins/ 目录
- 重启sonar
Upgrade Sonar
http://docs.codehaus.org/display/SONAR/Upgrade+guide
- 下载新版本,解压到新目录
- 停止sonar服务
- 复制原配置文件去新目录 sonar.properties and wrapper.conf
- 复制插件和自定义规则去新目录 extensions/plugins and extensions/rules
- 如果用了自己的jdbc驱动,也复制去新目录 /extensions/jdbc-driver/
- 启动新版本,打开 http://localhost:9000/setup 完成剩余步骤
Review
- 建议安装2.14版本,因为从此版本开始可以在任意代码段发起review,之前版本只支持在扫描出问题的代码段review。
- 如果在general settings中配置了邮箱,并且其他用户也配置自己的邮箱,在分发review的时候可以发邮件通知对方。
- review必须指定一个原因,可以自定义。
- 毕竟代码审核不是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/
具体处理:
- pom.xml里不要指定build阶段的cobertura-maven-plugin插件。
- sonar的具体JPA项目settings中,sonar.core.codeCoveragePlugin设为false,缺省用是cobertura
LCOM4
- Lack of Cohesion Among Method of Class
- 帮助分析类的职责,数值越大表示该类可拆分的部分越多。
Design
- 包的依赖关系图
- 目标是单向依赖
- 避免依赖循环
Violations
- 代码质量分析
- findbug、pmd等可配置插件
- 自定义rule
- 严重等级
- 发起review
Unit Test
- 自动运行unit test
- 自动分析测试覆盖率
- 可以手动指定测试报告
SCM Plugin
- 需要scm插件
- 配置scm
- 按时间统计提交数
- 浏览代码时显示提交人和时间
Attachments
-
Sonar Code Quality Testing Essentials.rar
(3.7 MB) -
added by chenchongqi 14 years ago.
![(please configure the [header_logo] section in trac.ini)](http://www1.pconline.com.cn/hr/2009/global/images/logo.gif)