SonarQube安裝文檔

大的項(xiàng)目管理是困難的,所以需要借助一些工具去規(guī)范代碼,掃描出代碼潛在的缺陷。這方便SonarQube非常優(yōu)秀,我們先介紹一下SonarQube檢查功能:
1,不遵守代碼標(biāo)準(zhǔn)(checkstyle)
2,潛在缺陷(空指針)
3,糟糕的復(fù)雜度(難以理解)
4,重復(fù)(通用的方法需要?dú)w納封裝)
5,糟糕的注釋
7,糟糕的設(shè)計(jì)(耦合度檢查)
需要用到的軟件有:SonarQube,Sonar-scanner。SonarQube為代碼檢查的server,并提供可視化界面;Sonar-scanner為client。用于收集檢查數(shù)據(jù)并且發(fā)送到server中。也就是傳統(tǒng)的C/S關(guān)系:

C/S關(guān)系

首先在服務(wù)器上下載:

  鏈接:wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.2.zip 
  由于最新鏈接需要java11+,所以使用較新版本。7.2版本需要jdk1.8,jdk安裝省略

創(chuàng)建sonar用戶:

  sudo useradd sonar -m -s /bin/bash
  sudo passwd sonar 

添加該用戶權(quán)限:

vi /etc/sudoers
root  ALL=(ALL:ALL) ALL  
sonar  ALL=(ALL:ALL) ALL 

啟動(dòng)sonar:

判斷l(xiāng)inux是32位還是64位:uname -a
cd /home/sonar/sonarqube-7.2/bin/linux-x86-64
普通啟動(dòng):./sonar.sh console
后臺(tái)啟動(dòng):./sonar.sh start   
通過(guò)指令:netstat -tnlp     檢查9000端口是否啟動(dòng)成功

登錄:

進(jìn)入ip+端口9000。使用admin/admin登錄,成功之后顯示警告:
Embedded database should be used for evaluation purpose only
警告圖

配置數(shù)據(jù)庫(kù):

[sonar@node1 /]$ cd /home/sonar/sonarqube-7.2/conf
[sonar@node1 conf]$ vi sonar.properties
在“#----- MySQL >=5.6 && <8.0 ”下面添加:
sonar.jdbc.url=jdbc:mysql://ip:port/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&maxAllowedPacket=256000000(maxAllowedPacket為后續(xù)描述的那個(gè)bug所加)
sonar.jdbc.username=XXX
sonar.jdbc.password=XXX
重啟之后警告消失,并且在sonarqube數(shù)據(jù)庫(kù)會(huì)創(chuàng)建一些基礎(chǔ)表

在線下載sonar-scan:

wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
版本2.8.zip

配置scan:

在/etc/profile里添加:
#sonar-scanner profile   
export  
 SONAR_SCANNER_HOME=/home/sonar/sonar-scanner 
export  
 PATH=${SONAR_SCANNER_HOME}/bin:${PATH}
source /etc/profile

執(zhí)行:sonar-scanner -v  
返回:
INFO: Scanner configuration file: /home/sonar/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /home/sonar/lightkits-center-service/sonar-project.properties
INFO: SonarQube Scanner 2.8
INFO: Java 1.8.0_65 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-327.18.2.el7.x86_64 amd64

在sonar-scanner的conf/sonar-scanner.properties關(guān)聯(lián)對(duì)應(yīng)的SonnarQube,為了實(shí)現(xiàn)上傳操作:

sonar.host.url=http://ip:9000
sonar.login=admin
sonar.password=admin

至此scanner配置完成

在工程項(xiàng)目的一級(jí)目錄添加文件sonar-project.properties,內(nèi)容如下:

# must be unique in a given SonarQube instance
sonar.projectKey=lightkits-center-service
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=lightkits-center-service
sonar.projectVersion=master

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=./
sonar.language=java

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
# Additional parameters  
sonar.my.property=value
sonar.java.binaries=**/classes

執(zhí)行:
sonar-scanner -X
成功之后即可在SonarQube頁(yè)面上瀏覽檢查不過(guò)關(guān)的代碼。
常見(jiàn)錯(cuò)誤:
1,Packet for query is too large (9716496 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable
解決辦法:通過(guò)設(shè)置mysql的my.cnf,添加參數(shù):max_allowed_packet=500M
重啟mysql,運(yùn)行指令:show VARIABLES like '%max_allowed_packet%
如果該值為500M,則修改成功。
最后在sonar的conf下連接jdbc的鏈接后加上maxAllowedPacket=500000000
問(wèn)題圓滿解決!??!

效果圖:


效果圖

擴(kuò)展:

集成Jenkins:
       在jenkins服務(wù)器上安裝客戶端Sonar-Scanner并配置好環(huán)境變量。
       當(dāng)jenkins自動(dòng)部署項(xiàng)目完成之后觸發(fā)邏輯:進(jìn)入項(xiàng)目文件夾 ——> 執(zhí)行指令:sonar-scanner

集成gitlab:
       通過(guò)git-runner執(zhí)行腳本gitlab-ci.yml。當(dāng)任意同事代碼提交時(shí)直接觸發(fā)gitlab-ci.yml。
       gitlab-ci.yml觸發(fā)sonarQube代碼檢查腳本比如:進(jìn)入項(xiàng)目文件夾 ——> 執(zhí)行指令:sonar-scanner
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容