如何使用SonarQube+ SonarScanner分析项目

如何使用SonarQube+ SonarScanner分析项目_第1张图片


 

前言: 六一儿童节要玩程序员的玩具,动手试一试挺有意思的


目录

1. 安装sonarqube 

2. 获取Sonarqube令牌

3. 下载安装SonarScanner 

5. SonarScanner分析项目

7. 查看分析结果

8.常见问题


 版本信息:

  • Sonarqube7.6
  • Sonar-scanner-4.8.0

1. 安装sonarqube 并获取令牌

Linux 下安装SonarQube icon-default.png?t=N4P3https://blog.csdn.net/Beijing_L/article/details/130772649?spm=1001.2014.3001.5502

2. 获取Sonarqube令牌

1. 访问 http://[sonar_pi]:9000  并使用默认用户admin登录,密码也是 admin

如何使用SonarQube+ SonarScanner分析项目_第2张图片

 2. 右上角选择我的账号 - 安全

如何使用SonarQube+ SonarScanner分析项目_第3张图片

 3. 输入令牌名称生成令牌后, 并记录令牌信息,后面SonarScanner需要使用

如何使用SonarQube+ SonarScanner分析项目_第4张图片

 

3. 安装SonarScanner 

1. 下载sonarscanner

https://docs.sonarqube.org/latest/analyzing-source-code/scanners/sonarscanner/

如何使用SonarQube+ SonarScanner分析项目_第5张图片

 

2. 修改conf/sonar-scanner.properties 文件,配置sonar服务器信息, 当sonarscanner扫描项目后会将扫描信息上传到配置的服务器

如何使用SonarQube+ SonarScanner分析项目_第6张图片

#----- Default SonarQube server
sonar.host.url=http://10.0.192.21:9000
#----- Default source code encoding
sonar.sourceEncoding=UTF-8

3. 将bin文件夹的全路径添加到环境变量path下,例如

  • 先创建sonar_scanner变量,如下图
  • 在path下增加sonar_scanner变量:%SONAR_SCANNER_HOME%\bin;

如何使用SonarQube+ SonarScanner分析项目_第7张图片

 

4. 配置要扫描的项目

在项目根目录下增加sonar-project.properties配置信息

sonar.projectKey=STARFSA-HLJ
sonar.projectName=STARFSA-HLJ
sonar.projectVersion=4.0.1
sonar.sources=.
sonar.java.binaries=**/target/classes

其中

  •  sonar.projectKey配置为项目KEY ,用户标识唯一
  • sonar.projectName配置项目名称
  • sonar.projectVersion=配置项目版本
  • sonar.sources配置相对于sonar-project.properties路径的文件
  • sonar.java.binaries配置扫描的所需要的class 文件路径

5. SonarScanner分析项目

使用sonar-scanner -Dsonar.login=sonar口令扫描项目

G:\GITLIB\SMSCA>
G:\GITLIB\SMSCA>sonar-scanner -Dsonar.login=b8741b83ed9141f0011fe2cfbd09ca2360769b45
INFO: Scanner configuration file: D:\Soft\sonar-scanner-4.8.0.2856-windows\bin\..\conf\sonar-scanner.properties
INFO: Project root configuration file: G:\GITLIB\SMSCA\sonar-project.properties
INFO: SonarScanner 4.8.0.2856
INFO: Java 11.0.17 Eclipse Adoptium (64-bit)
INFO: Windows 7 6.1 amd64
INFO: User cache: C:\Users\10001874\.sonar\cache
INFO: Analyzing on SonarQube server 7.6.0
INFO: Default locale: "zh_CN", source code encoding: "GBK" (analysis is platform dependent)
INFO: Load global settings
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.protobuf.UnsafeUtil (file:/C:/Users/10001874/.sonar/cache/993f707e3af7ba4f46f12103912
WARNING: Please consider reporting this to the maintainers of com.google.protobuf.UnsafeUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
INFO: Load global settings (done) | time=76ms
INFO: Server id: 9D2906DF-AXTZNwmu9mGkYmmLG0AI
INFO: User cache: C:\Users\10001874\.sonar\cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=46ms
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=96ms
INFO: Process project properties
INFO: Execute project builders
INFO: Execute project builders (done) | time=4ms
INFO: Project key: STARIBOSS-5GCA
INFO: Base dir: G:\GITLIB\SMSCA
INFO: Working dir: G:\GITLIB\SMSCA\.scannerwork
INFO: Load project settings
INFO: Load project settings (done) | time=797ms
INFO: Load project repositories
INFO: Load project repositories (done) | time=528ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=43ms
INFO: Load active rules
INFO: Load active rules (done) | time=616ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=23ms
INFO: Indexing files...
INFO: Project configuration:
INFO: 10739 files indexed
INFO: Quality profile for java: Sonar way
INFO: Quality profile for xml: Sonar way
INFO: ------------- Run sensors on module STARIBOSS-5GCA
INFO: Sensor JavaSquidSensor [java]
INFO: Configured Java source version (sonar.java.source): none
INFO: JavaClasspath initialization
WARN: Bytecode of dependencies was not provided for analysis of source files, you might end up with less precise results. Bytecode can
INFO: JavaClasspath initialization (done) | time=154ms
INFO: JavaTestClasspath initialization
INFO: JavaTestClasspath initialization (done) | time=5ms
INFO: Java Main Files AST scan
INFO: 4681 source files to be analyzed
WARN: Invalid character encountered in file G:/GITLIB/SMSCA/proxy/ca-frontend-service/src/main/java/com/star/sms/business/provision/ft
coding to be used using property 'sonar.sourceEncoding'.
INFO: 48/4681 files analyzed, current file: G:\GITLIB\SMSCA\proxy\ca-frontend-service\src\main\java\com\star\sms\business\provision\se
WARN: Invalid character encountered in file G:/GITLIB/SMSCA/proxy/ca-frontend-service/src/main/java/com/star/sms/business/provision/se
t or configure the encoding to be used using property 'sonar.sourceEncoding'.
WARN: Invalid character encountered in file G:/GITLIB/SMSCA/proxy/ca-frontend-service/src/main/java/com/star/sms/business/provision/se
re the encoding to be used using property 'sonar.sourceEncoding'.
WARN: Invalid character encountered in file G:/GITLIB/SMSCA/proxy/ca-frontend-service/src/main/java/com/star/sms/business/provision/se
r configure the encoding to be used using property 'sonar.sourceEncoding'.
INFO: 242/4681 files analyzed, current file: G:\GITLIB\SMSCA\proxy\fiveg_interface\src\main\java\com\star\sms\service\provision\dto\fi
WARN: Invalid character encountered in file G:/GITLIB/SMSCA/sms/basic/common/src/main/java/com/star/common/interceptor/AbstractMethodC
 be used using property 'sonar.sourceEncoding'.
INFO: 789/4681 files analyzed, current file: G:\GITLIB\SMSCA\sms\basic\common\src\main\java\com\star\common\interceptor\AbstractMethod
INFO: 998/4681 files analyzed, current file: G:\GITLIB\SMSCA\sms\basic\domain\src\main\java\com\star\sms\electronic\annotation\PrintAc
INFO: 1647/4681 files analyzed, current file: G:\GITLIB\SMSCA\sms\basic\domain\src\main\java\com\star\sms\model\customer\CmInfo.java
INFO: 2003/4681 files analyzed, current file: G:\GITLIB\SMSCA\sms\basic\domain\src\main\java\com\star\sms\model\note\baiwang\tcd\param
INFO: 2427/4681 files analyzed, current file: G:\GITLIB\SMSCA\sms\basic\domain\src\main\java\com\star\sms\model\product\condition\cata
INFO: 3062/4681 files analyzed, current file: G:\GITLIB\SMSCA\sms\basic\domain\src\main\java\com\star\sms\model\resource\condition\rep
INFO: 3732/4681 files analyzed, current file: G:\GITLIB\SMSCA\sms\basic\domain\src\main\java\com\star\sms\model\resource\dto\five\g\re
INFO: 4412/4681 files analyzed, current file: G:\GITLIB\SMSCA\sms\basic\domain\src\main\java\com\star\sms\service\accept2\orderDisplay
INFO: 4681/4681 source files have been analyzed
INFO: Java Main Files AST scan (done) | time=103340ms
INFO: Java Test Files AST scan
INFO: 0 source files to be analyzed
INFO: Java Test Files AST scan (done) | time=4ms
INFO: 0/0 source files have been analyzed
INFO: Sensor JavaSquidSensor [java] (done) | time=103976ms
INFO: Sensor PmdSensor [pmd]
INFO: Sensor PmdSensor [pmd] (done) | time=5ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=23ms
INFO: Sensor SurefireSensor [java]
INFO: parsing [G:\GITLIB\SMSCA\target\surefire-reports]
INFO: Sensor SurefireSensor [java] (done) | time=4ms
INFO: Sensor JaCoCoSensor [java]
INFO: Sensor JaCoCoSensor [java] (done) | time=4ms
INFO: Sensor JavaXmlSensor [java]
INFO: 15 source files to be analyzed
WARN: Invalid character encountered in file G:/GITLIB/SMSCA/.idea/workspace.xml at line 418 for encoding GBK. Please fix file content
INFO: Sensor JavaXmlSensor [java] (done) | time=262ms
INFO: 15/15 source files have been analyzed
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=32ms
INFO: Sensor XML Sensor [xml]
INFO: 15 source files to be analyzed
WARN: Unable to analyse file file:///G:/GITLIB/SMSCA/.idea/workspace.xml;
INFO: Sensor XML Sensor [xml] (done) | time=188ms
INFO: 15/15 source files have been analyzed
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=848ms
INFO: ------------- Run sensors on project
INFO: Sensor Java CPD Block Indexer
INFO: Sensor Java CPD Block Indexer (done) | time=1434ms
INFO: SCM provider for this project is: git
INFO: 4696 files to be analyzed
INFO: 986/4696 files analyzed
INFO: 2302/4696 files analyzed
INFO: 3574/4696 files analyzed
INFO: 4683/4696 files analyzed
WARN: Missing blame information for the following files:
WARN:   * .idea/vcs.xml
WARN:   * .idea/shelf/Uncommitted_changes_before_Checkout_at_2023_5_16_13_15__Changes_.xml
WARN:   * .idea/uiDesigner.xml
WARN:   * .idea/runConfigurations.xml
WARN:   * sms/basic/domain/src/main/java/com/star/sms/model/customer/queryCondition/ContractSearchCondition.java
WARN:   * .idea/workspace.xml
WARN:   * .idea/misc.xml
WARN:   * .idea/gradle.xml
WARN:   * proxy/ca-frontend-service/build/resources/main/spring/business-beans-core.xml
WARN:   * .idea/compiler.xml
WARN:   * .idea/jarRepositories.xml
WARN:   * proxy/ca-frontend-service/build/resources/main/spring/sendInterface-servlet.xml
WARN:   * platform/platform-boot-config/build/resources/main/logback.xml
WARN: This may lead to missing/broken features in SonarQube
INFO: 1194 files had no CPD blocks
INFO: Calculating CPD for 3487 files
INFO: CPD calculation finished
INFO: Analysis report generated in 6030ms, dir size=23 MB
INFO: Analysis report compressed in 9210ms, zip size=14 MB
INFO: Analysis report uploaded in 2953ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://127.0.0.1:9000/dashboard?id=STARIBOSS-5GCA
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://127.0.0.1:9000/api/ce/task?id=AYgtdRwEstOHat1LEBpi
INFO: Analysis total time: 3:10.386 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 3:11.760s
INFO: Final Memory: 21M/77M
INFO: ------------------------------------------------------------------------
请按任意键继续. . .

6. 查看分析结果

分析结束后,分析信息会上传到sonar服务器上, 此时访问服务器会显示有个后台任务正在处理

分析报告

如何使用SonarQube+ SonarScanner分析项目_第8张图片

 等待几分钟后查看项目信息

如何使用SonarQube+ SonarScanner分析项目_第9张图片

 如何使用SonarQube+ SonarScanner分析项目_第10张图片

8.常见问题

1)AnalysisException:Please provide compiled classes of your project with sonar.java.binaries property

INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 3:02.725s
INFO: Final Memory: 14M/54M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarScanner execution
org.sonar.java.AnalysisException: Please provide compiled classes of your project with sonar.java.binaries property
        at org.sonar.java.JavaClasspath.init(JavaClasspath.java:66)
        at org.sonar.java.AbstractJavaClasspath.getElements(AbstractJavaClasspath.java:280)
        at org.sonar.java.SonarComponents.getJavaClasspath(SonarComponents.java:175)
        at org.sonar.java.JavaSquid.(JavaSquid.java:82)
        at org.sonar.plugins.java.JavaSquidSensor.execute(JavaSquidSensor.java:91)

问题原因: sonarqube的sonar-java插件从4.1.2开始,强制要求配置sonar.java.binaries参数

2)MySQLSyntaxErrorException: The size of BLOB/TEXT data inserted in one transaction is greater than 10% of redo log size. 

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: The size of BLOB/TEXT data inserted in one transaction is greater than 10% of redo log size. Increase the redo log size using innodb_log_file_size.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)

问题原因:mysql中的innodb日志文件配置过小,增加配置的值后问题可解决 

如何使用SonarQube+ SonarScanner分析项目_第11张图片

3)PacketTooBigException: Packet for query is too large (16738132 > 4194304).

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (16738132 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3681)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2512)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)

 问题原因:MySQL会限制Server接受的数据包大小。有时候插入、更新或查询时数据包的大小,会受 max_allowed_packet 参数限制,当大于配置的值的时候就会出上述错误, 通过命令可查询配置的信息并修改配置信息

show VARIABLES like '%max_allowed_packet%';


set global max_allowed_packet = 2* 1024 * 1024*10;

上一篇:Linux 下安装SonarQube (版本sonarqube7.6)

你可能感兴趣的:(CICD,java,开发语言)