1. SonarQube安裝(sonarqube5.1.2 + sonar-runner-dist-2.4)
1.1 前提條件
1) 已安裝Java環境(version:1.7+)
2) 已安裝MySQL數據庫(version:5.x)
下載SonarQube: http://www.sonarqube.org/downloads/
下載Sonar-Runner: http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
中文補丁包下載:https://github.com/SonarCommunity/sonar-l10n-zh
3) MySQL數據庫配置
執行數據庫腳本,創建數據庫及用戶:
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;
1.2 安裝Sonar Server
1) 將下載的sonarqube-5.1.2.zip包解壓至D:\sonar\sonarqube-5.1.2;
2) 修改配置文件D:\sonar\sonarqube-5.1.2\conf\sonar.properties,進行配置數據庫設置
1.3 啟動Sonar Server服務
Sonar目前支持Linux/Macosx/Solaris/Windows等操作系統。以Windows 32位操作系統為例,目錄切換至D:\sonar\sonarqube-5.1.2\bin\windows-x86-32目錄(32對應jdk7_32bit 而非windows_32bit),運行StartSonar.bat文件啟動服務。
1.4 訪問Sonar Server
訪問地址:http://172.20.28.35:9000/ 或者 http://localhost:9000/
其中,172.20.28.35為服務器的IP
1.5 Installing SonarQube in Eclipse(安裝插件[非必須項],可以先忽略1.1-1.4步驟)
Plugin |
3.4 |
3.5 |
SonarQube |
3.6+ |
4.2+ |
3.7.x, 3.8.x, 4.2.x, 4.3.x, 4.4.x, 4.5.x |
4.2.x, 4.3.x, 4.4.x, 4.5.x |
|
Supported Plugins / Languages |
See Features details |
Installation: If a previous version of Eclipse SonarQube is already installed, you can update it. Go to Help > Check for Updates.
To install this plugin in your Eclipse IDE:
Go to Help > Eclipse Marketplace... and search for "SonarQube"
Detail link: http://docs.sonarqube.org/display/SONAR/Installing+SonarQube+in+Eclipse
2. Sonar Runner
2.1 安裝Sonar Runner
解壓sonar-runner-dist-2.4.zip到任意目錄,為了方便,將安裝包解壓到D:\sonar-runner-2.4下。
2.2 配置環境變量
1) 打開“系統屬性”對話框,點擊“環境變量”,進入環境變量對話框。
2) 在“系統變量(S)”下點擊“新建(W)...”,在編輯系統變量對話框中添加SONAR_RUNNER_HOME變量。
3) 在“系統變量(S)”下找到“Path”,點擊“編輯(I)...”,在編輯系統變量對話框中“變量值(V):”輸入框內容未尾添加、“;%SONAR_RUNNER_HOME%\bin”,點確定。
4) 按下圖所示命令進行驗證環境變量是否配置成功,如果展示結果如下圖所示,則表示配置成功。
2.3 配置Sonar Runner
編輯D:\sonar-runner-2.4\conf\sonar-runner.properties,配置指定的Sonar Server地址、數據庫URL、數據庫用戶名及密碼、Sonar Server用戶名及密碼
2.4 配置sonar-project.properties
在項目源碼的根目錄下創建sonar-project.properties配置文件
Multi-module Project
There are two ways to define a multi-module structure in SonarQube:
|
Using the given file structure... |
... with the given 'properties' files |
||||||
Way #1 Set all the configuration in the properties file in the root folder |
"MyProject/sonar-project.properties" file content
|
|||||||
Way #2 Set the configuration in multiple properties files |
"MyProject/sonar-project.properties" file content
"MyProject/module1/sonar-project.properties" file content
"MyProject/module2/sonar-project.properties" file content
|
Detail link: http://docs.sonarqube.org/display/SONAR/Analyzing+with+SonarQube+Runner
2.5 代碼掃描
切換到項目源碼根目錄,執行以下命令:sonar-runner
掃描結果中出現“EXCUTION SUCCESS”字樣則表示代碼掃描成功。
2.6 查看代碼掃描結果
2.7 補充說明
在實際應用過程中,可能會遇到報JVM空間不夠或內存溢出的情況,為了解決此問題,可以按下文所示方法通過修改批處理腳本D:\sonar-runner-2.4\bin\sonar-runner.bat文件即可。
根據實際情況並參照下圖所示修改sonar-runner.bat文件中第77行。
3. 案例分析
默認只有java plugin。添加javascript,jsp(web)檢測,需下載相關插件
link:http://docs.sonarqube.org/display/PLUG/Web+Plugin
MyProject:
sonar-project.properties內容如下:
Main Dashboard: 主要的信息展示
Duplications: 重復率比重
Complexity:復雜度
Configure widgets:布局控件
SQALE Rating:Software Quality Assessment based on Lifecycle Expectations Rating 基於生命周期期望的軟件質量模型評價
Debt:技術債務(修復所有issues所用的時間代價)
Issues: Blocker/Critical/Major/Minor/Info 問題的級別,依次從高級到低級。
如下圖:點擊issues 選擇Critical級別,右側會有一個列表,點擊文件進入。
在紅色箭頭處點擊展開,會有相關信息提示。
4. 參考文檔
1.如何搭建Sonar代碼質量管理平台
2.如何使用Sonar Runner V2.3進行代碼掃描
4.http://deejay.iteye.com/blog/1901950