SonarScaner代码质量扫描


1.安装sonarscanner

#!/bin/bash
cd /opt && rm -rf /opt/sonar-scanner-4.0.0.1744-linux  && wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.0.0.1744-linux.zip && unzip -o sonar-scanner-cli-4.0.0.1744-linux.zip -d /opt/

ln -sf /opt/sonar-scanner-4.0.0.1744-linux/bin/sonar-scanner  /usr/bin/sonar-scanner

rm -rf /opt/sonar-scanner-cli-4.0.0.1744-linux.zip

2.项目目录下新增sonar-project.properties文件

sonar.host.url=http://172.16.7.38:19000
sonar.projectKey=sonarScannerTest
sonar.login=025da6f30c6e848e7ses91b7a68cebb8619
sonar.projectName=sonarScannerfrxs
sonar.version=1.0
sonar.sources=.
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.java.binaries=.

参数说明:

# 项目名称,不同项目需要更改
sonar.projectName=sefs-ac-accepter
# 项目源文件,不同项目需要更改
sonar.sources=.
# 编译完打包路径,不同项目需要更改
sonar.java.binaries=accepter-service-impl/target
# 项目版本
sonar.projectVersion=1.0.0
# 项目编程语言
sonar.language=java
# sonar项目Key值,需要找管理员新建sonarqube项目获取,不同项目需要更改
sonar.projectKey=accepter
# 项目根目录,默认为当前目录
java-module.sonar.projectBaseDir=.

3. 执行扫描

sonar-scanner

4. 查看扫描结果

扫描后结果在当前目录下 .scannerwork中report.txt文件中 内容如下:

projectKey=sonarScannerTest
serverUrl=http://172.16.7.38:19000
serverVersion=7.1.0.11001
# 查看结果链接 dashboarUrl
dashboardUrl=http://172.16.7.38:19000/dashboard/index/sonarScannerTest
ceTaskId=AW1CD_uSe-M9PDLeL4kB
# 任务执行状态接口 ceTaskUrl
ceTaskUrl=http://172.16.7.38:19000/api/ce/task?id=AW1CD_uSe-M9PDLeL4kB

结果json返回

{
"task": {
"id": "AW1CD_uSe-M9PDLeL4kB",
"type": "REPORT",
"componentId": "AWxxx",
"componentKey": "sonarScannerTest",
"componentName": "sonarScannerTest",
"componentQualifier": "TRK",
"analysisId": "AW1CD_4DByAj22a5BRp7",
# 检测结果status
"status": "SUCCESS",
"submittedAt": "2019-09-18T01:50:06+0000",
"submitterLogin": "admin",
"startedAt": "2019-09-18T01:50:07+0000",
"executedAt": "2019-09-18T01:50:10+0000",
"executionTimeMs": 2807,
"logs": false,
"hasScannerContext": true,
"organization": "default-organization"
}
}