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

二、配置篇
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,安裝中文語言包
8.安裝成功後,重新開機sonarqube服務,再次通路http://localhost:9000/,即可看到中文界面
三、idea使用篇
1. 打開File->Settings->Plugins,搜尋sonar插件
2.點選圖中第二個框起來的選項,在搜尋框中輸入sonar,出現一下界面
3.選擇SonarLint,點選Install安裝
4.安裝完畢,點選Restart InteliJ IDEA
5.在maven中配置sonar:打開setting.xml配置檔案,在其中加入如下代碼:
sonar
true
jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
com.mysql.jdbc.Driver
sonar
sonar
http://localhost:9000
6.然後項目中就如出現如圖所示,就能分析檔案中的代碼品質了