1. 程式人生 > >程式碼質量檢查-SonarQube

程式碼質量檢查-SonarQube

SonarQube是管理程式碼質量一個開放平臺,可以快速的定位程式碼中潛在的或者明顯的錯誤

sonarQube : 提供圖形介面管理,報表展示,可靈活配置程式碼檢測的外掛 (findbugs;checkStyle;pmd等)

 

一、安裝:

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檔案

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,安裝中文語言包

 

7. 搜尋需要安裝的程式碼檢查外掛:findbugs;checkStyle;pmd 安裝。

  下載比較慢時,可到各外掛的官網下載,然後放到extensions\plugins目錄下

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

三、使用篇

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

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

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

9.我們點選專案,選擇問題連結,會看到分析程式碼的bug,以及作者

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

 

資料來源: http://www.cnblogs.com/qiaoyeye/p/5249786.html

http://www.sonarqube.org/

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