onarQube是管理代碼質量一個開放平臺,能夠快速的定位代碼中潛在的或者明顯的錯誤,下面將會介紹一下這個工具的安裝、配置以及使用。mysql
準備工做;sql
一、jdk(再也不介紹)數據庫
二、sonarqube:http://www.sonarqube.org/downloads/windows
三、SonarQube+Scanner:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.5.zipapi
四、mysql數據庫(再也不介紹)瀏覽器
1.下載好sonarqube後,解壓打開bin目錄,啓動相應OS目錄下的StartSonar。如本文演示使用的是win的64位系統,則打開D:\sonar\sonarqube-5.3\sonarqube-5.3\bin\windows-x86-64\StartSonar.bat服務器
2.啓動瀏覽器,訪問http://localhost:9000,如出現下圖則表示安裝成功。工具
1.打開mysql,新建一個數據庫。測試
2.打開sonarqube安裝目錄下的D:\sonar\sonarqube-5.3\sonarqube-5.3\conf\sonar.properties文件this
3.在mysql5.X節點下輸入如下信息
sonar.jdbc.url=jdbc:mysql://172.16.30.228:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.username=gmsd sonar.jdbc.password=gmsdtrade sonar.sorceEncoding=UTF-8 sonar.login=admin sonar.password=admin
url是數據庫鏈接地址,username是數據庫用戶名,jdbc.password是數據庫密碼,login是sonarqube的登陸名,sonar.password是sonarqube的密碼
4.重啓sonarqube服務,再次訪問http://localhost:9000,會稍微有點慢,由於要初始化數據庫信息
5.數據庫初始化成功後,登陸
6.按照下圖的點擊順序,進入插件安裝頁面
7.搜索chinese Pack,安裝中文語言包
8.安裝成功後,重啓sonarqube服務,再次訪問http://localhost:9000/,便可看到中文界面
1.打開D:\Program Files (x86)\sonar-scanner-3.0.3.778-windows\conf\sonar-scanner.properties文件
2.mysql節點下輸入如下信息
sonar.jdbc.url=jdbc:mysql://172.16.24.12:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.username=root sonar.jdbc.password=root
注意:若是測試項目與服務器不在同一臺機子,則須要添加服務器的IP:
#----- Default SonarQube server sonar.host.url=http://XXX.XXX.XXX.XXX:9000
3.配置環境變量
a.新建變量,name=SONAR_RUNNER_HOME。value=D:\sonar\sonar-scanner-2.5
b.打開path,輸入%SONAR_RUNNER_HOME%\bin;
c.sonar-runner -version,出現如下信息,則表示環境變量設置成功
4.打開要進行代碼分析的項目根目錄,新建sonar-project.properties文件
5.輸入如下信息
# must be unique in a given SonarQube instance sonar.projectKey=my:project # this is the name displayed in the SonarQube UI sonar.projectName=apiautocore 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 # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8
其中:projectName是項目名字,sources是源文件所在的目錄
6.設置成功後,啓動sonarqube服務,並啓動cmd
7.在cmd進入項目所在的根目錄,輸入命令:sonar-scanner,分析成功後會出現下圖
8.打開http://localhost:9000/,咱們會看到主頁出現了分析項目的概要圖
9.咱們點擊項目,選擇問題連接,會看到分析代碼的bug,哇,好多
10.選擇一個最嚴重的bug,看看
原來是這個地方會出現空指針異常,緣由是我沒有進行初始化就使用這個變量了。Over