Skip to content

1.3

安装sonar-scaner

# pwd
/opt/sonar-scanner-5.0.1.3006-linux
# chown -R sonar:sonar *
$ vi conf/sonar-scanner.properties
sonar.host.url=http://192.168.X.93:9000
sonar.sourceEncoding=UTF-8

# cat /etc/profile
# sonar-scanner
export SONAR_SCANNER_HOME=/opt/sonar-scanner-5.0.1.3006-linux
export PATH=$SONAR_SCANNER_HOME/bin:$PATH

# su - sonar
上一次登录:二 3月 26 09:22:25 CST 2024pts/1 上
$ sonar-scanner -v
INFO: Scanner configuration file: /opt/sonar-scanner-5.0.1.3006-linux/conf/sonar-scanner.p
ropertiesINFO: Project root configuration file: NONE
INFO: SonarScanner 5.0.1.3006
INFO: Java 17.0.7 Eclipse Adoptium (64-bit)
INFO: Linux 3.10.0-1160.el7.x86_64 amd64

扫描执行(命令和配置文件)

sonar-scanner  -Dsonar.sourceEncoding=UTF-8

sonar-scanner  -Dsonar.sourceEncoding=UTF-8 -Dsonar.pdf.username=admin -Dsonar.pdf.password=123

sonar-scanner  -Dsonar.sourceEncoding=UTF-8 -Dsonar.host.url=http://192.168.X.93:9000 -Dsonar.login=admin -Dsonar.password=123 -Dsonar.projectKey=AI-project -Dsonar.pdf.username=admin -Dsonar.pdf.password=123
sonar.host.url=http://192.168.X.93:9000/
sonar.sourceEncoding=UTF-8
sonar.login=admin
sonar.password=123
sonar.projectName=ai
sonar.projectKey=ai
sonar.projectVersion=0.0.1
sonar.sources=/home/sonar/source/

进阶学习

指定扫描的配置文件

// 参考指定配置,避免脚本传参
# cat sonar-scanner.properties | grep -v ^# | grep -v ^$
sonar.login=admin
sonar.password=123
sonar.pdf.username=admin
sonar.pdf.password=123
sonar.host.url=http://192.168.X.93:9000
sonar.sourceEncoding=UTF-8
sonar.projectKey=AI-Project
sonar.sources=.
sonar.language=python
# sonar-scanner -Dsonar.config={{PATH}}/sonar-scanner.properties

// 如上方式并没有生效,参数指定错误
INFO: Load global settings
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 0.808s
 INFO: Final Memory: 7M/54M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarScanner execution
ERROR: Not authorized. Analyzing this project requires authentication. Please provide a user token in sonar.login or other credentials in sonar.login and sonar.password.
ERROR: 
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.

// 通过如下方式解决
# sonar-scanner -Dproject.settings={{PATH}}/sonar-scanner.properties

集成Jacoco

异常排错

sonarqube

2024.03.26 00:04:51 INFO  app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up an
d runninguncaught exception in thread [main] java.lang.RuntimeException: can not run elasticsearch as root
// 解决方式
以非root方式启动
su - sonar
ERROR: [1] bootstrap checks failed. You must address the points described in the following
 [1] lines before starting Elasticsearch.bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
// 解决方式
# cat /etc/sysctl.conf
vm.max_map_count=262144
# sysctl -p
ERROR: PDF file not found in local filesystem. Report could not be sent to server.
[sonar@node1 source]$ sonar-scanner  -Dsonar.pdf.username=admin -Dsonar.pdf.password=123

// 解决生成报告失败的问题
https://blog.csdn.net/qq_39308071/article/details/122432748
INFO: Generating PDF report...
INFO: PDF report generated (see ai.pdf on build output directory)
INFO: Uploading PDF to server...
WARN: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
ERROR: Something went wrong storing the PDF at server side. Error: {"errors":[{"msg":"An error has occurred. Please contact your administrator"}]}
INFO: Analysis total time: 30.672 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 31.987s
INFO: Final Memory: 57M/220M
INFO: ------------------------------------------------------------------------
[root@node-1 source]# sonar-scanner
INFO: Scanner configuration file: /opt/sonar-scanner-5.0.1.3006-linux/conf/sonar-scanner.p
ropertiesINFO: Project root configuration file: NONE
INFO: SonarScanner 5.0.1.3006
INFO: Java 17.0.7 Eclipse Adoptium (64-bit)
INFO: Linux 3.10.0-1160.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Analyzing on SonarQube server 9.9.4.87374
INFO: Default locale: "zh_CN", source code encoding: "UTF-8"
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------

// 解决方式
sonar-scanner -Dsonar.sourceEncoding=your_encoding
ERROR: Error during SonarScanner execution
ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey
ERROR: 
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.

// 解决方式
增加 -Dsonar.projectKey=xxx 参数
INFO: PDF report generated (see AI-project.pdf on build output directory)
INFO: Uploading PDF to server...
WARN: Going to buffer response body of large or unknown size. Using getResponseBodyAsStrea
m instead is recommended.ERROR: Something went wrong storing the PDF at server side. Error: {"errors":[{"msg":"An error has occurred. Please contact your administrator"}]}
// 已经整理了相关的issue
https://gitee.com/zzulj/sonar-pdf-plugin/issues/I9BMI7

// Software version:
sonarqube version: Community Edition版本 9.9.4 (build 87374)
sonar-scanner version: sonar-scanner-5.0.1.3006
sonar-pdfreport-plugin: sonar-pdfreport-plugin-4.0.1.jar

// Install
CentOS 7.9 virtual machine deploy

// Exec command
sonar-scanner -Dsonar.sourceEncoding=UTF-8 -Dsonar.host.url=http://
{{IP}}:9000 -Dsonar.login=admin -Dsonar.password={{PWD}} -Dsonar.projectKey=AI-project -Dsonar.pdf.username=admin -Dsonar.pdf.password={{PWD}}
// Exec error log show
ERROR: Something went wrong storing the PDF at server side. Error: {"errors":[{"msg":"An e
rror has occurred. Please contact your administrator"}]}
// access.log
"POST /api/pdfreport/store HTTP/1.1" 500 -
"-" "Jakarta Commons-HttpClient/3.1" "AY54nNBx/Ewa6aQDAAKj"

// Loacl pdf report
ls -al .scannerwork/
总用量 992
drwxr-xr-x 2 root root 70 3月 26 11:15 .
drwxr-xr-x 11 root root 4096 3月 26 10:40 ..
-rw-r--r-- 1 root root 1004885 3月 26 11:15 AI-project.pdf
-rw-r--r-- 1 root root 249 3月 26 11:15 report-task.txt
-rw-r--r-- 1 root root 0 3月 26 10:40 .sonar_lock

// Add / at the end of port 9000, Just show this is unusefull
sonar-scanner -Dsonar.sourceEncoding=UTF-8 -Dsonar.host.url=http://
{{IP}}:9000/ -Dsonar.login=admin -Dsonar.password={{PWD}} -Dsonar.projectKey=AI-project -Dsonar.pdf.username=admin -Dsonar.pdf.password={{PWD}}
// log show
INFO: Uploading PDF to server...
INFO: PDF uploaded.
INFO: Analysis total time: 54.807 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 57.728s
INFO: Final Memory: 55M/190M
INFO: ------------------------------------------------------------------------

// click pdf download button the chrome url & result:
http://{{IP}}:9000/api/pdfreport/get?componentKey=AI-project
{"error":"Report is not available. At least one analysis is required after installing the plugin!"}

ERROR web[AY54nNBx/Ewa6aQDAAND][o.s.s.w.WebServiceEngine] Fail to process request http://{{ip}}:9000/api/pdfreport/storejava.security.AccessControlException: access denied ("java.io.FilePermission" "/opt/sonarqube-9.9.4.87374/pdf-files" "write")

// check dir access
ll sonarqube-9.9.4.87374
总用量 88
drwxr-xr-x 6 sonar sonar 96 2月 6 14:19 bin
drwxr-xr-x 2 sonar sonar 30 3月 26 13:34 conf
-rw-r--r-- 1 sonar sonar 7651 2月 6 14:19 COPYING
drwxr-xr-x 4 sonar sonar 46 3月 26 00:11 data
-rw-r--r-- 1 sonar sonar 77622 2月 6 14:21 dependency-license.json
drwxr-xr-x 7 sonar sonar 132 2月 6 14:27 elasticsearch
drwxr-xr-x 5 sonar sonar 57 3月 26 00:22 extensions
drwxr-xr-x 5 sonar sonar 132 2月 6 14:27 lib
drwxr-xr-x 2 sonar sonar 119 3月 26 00:23 logs
drwxrwxr-x 2 sonar sonar 28 3月 26 13:39 pdf-files
drwxr-xr-x 8 sonar sonar 213 3月 26 13:39 temp
drwxr-xr-x 5 sonar sonar 4096 2月 6 14:27 web
pwd
/opt
ll | grep sonarqube-9.9.4.87374
drwxr-xr-x 12 root root 189 3月 26 13:39 sonarqube-9.9.4.87374

chown -R sonar:sonar sonarqube-9.9.4.87374
ll | grep sonarqube-9.9.4.87374
drwxr-xr-x 12 sonar sonar 189 3月 26 13:39 sonarqube-9.9.4.87374

// Then restart sonarqube delete project and re-scan project
// downlad pdf reporter sucessfully