| | 5 | |
| | 6 | == Install == |
| | 7 | 1. 下载及安装 [http://docs.codehaus.org/display/SONAR/Install+Sonar] |
| | 8 | 2. 修改/conf/sonar.properties: |
| | 9 | {{{ |
| | 10 | sonar.jdbc.username: sonar |
| | 11 | sonar.jdbc.password: sonar |
| | 12 | sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 |
| | 13 | sonar.jdbc.driverClassName: com.mysql.jdbc.Driver |
| | 14 | }}} |
| | 15 | 3. 修改maven配置settings.xml |
| | 16 | {{{ |
| | 17 | <profile> |
| | 18 | <id>sonar</id> |
| | 19 | <activation> |
| | 20 | <activeByDefault>true</activeByDefault> |
| | 21 | </activation> |
| | 22 | <properties> |
| | 23 | <!-- EXAMPLE FOR MYSQL --> |
| | 24 | <sonar.jdbc.url> |
| | 25 | jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 |
| | 26 | </sonar.jdbc.url> |
| | 27 | <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName> |
| | 28 | <sonar.jdbc.username>sonar</sonar.jdbc.username> |
| | 29 | <sonar.jdbc.password>sonar</sonar.jdbc.password> |
| | 30 | |
| | 31 | <!-- optional URL to server. Default value is http://localhost:9000 --> |
| | 32 | <sonar.host.url> |
| | 33 | http://localhost:9000 |
| | 34 | </sonar.host.url> |
| | 35 | </properties> |
| | 36 | </profile> |
| | 37 | }}} |
| | 38 | 4. 添加项目到sonar: |
| | 39 | {{{ |
| | 40 | mvn sonar:sonar |
| | 41 | }}} |
| | 42 | |
| | 43 | == Install Plugin == |
| | 44 | 1. 下载插件jar包并复制到 /extensions/plugins/ 目录 |
| | 45 | 2. 重启sonar |
| | 46 | |
| | 47 | == Upgrade Sonar == |
| | 48 | [http://docs.codehaus.org/display/SONAR/Upgrade+guide] |
| | 49 | |
| | 50 | 1. 下载新版本,解压到新目录 |
| | 51 | 2. 停止sonar服务 |
| | 52 | 3. 复制原配置文件去新目录 sonar.properties and wrapper.conf |
| | 53 | 4. 复制插件和自定义规则去新目录 extensions/plugins and extensions/rules |
| | 54 | 5. 如果用了自己的jdbc驱动,也复制去新目录 /extensions/jdbc-driver/ |
| | 55 | 6. 启动新版本,打开http://localhost:9000/setup 完成剩余步骤 |
| | 56 | |
| | 57 | == Review == |
| | 58 | 1. 建议安装2.14版本,因为从此版本开始可以在任意代码段发起review,之前版本只支持在扫描出问题的代码段review。 |
| | 59 | 2. 如果在general settings中配置了邮箱,并且其他用户也配置自己的邮箱,在分发review的时候可以发邮件通知对方。 |
| | 60 | 3. review必须指定一个原因,可以自定义。 |
| | 61 | 4. 毕竟不是代码审核不是sonar的重点功能,使用起来不顺手,特别是没法针对具体的svn提交来审核,推荐别的代码审核工具例如电脑组正在使用的:[http://192.168.21.234:8080/r/ ReviewBoard]。 |
| | 62 | |
| | 63 | == JPA Related == |
| | 64 | JPA单元测试手动执行mvn test都是成功的,但是集成在sonar中总是会报错 |
| | 65 | {{{ |
| | 66 | Caused by: org.springframework.beans.factory.BeanCreationException: |
| | 67 | Could not autowire field: cn.com.pconline.enterprise.kuaisou.ProductSearcher |
| | 68 | cn.com.pconline.enterprise.service.BuildIndexService.productSearcher; |
| | 69 | nested exception is java.lang.IllegalArgumentException: |
| | 70 | Can not set cn.com.pconline.enterprise.kuaisou.ProductSearcher field |
| | 71 | cn.com.pconline.enterprise.service.BuildIndexService.productSearcher to $Proxy22 |
| | 72 | }}} |
| | 73 | 经过查证发现是cobertura-maven-plugin插件的影响,spring的@Autowire是按类型注入的,而cobertura 在Instrumentation步骤中会对类型有修改,导致spring的注入有问题,对于商城这样复杂的注入情况来说暂时没有好的办法,只能先去掉代 码覆盖率的检测步骤。 |
| | 74 | http://blog.anorakgirl.co.uk/2010/01/maven-junit-cobertura-and-beancreationexception/ |
| | 75 | |
| | 76 | 具体处理: |
| | 77 | 1. pom.xml里不要指定build阶段的cobertura-maven-plugin插件。 |
| | 78 | 2. sonar的具体JPA项目settings中,sonar.core.codeCoveragePlugin设为false,缺省用是cobertura |
| | 79 | |
| | 80 | == LCOM4 == |
| | 81 | 1. Lack of Cohesion Among Method of Class |
| | 82 | 2. 帮助分析类的职责,数值越大表示该类可拆分的部分越多。 |
| | 83 | |
| | 84 | == Design == |
| | 85 | 1. 包的依赖关系图 |
| | 86 | 2. 目标是单向依赖 |
| | 87 | 3. 避免依赖循环 |
| | 88 | |
| | 89 | == Violations == |
| | 90 | 1. 代码质量分析 |
| | 91 | 2. findbug、pmd等可配置插件 |
| | 92 | 3. 自定义rule |
| | 93 | 4. 严重等级 |
| | 94 | 5. 发起review |
| | 95 | |
| | 96 | == Unit Test == |
| | 97 | 1. 自动运行unit test |
| | 98 | 2. 自动分析测试覆盖率 |
| | 99 | 3. 可以手动指定测试报告 |
| | 100 | |
| | 101 | == SCM Plugin == |
| | 102 | 1. 需要scm插件 |
| | 103 | 2. 配置scm |
| | 104 | 3. 按时间统计提交数 |
| | 105 | 4. 浏览代码时显示提交人和时间 |