实践代码质量管理之SonarQube

SonarQube, The leading product for Continuous Code Quality.

安装

下载(sonarqube-7.4.zip)解压即可。

新建数据库schema和user。

修改配置文件sonar.properties,设置数据库连接相关信息。

1
$ sonar.sh { console | start | stop | restart | status | dump }

plugin

插件可以通过Web页面上的marketplace安装,或者人工安装插件jar到extensions/plugins目录。

Community plugins for SonarQube

[Plugin Library](https://docs.sonarqube.org/display/PLUG/Plugin+Library)

[Plugin Version Matrix](https://docs.sonarqube.org/display/PLUG/Plugin+Version+Matrix)

SonarJava

findbugs

cobertura

checkstyle

pmd

chinese pack

Android Lint

JaCoCo

SonarQube默认支持JaCoCo,从jacoco.execjacoco-it.execsonar.jacoco.reportPaths】生成测试覆盖相关数据。

1
2
$ mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install
$ mvn sonar:sonar

参考Usage of JaCoCo with SonarJava

spotbugs

sonarcube从spotbugs生成的reports导入

1
<sonar.java.spotbugs.reportPaths>./target/spotbugsXml.xml</sonar.java.spotbugs.reportPaths>
1
$ mvn clean package com.github.spotbugs:spotbugs-maven-plugin:spotbugs sonar:sonar

PMD

sonarcube从pmd生成的reports导入

1
<sonar.java.pmd.reportPaths>./target/pmd.xml</sonar.java.pmd.reportPaths>
1
$ mvn clean package pmd:pmd sonar:sonar

checkstyle

sonarcube从checkstyle生成的reports导入

1
<sonar.java.checkstyle.reportPaths>./target/checkstyle-result.xml</sonar.java.checkstyle.reportPaths>
1
$ mvn clean package checkstyle:checkstyle sonar:sonar