1. 程式人生 > 程式設計 >SonarQube安裝、配置與使用教程圖解

SonarQube安裝、配置與使用教程圖解

SonarQube是管理程式碼質量一個開放平臺,可以快速的定位程式碼中潛在的或者明顯的錯誤,下面將會介紹一下這個工具的安裝、配置以及使用。

準備工作;

1、jdk(不再介紹)

2、sonarqube:http://www.sonarqube.org/downloads/

3、SonarQube+Scanner:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.5.zip

4、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,如出現下圖則表示安裝成功。

SonarQube安裝、配置與使用教程圖解

二、配置篇

1.開啟mysql,新建一個數據庫。

2.開啟sonarqube安裝目錄下的D:\sonar\sonarqube-5.3\sonarqube-5.3\conf\sonar.properties檔案

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.按照下圖的點選順序,進入外掛安裝頁面

SonarQube安裝、配置與使用教程圖解

7.搜尋chinese Pack,安裝中文語言包

SonarQube安裝、配置與使用教程圖解

8.安裝成功後,重啟sonarqube服務,再次訪問http://localhost:9000/,即可看到中文介面

SonarQube安裝、配置與使用教程圖解

三、使用篇

1.開啟D:\sonar\sonar-scanner-2.5\conf\sonar-runner.properties檔案

2.mysql節點下輸入以下資訊

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

注意:如果測試專案與伺服器不在同一臺機子,則需要新增伺服器的IP:

#----- Default SonarQube server

sonar.host.url=http://XXX.XXX.XXX.XXX:9000[/code]

3.配置環境變數

a.新建變數,name=SONAR_RUNNER_HOME。value=D:\sonar\sonar-scanner-2.5

b.開啟path,輸入%SONAR_RUNNER_HOME%\bin;

c.sonar-runner -version,出現以下資訊,則表示環境變數設定成功

SonarQube安裝、配置與使用教程圖解

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-runner,分析成功後會出現下圖

SonarQube安裝、配置與使用教程圖解

8.開啟http://localhost:9000/,我們會看到主頁出現了分析專案的概要圖

SonarQube安裝、配置與使用教程圖解

9.我們點選專案,選擇問題連結,會看到分析程式碼的bug,哇,好多

SonarQube安裝、配置與使用教程圖解

10.選擇一個最嚴重的bug,看看

SonarQube安裝、配置與使用教程圖解

原來是這個地方會出現空指標異常,原因是我沒有進行初始化就使用這個變量了。Over

預知更多使用,請登入官網自行學習。

http://www.sonarqube.org/

http://docs.sonarqube.org/display/SONAR/Analyzing+with+SonarQube+Scanner

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。