1 jenkins和 SnoarQube 和 sonar-scanner 之间有依赖关系,版本需要符合条件最后才可以生成报表
持续集成环境:Jenkins
SonarQube和sonar-scanner的关系就像是 github 官网和我们本地的 git 软件的关系
1 安装jenkins
SonarQube 版本号 7.6 不能是7.9之上,不然,需要java 版本11
sonar-scanner 提取码:cy1n
mysql 5.7.19 提取码:pwwv 这个版本最好是5.6. 5.7 都行,对版本也有限制
2 安装mysql
3.首先需要安装 MySql 数据库(为了方便可配置mysql相关环境变量,同时第一次进入mysql需要修改密码,此处都略过,百度一下)
root 登录 mysql,创建 sonar 数据库和用户授权。这个
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; #新建数据库;数据库中不用自己建表,执行分析命令时会自动在库中建表
CREATE USER ‘sonar‘ IDENTIFIED BY ‘sonar‘; #新建用户
GRANT ALL ON sonar.* TO ‘sonar‘@‘%‘ IDENTIFIED BY ‘sonar‘; #赋权限,所有pc 都可以访问;还需要设置用户密码
GRANT ALL ON sonar.* TO ‘sonar‘@‘localhost‘ IDENTIFIED BY ‘sonar‘;
FLUSH PRIVILEGES; #刷新缓存立即生效
4 安装 SonarQube
sonarqube已解压
在路径下的 conf 文件夹下修改 sonar.properties
url是数据库连接地址,username是数据库用户名,jdbc.password是数据库密码,login是sonarqube的登录名,sonar.password是sonarqube的密码
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=root
sonar.jdbc.password=walker123
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
之后打开 bin 目录下对应的 StartSnoar.bat 就可以启动 Snoar 服务器了。可通过浏览器 http://127.0.0.1:9000 查看,就可以访问了
5
配置 sonar-scanner(SonarQube和sonar-scanner的关系就像是 github 官网和我们本地的 git 软件的关系);必须版本合适
1.配置 sonar-scanner 目录下 conf 中的 snoar-scanner.properties sonar-scanner
sonar.jdbc.url=jdbc:mysql://mysql:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=root
sonar.jdbc.password=root
配置环境变量
a.新建变量,name=SONAR_RUNNER_HOME。value=D:\sonar\sonar-scanner-2.5
b.打开path,输入%SONAR_RUNNER_HOME%\bin;
cmd 执行如下命令 sonar-runner -v 未报错即ok
6 打开要进行代码分析的项目根目录,新建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=apiautocore
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
原文:https://www.cnblogs.com/pythonwork/p/13039192.html