1 jenkins和 SnoarQube 和 sonar-scanner 之間有依賴關系,版本需要符合條件最后才可以生成報表
持續集成環境:Jenkins
- 所需系統環境: java 8 及以上
-
- 代碼托管:Git 2.22 或者 gitlab
- 審查工具:SonarQube
- 該工具由兩部分組成,分別是 SonarQube本身(即是審查服務器),和 sonar-scanner(審查服務端)
SonarQube和sonar-scanner的關系就像是 github 官網和我們本地的 git 軟件的關系
- snoar-scanner = sonar-runner 是同一個軟件在不同版本下的名字,網上的教程兩者基本上是通用
- 該工具由兩部分組成,分別是 SonarQube本身(即是審查服務器),和 sonar-scanner(審查服務端)
1 安裝jenkins
下載鏈接地址
鏈接:https://pan.baidu.com/s/1KFmC2Fj4WUOtf1h4qJd8Kg
提取碼:0br8
SonarQube 版本號 7.6 不能是7.9之上,不然,需要java 版本11
sonar-scanner 提取碼:cy1n
mysql 5.7.19 提取碼:pwwv 這個版本最好是5.6. 5.7 都行,對版本也有限制
2 安裝mysql
3.首先需要安裝 MySql 數據庫(為了方便可配置mysql相關環境變量,同時第一次進入mysql需要修改密碼,此處都略過,百度一下)
root 登錄 mysql,創建 sonar 數據庫和用戶授權。這個
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'; #賦權限,所有pc 都可以訪問;還需要設置用戶密碼 GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar'; FLUSH PRIVILEGES; #刷新緩存立即生效
4 安裝 SonarQube
sonarqube已解壓
在路徑下的 conf 文件夾下修改 sonar.properties
url是數據庫連接地址,username是數據庫用戶名,jdbc.password是數據庫密碼,login是sonarqube的登錄名,sonar.password是sonarqube的密碼
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=root
sonar.jdbc.password=walker123
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
之后打開 bin 目錄下對應的 StartSnoar.bat 就可以啟動 Snoar 服務器了。可通過瀏覽器 http://127.0.0.1:9000 查看,就可以訪問了
5
配置 sonar-scanner(SonarQube和sonar-scanner的關系就像是 github 官網和我們本地的 git 軟件的關系);必須版本合適
1.配置 sonar-scanner 目錄下 conf 中的 snoar-scanner.properties sonar-scanner
sonar.jdbc.url=jdbc:mysql://mysql:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.username=root sonar.jdbc.password=root
配置環境變量
a.新建變量,name=SONAR_RUNNER_HOME。value=D:\sonar\sonar-scanner-2.5
b.打開path,輸入%SONAR_RUNNER_HOME%\bin;
cmd 執行如下命令 sonar-runner -v 未報錯即ok
6 去我們 git 項目的文件夾里,新建sonar-project.properties文件,內容
其中:projectName是項目名字,sources是源文件所在的目錄,sonar.language 代碼語言,python 是py
看有的沒有配置
# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name displayed in the SonarQube UI
sonar.projectName=apiautocore
sonar.projectVersion=1.0
sonar.language=go
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=src
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
7.設置成功后,啟動sonarqube服務,並啟動cmd
8 .在cmd進入項目所在的根目錄,輸入命令:sonar-runner,分析成功后會出現下圖
訪問地址 localhost:9000
sonarqube生成token 值
9 .在配置頁對SonarQube servers進行配置:
系統配置–系統設置–SonarQube servers
soarqube 中文插件地址 ;將中文的jia包放在 \sonarqube-7.6\extensions\plugins 目錄重啟服務即可
https://github.com/SonarQubeCommunity/sonar-l10n-zh/tags

FQA 1 snoarqube 啟動失敗,1 任務管理器中結束 java的進程,在temp目錄下刪除所有文件,cmd 執行 StartSonar.bat 命令即可以正常重啟 sonarqube的服務
轉載 https://www.cnblogs.com/can-i-do/p/11173669.html