SonarQube
若要轉載本文,請務必聲明出處:https://www.cnblogs.com/zhongyuanzhao000/p/11686522.html
概念:
SonarQube是一種自動代碼審查工具,用於檢測代碼中的錯誤,漏洞和代碼異味。它可以與您現有的工作流程集成,以便在項目分支和拉取請求之間進行連續的代碼檢查。
優點:對大量的持續集成工具提供了接口支持,可以很方便地在持續集成中使用 Sonar;集成不同的測試工具,代碼分析工具,以及CI工具,比如pmd-cpd、checkstyle、findbugs、Jenkins;
在CentOS系統中安裝SonarQube:
環境需求:Jdk 1.8,MySQL
另外,本次實踐的CentOS7服務器的IP為:10.141.211.174
-
數據庫配置:
進入mysql,創建數據庫sonar,密碼為sonar $ mysql -u root -p mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar'; mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar'; mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar'; mysql> FLUSH PRIVILEGES;
-
使用wget命令下載安裝包,鏈接如下:
sonarqube: https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.2.zip sonar-scanner-cli: https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip 可以自己更改需要的版本號(進入地址的Distribution/中進行查看)
具體命令如下:
# 下載壓縮包並解壓 cd /usr/local wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.2.zip unzip sonarqube-7.2.zip # 添加用戶sonar,並更改 該目錄 的owner(原因:sonarqube中的es不許以root啟動,故以sonar用戶來啟動) useradd sonar chown -R sonar. /usr/local/sonarqube-7.2
-
編輯sonarqube配置文件
vi /usr/local/sonarqube-7.2/conf/sonar.properties 相應的修改處如下: 1. # User credentials. # Permissions to create tables, indices and triggers must be granted to JDBC user. # The schema must be created first. sonar.jdbc.username=sonar sonar.jdbc.password=sonar 2. #----- DEPRECATED #----- MySQL >=5.6 && <8.0 # Support of MySQL is dropped in Data Center Editions and deprecated in all other editions # Only InnoDB storage engine is supported (not myISAM). # Only the bundled driver is supported. It can not be changed. sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false 3. # Binding IP address. For servers with more than one IP address, this property specifies which # address will be used for listening on the specified ports. # By default, ports will be used on all IP addresses associated with the server. sonar.web.host=10.141.211.174 (填寫本機IP)
然后保存並退出。
-
運行腳本啟動服務
cd /usr/local/sonarqube-7.2/ # 以普通用戶sonar啟動服務,不然es啟動會報錯,用法:console、start、status、stop... su sonar bin/linux-x86-64/sonar.sh start # 查看狀態,但這個狀態只是暫時的,並不可信 su sonar bin/linux-x86-64/sonar.sh status # 跟蹤日志,確保啟動成功(先跟着sonar.log日志,如果報es錯誤,可以去查看es.log;如果報了web錯誤,那么就是查看web.log) tail -f logs/sonar.log
-
登錄web端:
在瀏覽器輸入:http://IP:9000 ,即可成功進入(初始用戶:admin ,初始密碼:admin,設置好token即可)。
安裝Sonar-Scanner:
-
下載壓縮包並解壓(最好用sonar-scanner-2.8版本,支持jdk1.8,否則其他版本會出錯)
cd /usr/local wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip unzip sonar-scanner-2.8.zip # 解壓后,目錄名改為sonar-scanner
-
編輯 /etc/profile 文件
把以下配置添加到文件末尾,如下: #set sonar-scanner environment export SONAR_SCANNER_HOME=/usr/local/sonar-scanner export PATH=${SONAR_SCANNER_HOME}/bin:${PATH} 然后執行命令: source /etc/profile
-
查看sonar-scanner版本:
sonar-scanner -v
然后根據提示,編輯sonar-scanner.properties文件,如下:vi /usr/local/sonar-scanner/conf/sonar-scanner.properties 修改SonarQube server的地址,改為前面SonarQube的地址(我的是http://10.141.211.174:9000) #----- Default SonarQube server sonar.host.url=http://10.141.211.174:9000 去掉mysql的注釋 #----- MySQL sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
嘗試使用SonarQube:
-
保證SonarQube已經啟動,若沒有啟動可以運行:
su sonar /usr/local/sonarqube-7.2/bin/linux-x86-64/sonar.sh start
-
克隆一個項目到本地:
git clone xxx
-
在該項目的根目錄中,創建文件 sonar-project.properties,並對其進行編輯,編輯內容如下:
# must be unique in a given SonarQube instance sonar.projectKey=simple-java-maven-app sonar.projectName=simple-java-maven-app sonar.projectVersion=1.0 sonar.sources=src sonar.language=java sonar.sourceEncoding=UTF-8 sonar.java.binaries=/usr/local/workspace/simple-java-maven-app/target/classes
-
使用sonar-scanner進行分析,在項目根目錄中執行命令:
sonar-scanner
,然后就可以到http://10.141.211.174:9000 查看分析結果了。
除了sonar-scanner
命令,我們也可以使用maven的命令來實現同樣的代碼分析效果,命令如下:mvn sonar:sonar \ -Dsonar.host.url=http://10.141.211.174:9000 \ -Dsonar.login=acfa9b0585a3c0a10366826143edebf4abd36f6b # 這個是sonarqube登錄時的token
參考:
https://www.cnblogs.com/ding2016/p/8065241.html
https://www.cnblogs.com/owenma/p/7891170.html
https://blog.csdn.net/qq_21816375/article/details/80787993