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」,找到數字對應的代碼,並進行分析改進。