SonarQube是管理代码质量一个开放平台,能够快速的定位代码中潜在的或者明显的错误,下面将会介绍一下这个工具的安装、配置以及使用。html
准备工做;java
一、jdk(1.8及以上)mysql
二、SonarQube:http://www.sonarqube.org/downloads/git
三、SonarQube+Scanner:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.2.0.1227-windows.zipgithub
四、mysql数据库(5.5至8.0版本)sql
1.下载好SonarQube后,解压打开bin目录,启动相应操做系统目录下的StartSonar。如本文演示使用的是win的64位系统,则打开数据库
sonarqube-7.6\bin\windows-x86-64\StartSonar.batwindows
2.启动浏览器,访问http://localhost:9000,如出现下图则表示安装成功。浏览器
3.默认的用户名密码:admin/admin服务器
注意:若是要关闭sonarqube服务,则在服务窗口界面,快捷键Ctrl+C,而后输入Y退出,如图所示:
不要直接关闭,否则再次启动时会出现以下报错:
当你看到以下所示内容的时候,极可能是因为以前打开的sonarqube未正常关闭,这时须要打开任务管理器,将和Java有关的进程所有关掉,从新启动sonarqube便可成功。
4.SQ汉化:https://github.com/SonarQubeCommunity/sonar-l10n-zh
点击官方文档左侧的Plugins 进入右侧右下角的Localization 点击Chinese 进入了github地址(
https://github.com/SonarQubeCommunity/sonar-l10n-zh) 对应版本是
SonarQube 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7
sonar-l10n-zh 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19
SonarQube 5.4 5.5 5.6
sonar-l10n-zh 1.9 1.10 1.11
SonarQube 4.0 4.1
sonar-l10n-zh 1.7 1.8
SonarQube 3.1 3.2 3.3 3.4 3.5 3.6 3.7
sonar-l10n-zh 1.0 1.1 1.2 1.3 1.4 1.5 1.6
查看对应的版本 5.4须要1.9的sonar-l10n-zh 点击releases下载对应版本
将下载好的jar 置入 extensions\plugins目录下
重启服务 访问http://localhost:9090发现变成中文版
另一种方式 也能够登陆系统 点击配置-系统-更新中心管理插件 能够在线搜索安装chinese插件
1.打开mysql,新建一个数据库,名为sonarDB。
2.打开SonarQube安装目录下的
sonarqube-7.6\conf\sonar.properties文件
3.在MySQL节点下输入如下信息
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarDB?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false sonar.jdbc.username=root sonar.jdbc.password=root sonar.sorceEncoding=UTF-8
4.重启sonarqube服务,再次访问http://localhost:9000,会稍微有点慢,由于要初始化数据库信息
5.数据库初始化成功后,登陆(默认用户名和密码分别是admin/admin)
1.访问官网:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
下载sonar-scanner插件:
2.打开sonar-scanner-3.3.0.1492-windows\conf\sonar-scanner.properties文件
3.mysql节点下输入如下信息
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarDB?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.username=root sonar.jdbc.password=root
注意:若是测试项目与服务器不在同一台机子,则须要添加SonarQube服务器的IP:
sonar.host.url=http://localhost:9000
4.配置环境变量
a.新建变量,name=SONAR_RUNNER_HOME。value=E:\Program Files\sonar-scanner-3.2.0.1227
b.打开path,输入%SONAR_RUNNER_HOME%\bin;
c.sonar-scanner --version,出现如下信息,则表示环境变量设置成功
5.打开要进行代码分析的项目根目录,新建sonar-project.properties文件
6.输入如下信息
# must be unique in a given SonarQube instance sonar.projectKey=my:project # this is the name displayed in the SonarQube UI sonar.projectName=hnsi-calc-ybjs-service 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=src sonar.java.binaries=target sonar.language=java # Encoding of the source code. Default is default system encoding sonar.sourceEncoding=UTF-8
其中:projectName是项目名字,sources是源文件所在的目录,binaries是class文件所在的目录
7.设置成功后,启动sonarqube服务,并启动cmd
8.在cmd进入项目所在的根目录,输入命令:sonar-scanner,分析成功后会出现下图
9.打开http://localhost:9000/,咱们会看到主页出现了分析项目的概要图
10.咱们点击项目,选择“Bugs”,能够看到问题bug列表:
10.选择一个bug,点击,能看到具体的代码。看到数字“+2”,说明引发该问题的缘由是“1”,会发生问题的是“2”,找到数字对应的代码,并进行分析改进。