SonarQube-代码质量检测工具

SonarQube是一款代码质量检测工具,用于检测代码BUG,漏洞之类的,当然这类工具是辅助工具,不一定是代码有问题,需要设置扫描规则

如果需要使用轻量级的,则可以直接安装sonarlint IDEA插件,本地化操作,不需要安装SonarQube服务端
sonarlint 连接SonarQube

环境检查:

首选需要用到数据库,oracle、sql server、mysql都可以
这里使用mysql,安装之前需要先检查环境配置,
SonarQube8.0以上使用的是JDK11以上版本,本地环境是JDK8,因此下载的是SonarQube7.4,但是要求mysql版本[5.6-8),因此装机mysql是5.7版本
SonarQube-代码质量检测工具_第1张图片
SonarQube-代码质量检测工具_第2张图片
安装:

1.安装数据库

安装数据库,此处选用mysql5.7
–mysql-zip版本安装
–建用户赋权


2.配置启动SonarQube

根目录conf/sonar.properties,添加数据库连接配置

#----- DEPRECATED 
#----- MySQL >=5.6 && <8.0
# Support of MySQL is dropped in Data Center Editions and deprecated in all other editions
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

sonar.jdbc.url=jdbc:mysql://localhost:3307/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=sonarqube
sonar.jdbc.password=sonarqube
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin

上面的信息明确显示mysql版本5.6-8.0

以管理员运行cmd,sonarqube-7.4\bin\windows-x86-64,执行StartSonar.bat

之间会初始化数据库,建立60张表

3.配置sonar-scanner

sonar-scanner-4.2.0.1873-windows\conf\sonar-scanner.properties
添加SonarQube访问地址

sonar.host.url=http://localhost:9000

配置环境变量path

D:\Program Files\sonar-scanner-4.2.0.1873-windows\bin

配置成功检测,执行命令 sonar-scanner --version
在这里插入图片描述
4.编写扫描文件

扫描文件位置需要放在项目根目录(与pom、src、target同级),统一命名为sonar-project.properties

# must be unique in a given SonarQube instance
sonar.projectKey=zyb-crm-service:project
# this is the name displayed in the SonarQube UI
sonar.projectName=cic-crm-service
sonar.projectVersion=1.0
 
# 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
sonar.java.binaries=target
#sonar.language=java
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

sonar.projectName:扫描项目名称
sonar.projectKey:自定义唯一项目key
sonar.sources:扫描目录(路径)
sonar.java.binaries:编译文件目录

5.扫描
cmd进入扫描项目根目录,执行命令sonar-scanner

6.与IDEA关联-插件
file–settings–plugins–sonar

SonarQube-代码质量检测工具_第3张图片
可以选择sonarqube,不过轻量级使用sonarlint足够

问题:
1.上传文件大小限制:
sonar-scanner报错

ERROR: Error during SonarQube Scanner execution
ERROR: Failed to upload report - An error has occurred. Please contact your administrator

查看sonar安装目录下的logs中的web.log,里面有如下报错信息

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (18307204 > 16777216). 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)

是说向MySQL数据库中写数据的时候超过了单次最大数据量,可通过修改MySQL中max_allowed_packet参数进行解决。

在mysql配置文件中添加如下配置
SonarQube-代码质量检测工具_第4张图片
2.重启SonarQube报错,关闭的链接

报错WrapperSimpleApp: Encountered an error running main: java.nio.file.FileSystemException: D:\sonarqube-7.8\temp\ce-exploded-plugins\authgithub\META-INF\lib\gson-2.3.1.jar: 另一个程序正在使用此文件,进程无法访问。

进入SonarQube安装目录,删除temp下所有内容

3.中文插件只有匹配SonarQube8.0问题
自己登陆下载相应版本的中文插件jar,下有链接,复制到SonarQube安装目录extensions\plugins下

下载:https://www.sonarqube.org/downloads/
中文插件:https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/tag/sonar-l10n-zh-plugin-1.24

你可能感兴趣的:(运维)