Sonar代码质量管理

一:简介

Sonar一个Web系统,展现了静态代码扫描的结果,结果是可以自定义的 ,支持多种语言的原理是它的扩展性。

在这里插入图片描述

在这里插入图片描述

二:JDK安装

SonarQube7.8版本需要jdk1.8或者更高的版本。

三:SonarQube安装

1. SonarQube 下载

SonarQube 下载地址 注意:要下载7.8版本,因为7.8版本以上的不再支持mysql数据库了,下载后简单解压一下即可。

直接点击Community Edition即可,不用点下拉的选项。
在这里插入图片描述
在这里插入图片描述

2. 启动SonarQube Web服务

通过命令行启动 ./sonar.sh start

  • start 启动web项目
  • console 查看start的命令打印的日志,即查看logs/sonar.log文件内容
    在这里插入图片描述

访问SonarQube Web地址 http://localhost:9000
在这里插入图片描述

3. 配置数据库信息

# 需要预先创建好数据库
CREATE DATABASE `sonar` DEFAULT CHARACTER SET utf8;

/sonarqube-7.8/conf/sonar.properties

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
# 数据库的用户名
sonar.jdbc.username=root
# 数据库密码
sonar.jdbc.password=root123
sonar.sorceEncoding=UTF-8
# SonarQube Web服务登录的用户名和密码
sonar.login=admin
sonar.password=admin

在这里插入图片描述

4. 重启命令restart

restart命令执行后会初始化数据库表
在这里插入图片描述
在这里插入图片描述

5. 使用用户名和密码再次访问SonarQube Web

http://localhost:9000 输入用户名和密码,即在sonar.properties文件中配置的login和password值,admin amdin
在这里插入图片描述

6. 安装中文包插件

在这里插入图片描述
重启使插件生效
在这里插入图片描述
在这里插入图片描述

四:SonarScanner安装

1. 下载SonarScanner

SonarScanner下载地址

配置sonar-scanner-4.0.0.1744-macosx/conf/sonar-scanner.properties数据库信息

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=root
sonar.jdbc.password=root123

在这里插入图片描述

2. 添加环境变量

vi ~/.bash_profile
export SONAR_RUNNER_HOME=/Users/mengday/Softwares/sonar/sonar-scanner-4.0.0.1744-macosx
export PATH=$SONAR_RUNNER_HOME/bin:$PATH
soruce ~/.bash_profile

# 查看sonar-scanner命令是否生效
sonar-scanner --version

在这里插入图片描述

3. 在一个maven项目中配置sonar-project.properties文件

本文使用了springboot-pay-example 这个项目作为演示的,可以到github上限下载下来,然后在idea中打开。

在一个项目的根路径下创建sonar-project.properties文件,并设置属性值
在这里插入图片描述

sonar-project.properties

# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name displayed in the SonarQube UI
sonar.projectName=springboot-pay-example
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
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
#path to your project build output path
sonar.java.binaries=target/classes
  • sonar.projectName是项目名字,
  • sonar.sources是源文件所在的目录,
  • sonar.java.binaries SonarQube新版相对于之前的版本新增要求必须指定项目class文件的目录,如果不配置会报错,注意target/classes目录必须存在,为了确保target/classes存在,先通过maven clean install一下
# 切换到项目根目录下
cd /Users/mengday/Projects/learning/springboot-pay-example
# 开始代码扫描分析
sonar-scanner

在这里插入图片描述

4. 查看代码分析结果

在这里插入图片描述
这里分析出来2个Bug
在这里插入图片描述

在这里插入图片描述
点开第二个查看具体问题代码
在这里插入图片描述

参考文章:

  • https://www.jianshu.com/p/aa863cf30406
  • https://www.hellojava.com/a/82379.html
  • http://www.info110.com/96657.html
发布了308 篇原创文章 · 获赞 936 · 访问量 133万+

猜你喜欢

转载自blog.csdn.net/vbirdbest/article/details/99957019