部署sonar-scanner
下载sonar-scanner
wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.8.0.2856-linux.zip
设置sonar-scanner
解压sonar-scanner
unzip sonar-scanner-cli-4.8.0.2856-linux.zip -d /data/apps/sonar-scanner/
设置软链接
ln -sv /data/apps/sonar-scanner/sonar-scanner-4.8.0.2856-linux/ /usr/local/sonar-scanner
‘/usr/local/sonar-scanner’ -> ‘/data/apps/sonar-scanner/sonar-scanner-4.8.0.2856-linux/’
sonar-scanner.properties
# egrep -v "^$|^#" /usr/local/sonar-scanner/conf/sonar-scanner.properties
sonar.host.url=http://192.168.0.81:9000
sonar.sourceEncoding=UTF-8
身份验证令牌
示例项目
下载示例项目
# wget https://github.com/SonarSource/sonar-scanning-examples/archive/master.zip
解压示例项目
# unzip master.zip -d /data/apps
查看示例项目
# ls -l /data/apps/sonar-scanning-examples-master/sonarqube-scanner
total 8
drwxr-xr-x 2 root root 79 Feb 23 04:09 copybooks
drwxr-xr-x 2 root root 40 Feb 23 04:09 coverage-report
-rw-r--r-- 1 root root 112 Feb 23 04:09 README.md
-rw-r--r-- 1 root root 647 Feb 23 04:09 sonar-project.properties
drwxr-xr-x 20 root root 241 Feb 23 04:09 src
sonar-project.properties
sonar.projectKey=org.sonarqube:sonarqube-scanner
sonar.projectName=Example of SonarQube Scanner Usage
sonar.projectVersion=1.0
sonar.sources=src,copybooks
sonar.sourceEncoding=UTF-8
## Cobol Specific Properties
# comma-separated paths to directories with copybooks
sonar.cobol.copy.directories=copybooks
# comma-separated list of suffixes
sonar.cobol.file.suffixes=cbl,cpy
sonar.cobol.copy.suffixes=cpy
## Flex Specific Properties
# retrieve code coverage data from the Cobertura report
sonar.flex.cobertura.reportPath=coverage-report/coverage-cobertua-flex.xml
# PL/I Specific Properties
sonar.pli.marginLeft=2
sonar.pli.marginRight=0
启动分析命令
# cd /data/apps/sonar-scanning-examples-master/sonarqube-scanner
# /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.login=squ_9e14be0cd05d214ceca9e02b41eedbe897411a55
INFO: Scanner configuration file: /data/apps/sonar-scanner/sonar-scanner-4.8.0.2856-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: /data/apps/sonar-scanning-examples-master/sonarqube-scanner/sonar-project.properties
INFO: SonarScanner 4.8.0.2856
INFO: Java 11.0.17 Eclipse Adoptium (64-bit)
INFO: Linux 3.10.0-1062.9.1.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Analyzing on SonarQube server 9.9.0.65466
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Load global settings
INFO: Load global settings (done) | time=120ms
INFO: Server id: E16C2B31-AYacQbObEuWJ7TK0duqu
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=62ms
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
INFO: Load/download plugins (done) | time=1650ms
INFO: Process project properties
INFO: Process project properties (done) | time=11ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=2ms
INFO: Project key: org.sonarqube:sonarqube-scanner
INFO: Base dir: /data/apps/sonar-scanning-examples-master/sonarqube-scanner
INFO: Working dir: /data/apps/sonar-scanning-examples-master/sonarqube-scanner/.scannerwork
INFO: Load project settings for component key: 'org.sonarqube:sonarqube-scanner'
WARN: SCM provider autodetection failed. Please use "sonar.scm.provider" to define SCM of your project, or disable the SCM Sensor in the project settings.
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=118ms
INFO: Load active rules
INFO: Load active rules (done) | time=1982ms
INFO: Load analysis cache
INFO: Load analysis cache (404) | time=8ms
INFO: Load project repositories
INFO: Load project repositories (done) | time=9ms
INFO: Indexing files...
INFO: Project configuration:
INFO: 35 files indexed
INFO: Quality profile for flex: Sonar way
INFO: Quality profile for js: Sonar way
INFO: Quality profile for kotlin: Sonar way
INFO: Quality profile for php: Sonar way
INFO: Quality profile for py: Sonar way
INFO: Quality profile for web: Sonar way
INFO: Quality profile for xml: Sonar way
INFO: ------------- Run sensors on module Example of SonarQube Scanner Usage
.....
INFO: Analysis report generated in 80ms, dir size=215.3 kB
INFO: Analysis report compressed in 50ms, zip size=69.6 kB
INFO: Analysis report uploaded in 402ms
INFO: ANALYSIS SUCCESSFUL, you can find the results at: http://192.168.0.81:9000/dashboard?id=org.sonarqube%3Asonarqube-scanner
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://192.168.0.81:9000/api/ce/task?id=AYackriauPeH7lVAPbwH
INFO: Analysis total time: 16.830 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 19.971s
INFO: Final Memory: 30M/107M
INFO: ------------------------------------------------------------------------
查看分析结果
扫描node项目
配置项目
在项目的根目录中创建一个名为的配置文件 sonar-project.properties
# projectKey是项目的唯一标识,不能出现重复
sonar.projectKey=dcmp-admin-rel
# --- optional properties ---
# 项目名称
sonar.projectName=dcmp-admin-rel
# 项目版本号
sonar.projectVersion=1.0
# 项目的语言
sonar.language=java
项目的源代码目录。 Defaults to .
sonar.sources=.
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
查看项目
# ls -l
total 1356
-rw-r--r-- 1 root root 565 Feb 8 18:21 commitlint.config.js
drwxr-xr-x 3 root root 186 Feb 28 10:16 dist
-rw-r--r-- 1 root root 4052 Feb 8 18:21 initCommitizen.sh
-rw-r--r-- 1 root root 550 Feb 8 18:36 Jenkinsfile
drwxr-xr-x 1014 root root 32768 Feb 28 10:15 node_modules
-rw-r--r-- 1 root root 2095 Feb 28 10:13 package.json
-rw-r--r-- 1 root root 1304947 Feb 28 10:15 package-lock.json
drwxr-xr-x 2 root root 145 Feb 15 16:10 public
-rw-r--r-- 1 root root 1236 Feb 8 18:21 README.md
-rw-r--r-- 1 root root 400 Mar 1 18:11 sonar-project.properties
drwxr-xr-x 13 root root 328 Feb 15 16:10 src
-rw-r--r-- 1 root root 651 Feb 8 18:21 tsconfig.json
分析项目
# /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.login=squ_9e14be0cd05d214ceca9e02b41eedbe897411a55
.....
INFO: Analysis report generated in 92ms, dir size=1.2 MB
INFO: Analysis report compressed in 279ms, zip size=680.0 kB
INFO: Analysis report uploaded in 65ms
INFO: ANALYSIS SUCCESSFUL, you can find the results at: http://192.168.0.81:9000/dashboard?id=dcmp-admin-rel
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://192.168.0.81:9000/api/ce/task?id=AYacrgkfuPeH7lVAPbwS
INFO: Analysis total time: 51.637 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 53.140s
INFO: Final Memory: 17M/64M
INFO: ------------------------------------------------------------------------
查看分析结果
参考文档
https://docs.sonarqube.org/latest/analyzing-source-code/scanners/sonarscanner/