1 环境准备
- jdk:http://www.oracle.com/technetwork/java/javase/downloads/index.html
- mysql:http://www.mysql.com/
- apache-maven:http://maven.apache.org/
- sonarqube:http://www.sonarqube.org/downloads/
- sonar-scanner:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/
2 安装启动
2.1 启动qube
下载好sonarqube后,解压打开bin目录,启动相应OS目录下的StartSonar。
说明: 如本文演示使用的是win的64位系统,则打开C:\Users\Administrator\Desktop\sonarqube-6.3.1\bin\windows-x86-64\StartSonar.bat
2.2 访问qube
启动浏览器,访问http://localhost:9000
3 参数配置
3.1 新建数据库
打开mysql,新建一个数据库。如sonarqube
3.2 修改配置文件
打开sonarqube安装目录下的C:\Users\Administrator\Desktop\sonarqube-6.3.1\conf\sonar.properties文件,写入以下信息
sonar.jdbc.url=jdbc:mysql://172.17.11.225/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=sonarqube
sonar.jdbc.password=sonarqube
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
说明: url是数据库连接地址,username是数据库用户名,jdbc.password是数据库密码,login是sonarqube的登录名,sonar.password是sonarqube的密码
3.3 重启服务
重启sonarqube服务,再次访问http://localhost:9000,会稍微有点慢,因为要初始化数据库信息,数据库初始化成功后,重新登录
3.4 安装中文语言包
按照下图的点击顺序,进入插件安装页面,搜索chinese Pack,安装中文语言包
重启sonarqube服务 安装成功后,重启sonarqube服务,再次访问http://localhost:9000/,即可看到中文界面
4 使用方式(Maven)
4.1 配置
打开Maven目录中的settings.xml文件。如,D:\apache-maven-3.3.9\conf\settings.xml并修改代码如下:
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>http://172.17.11.218:9000</sonar.host.url>
</properties>
</profile>
</profiles>
注:需要排除的路径可以在pom.xml的<properties>里面加sonar.exclusions, 如: <sonar.exclusions>com/demo/test/constant/,com/demo/test/entity/</sonar.exclusions>
4.2 启动
转到项目的pom.xml所在目录,打开命令窗口执行
mvn clean verify sonar:sonar
分析成功后会出现下图
5 使用方式(sonar-scanner)
5.1 修改基础配置文件
下载sonar-scanner,打开配置文件sonar-scanner.properties,此文件一般在安装目录下conf文件夹中,添加配置信息如下
#字符集
sonar.sourceEncoding=UTF-8
#数据库连接
sonar.jdbc.url=jdbc:mysql://172.17.11.225/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=sonarqube
sonar.jdbc.password=sonarqube
#SonarQube地址
sonar.host.url=http://172.17.11.218:9000
5.3 添加项目配置文件
在项目根目录新建sonar-project.properties文件,并录入以下内容
# must be unique in a given SonarQube instance
sonar.projectKey=com.demo:project
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=project
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=.
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
5.4 启动
在项目根目录运行sonar-scanner,他会自动找当前目录下的sonar-project文件并加载
C:\Users\Administrator\Desktop\sonar-scanner-3.0.3.778-windows\bin\sonar-scanner.bat
分析成功后会出现下图
6 运行结果
打开http://localhost:9000/,我们会看到主页出现了分析项目的概要图
7 参考资料
http://www.lan-luo.pw/archives/62
http://www.cnblogs.com/winner-0715/p/5599350.html
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven