十分鐘搭建和使用sonarqube代碼質量管理平臺
Sonarqube為靜態代碼檢查工具,采用B/S架構,幫助檢查代碼缺陷,改善代碼質量,提高開發速度,通過插件形式,可以支持Java、C、C++、JavaScripe等等二十幾種編程語言的代碼質量管理與檢測。本文介紹如何快速安裝、配置、使用Sonarqube及Sonarqube Scanner。
工作原理
Sonaqube-scanner:負責搜集代碼相關數據 (可以理解為搜集端)
Sonarqube:負責對搜集的數據進行分析,通過不同的插件算法來對這些結果進行再加工,最終以量化的方式來衡量代碼質量,最終展現給用戶。
環境信息
IP | 功能 | 軟件 | 安裝路徑 | 操作系統 |
192.168.9.11 | sonarqube服務端 | jdk1.8.0_151,mysql5.7.21 sonarqube6.7 | /usr/local/ | centos7.5 |
192.168.9.12 | sonarqube-scanner客戶端 | sonarqube-scanner3.2.0 | d:/sonar | windows7 |
搭建步驟:(sever端安裝jdk mysql過程略)
sonarqube的安裝
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.7.ziunzip sonarqube-6.7.zip -d /usr/local/useradd sonar chown -R sonar. /usr/local/sonarqube-6.7/
sonarqube配置文件修改
vim /usr/local/sonarqube-6.7/conf/sonar.properties sonar.web.host=0.0.0.0 sonar.web.port=9000 sonar.jdbc.url=jdbc:mysql:sonar.jdbc.driver==sonar=password
建立數據庫 (在數據庫中建立需使用的庫)
mysql -u root -p > CREATE USER %password> GRANT all privileges ON sonarqube.* TO sonar’@‘%password>> create database sonarqube;
啟動及關閉
cd /usr/local/sonarqube-/bin/linux-x86-64 .//sonar.sh stop
通過瀏覽器登錄查看,並使用缺省密碼登錄。(可以在“配置”項的“應用市場”中選擇安裝中文插件來支持中文,)
sonarqube-scanner的安裝
1.下載sonar-scanner-3.2.0.1227-windows.zip並解壓,將bin文件加入環境變量path中如我的路徑D:\sonar\sonar-scanner\bin將此路徑加入path中
2.編輯配置文件conf/sonar-scanner.properties。根據數據庫使用情況進行取消相關的註釋即可,同時需要添加數據庫用戶名和密碼信息,即配置要訪問的sonar服務和mysql服務器地址
#Configure here general information about the environment, such as SonarQube server connection details for example #No information about specific project should appear here #----- Default SonarQube server sonar.host.url=http://192.168.9.11:9000#----- Default source code encoding #sonar.sourceEncoding=UTF-8sonar.jdbc.username=sonar sonar.jdbc.password=passwordsonar.jdbc.url=jdbc:mysql://192.168.9.150:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
3.編輯sonar-project.properties 放入需要掃描的project中(註意:同時要在sonarqube裏添加對應的項目名稱)
# must be unique in a given SonarQube instance sonar.projectKey=saas_core # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=saas_core sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.# This property is optional if sonar.modules is set. sonar.sources=saas_core sonar.binaries=bin sonar.language=java sonar.java.binaries=D:\sonar\saas_core # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8
4.sonar-scanner執行掃描分析
sonar-scanner -X
以上分析執行後,會將分析數據上傳至sonarqube,在sonarqube中分析完成後,就可以看到分析結果
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
十分鐘搭建和使用sonarqube代碼質量管理平臺