十分鐘搭建和使用sonarqube代碼質量管理平臺

前言

Sonarqube爲靜態代碼檢查工具,採用B/S架構,幫助檢查代碼缺陷,改善代碼質量,提高開發速度,通過插件形式,可以支持Java、C、C++、JavaScripe等等二十幾種編程語言的代碼質量管理與檢測。本文介紹如何快速安裝、配置、使用Sonarqube及Sonarqube Scanner。

工作原理

Sonaqube-scanner:負責蒐集代碼相關數據 (可以理解爲蒐集端)

Sonarqube:負責對蒐集的數據進行分析,通過不同的插件算法來對這些結果進行再加工,最終以量化的方式來衡量代碼質量,最終展現給用戶。

sonarqub.jpg

環境信息

IP功能軟件安裝路徑操作系統
192.168.9.11sonarqube服務端jdk1.8.0_151,mysql5.7.21 sonarqube6.7/usr/local/centos7.5
192.168.9.12sonarqube-scanner客戶端sonarqube-scanner3.2.0d:/sonarwindows7

 

搭建步驟:(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

通過瀏覽器登錄查看,並使用缺省密碼登錄。(可以在“配置”項的“應用市場”中選擇安裝中文插件來支持中文,)

s111.jpg

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中分析完成後,就可以看到分析結果

s222.jpg

 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章