白盒测试工具 - sonar的安装、配置与使用入门手册,用sonar检查代码质量实战演示

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 白盒测试工具 - sonar的安装、配置与使用入门手册,用sonar检查代码质量实战演示

       

sonar 检测代码质量实战演示

安装过程主要分为两部分,一个是主程序 sonarqube,一个是 sonarScanner 插件,两个都安装配置好后就可以进行代码扫描了。sonarqube 在配置数据库的过程中会碰到很多问题,第二章专门针对这些问题来进行讲解。

第一章:sonarqube 的安装与启用

① sonarqube 获取

下载地址:官方网站

image.png

② sonarqube 配置

解压后的程序目录下,进入 conf 里的 sonar.properties 来进行配置。

image.png

# 指定oracle数据库
sonar.jdbc.url=jdbc:oracle:thin:@10.10.18.70:1521/orcl
# 指定数据库用户名
sonar.jdbc.username=ncc_auto_sonar 
# 指定数据库密码
sonar.jdbc.password=1
# 指定端口
sonar.web.port=9000

image.png

③ sonarqube 驱动放置

不要忘了把驱动放到

image.png

④ sonarqube 的启动

StartSonar 来启动程序,如果闪退了,可以拖到 cmd 里运行,可以看到报错信息,第二章就介绍它的一些常见报错的解决方法。

image.png

然后再访问 localhost:9000 就进来了。

image.png

第二章:sonarqube 报错解决

① Unsupported JDBC driver provider: mysql. 【sonarqube8.0 以后不支持 mysql】

sonarqube8.0 以后不再支持 mysql,所以我用 oracle 来做适配。

jvm 1    | WrapperSimpleApp: Encountered an error running main: org.sonar.proces
s.MessageException: Unsupported JDBC driver provider: mysql
jvm 1    | org.sonar.process.MessageException: Unsupported JDBC driver provider:
 mysql
wrapper  | <-- Wrapper Stopped

image.png

② Directory does not contain JDBC driver: extensions/jdbc-driver/oracle. 【没有找到驱动】

没有配置数据库驱动。extensions/jdbc-driver/oracle 下面没有放驱动,或是驱动放的不对。

jvm 1    | WrapperSimpleApp: Encountered an error running main: org.sonar.proces
s.MessageException: Directory does not contain JDBC driver: extensions/jdbc-driv
er/oracle
jvm 1    | org.sonar.process.MessageException: Directory does not contain JDBC d
river: extensions/jdbc-driver/oracle
wrapper  | <-- Wrapper Stopped

image.png

③ SonarQube requires Java 11 to run. 【sonarqube8.0 需要 java11 的支持】

我原来用的版本是 java8 的。

image.png

运行时会提示要用 java11 来运行。

jvm 1    | WrapperSimpleApp: Encountered an error running main: java.lang.Illega
lStateException: SonarQube requires Java 11 to run
jvm 1    | java.lang.IllegalStateException: SonarQube requires Java 11 to run

image.png

java11 官网下载

image.png

安装完后配置环境变量后就可以了。

image.png

④ 远程主机强迫关闭了一个现有的连接。【sonarqube 数据库驱动版本不匹配】

报这个错感觉我的数据库驱动版本不对,然后换了个驱动就好了。

jvm 1    | 2020.05.22 17:25:35 WARN  app[][o.e.t.TcpTransport] exception caught
on transport layer [Netty4TcpChannel{localAddress=/127.0.0.1:62245, remoteAddres
s=/127.0.0.1:9001}], closing connection
jvm 1    | java.io.IOException: 远程主机强迫关闭了一个现有的连接。

image.png

然后再访问 localhost:9000 就进来了。

image.png

⑤ 查看 sonarqube 的数据库支持

配置文档里有很多的帮助,我直接搜 oracle 就能查看 oracle 的支持了,还有下面说的驱动放置方法,以及更下面的官方支持网站,如果还有其它的问题,兴许就能在里面找到问题的解决方法。

image.png

第三章:sonarScanner 插件的安装与配置

① sonarScanner 获取

获取地址:官网下载

下载完后解压即可。

image.png

② sonarScanner 设置环境变量

我们把它根目录下面的 bin 文件夹加入环境变量。

image.png

image.png

cmd 里输入 sonar-scanner -h 可以看有没有配置对。

image.png

③ sonarScanner 配置

在这个配置文件夹里的配置文件进行配置。

image.png

还是跟前面 sonarqube 的数据库配置一样。

# 指定oracle数据库
sonar.jdbc.url=jdbc:oracle:thin:@10.10.18.70:1521/orcl
# 指定数据库用户名
sonar.jdbc.username=ncc_auto_sonar 
# 指定数据库密码
sonar.jdbc.password=1

image.png

第四章:用 sonar 检查代码质量

① 配置

在要检查代码的地方建个这个配置文件 sonar-project.properties

image.png

sonarScanner 官网 给的配置方法。

image.png

# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# --- optional properties ---
# defaults to project key
#sonar.projectName=My project
# defaults to 'not provided'
#sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Defaults to .
#sonar.sources=.
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

image.png

② 运行

在放好配置文件的目录下输入 sonar-scanner 命令就可以扫描了。

image.png

扫描成功标志。

image.png

扫描过程详细信息。

C:\Users\Administrator\Desktop\XpathRobot>sonar-scanner
INFO: Scanner configuration file: D:\server\sonar\sonar-scanner-4.3.0.2102-windo
ws\bin\..\conf\sonar-scanner.properties
INFO: Project root configuration file: C:\Users\Administrator\Desktop\XpathRobot
\sonar-project.properties
INFO: SonarScanner 4.3.0.2102
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Windows 7 6.1 amd64
INFO: User cache: C:\Users\Administrator\.sonar\cache
INFO: Scanner configuration file: D:\server\sonar\sonar-scanner-4.3.0.2102-windo
ws\bin\..\conf\sonar-scanner.properties
INFO: Project root configuration file: C:\Users\Administrator\Desktop\XpathRobot
\sonar-project.properties
INFO: Analyzing on SonarQube server 8.2.0
INFO: Default locale: "zh_CN", source code encoding: "GBK" (analysis is platform
 dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=205ms
INFO: Server id: 02A721FB-AXJaPgmVryaM9Bas_OmF
INFO: User cache: C:\Users\Administrator\.sonar\cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=161ms
INFO: Load/download plugins (done) | time=3743ms
INFO: Process project properties
INFO: Process project properties (done) | time=1ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=5ms
INFO: Project key: my:project
INFO: Base dir: C:\Users\Administrator\Desktop\XpathRobot
INFO: Working dir: C:\Users\Administrator\Desktop\XpathRobot\.scannerwork
INFO: Load project settings for component key: 'my:project'
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=305ms
INFO: Load active rules
INFO: Load active rules (done) | time=3550ms
WARN: SCM provider autodetection failed. Please use "sonar.scm.provider" to defi
ne SCM of your project, or disable the SCM Sensor in the project settings.
INFO: Indexing files...
INFO: Project configuration:
INFO: 21 files indexed
INFO: Quality profile for py: Sonar way
INFO: Quality profile for web: Sonar way
INFO: ------------- Run sensors on module my:project
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=116ms
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by net.sf.cglib.core.ReflectUtils$1 (file:/C:
/Users/Administrator/.sonar/cache/54f6535c111cefad0fb6a09ba3e61922/sonar-javascr
ipt-plugin.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byt
e[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of net.sf.cglib.core.
ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflect
ive access operations
WARNING: All illegal access operations will be denied in a future release
INFO: Sensor Python Sensor [python]
INFO: Starting global symbols computation
INFO: 3 source files to be analyzed
INFO: Load project repositories
INFO: Load project repositories (done) | time=14ms
INFO: Starting rules execution
INFO: 3 source files to be analyzed
INFO: 3/3 source files have been analyzed
INFO: Sensor Python Sensor [python] (done) | time=1481ms
INFO: Sensor Cobertura Sensor for Python coverage [python]
INFO: 3/3 source files have been analyzed
INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=23ms
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=11ms
INFO: Sensor SonarCSS Rules [cssfamily]
INFO: 1 source files to be analyzed
INFO: 1/1 source files have been analyzed
INFO: Sensor SonarCSS Rules [cssfamily] (done) | time=14364ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=4ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=2ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=150ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=48ms
INFO: SCM Publisher No SCM system was detected. You can use the 'sonar.scm.provi
der' property to explicitly specify it.
INFO: CPD Executor 1 file had no CPD blocks
INFO: CPD Executor Calculating CPD for 3 files
INFO: CPD Executor CPD calculation finished (done) | time=15ms
INFO: Analysis report generated in 106ms, dir size=214 KB
INFO: Analysis report compressed in 99ms, zip size=52 KB
INFO: Analysis report uploaded in 1527ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard?id=my%
3Aproject
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://localhost:9000/api/ce/task?id=A
XJeUNiUryaM9Bas_RCa
INFO: Analysis total time: 25.030 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 32.540s
INFO: Final Memory: 13M/50M
INFO: ------------------------------------------------------------------------

③ 查看扫描报告

刷新后就能看到扫描后的结果了!

image.png

④ 分析报告问题

点进 bug 里可以看到很多问题。

image.png

点击 Why is this an issue? 可以查看问题原因,点击整个粉色的部分可以追踪到代码。

image.png

⑤ 代码质量评级

点击 quality gates 可以查看代码质量的综合评分。

image.png

第五章:sonar 相关设置

① sonar 管理员登录

sonar 管理员登录。

image.png

管理员的账号和密码都是 admin

image.png

② sonar 设置

管理员登录后才能看到设置页签。

image.png

喜欢的点个赞❤吧!

   

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
16天前
|
存储 关系型数据库 测试技术
玩转n8n测试自动化:核心节点详解与测试实战指南
n8n中节点是自动化测试的核心,涵盖触发器、数据操作、逻辑控制和工具节点。通过组合节点,测试工程师可构建高效、智能的测试流程,提升测试自动化能力。
|
2月前
|
Web App开发 人工智能 JavaScript
主流自动化测试框架的技术解析与实战指南
本内容深入解析主流测试框架Playwright、Selenium与Cypress的核心架构与适用场景,对比其在SPA测试、CI/CD、跨浏览器兼容性等方面的表现。同时探讨Playwright在AI增强测试、录制回放、企业部署等领域的实战优势,以及Selenium在老旧系统和IE兼容性中的坚守场景。结合六大典型场景,提供技术选型决策指南,并展望AI赋能下的未来测试体系。
|
2月前
|
存储 人工智能 算法
AI测试平台实战:深入解析自动化评分和多模型对比评测
在AI技术迅猛发展的今天,测试工程师面临着如何高效评估大模型性能的全新挑战。本文将深入探讨AI测试平台中自动化评分与多模型对比评测的关键技术与实践方法,为测试工程师提供可落地的解决方案。
|
8天前
|
机器学习/深度学习 人工智能 测试技术
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
EdgeMark是一个面向嵌入式AI的自动化部署与基准测试系统,支持TensorFlow Lite Micro、Edge Impulse等主流工具,通过模块化架构实现模型生成、优化、转换与部署全流程自动化,并提供跨平台性能对比,助力开发者在资源受限设备上高效选择与部署AI模型。
101 9
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
|
2月前
|
人工智能 缓存 测试技术
Playwright进阶指南 (6) | 自动化测试实战
2025企业级测试解决方案全面解析:从单元测试到千级并发,构建高可用测试体系。结合Playwright智能工具,解决传统测试维护成本高、环境依赖强、执行效率低等痛点,提升测试成功率,内容从测试架构设计、电商系统实战框架、高级测试策略、Docker化部署、CI/CD集成及AI测试应用,助力测试工程师掌握前沿技术,打造高效稳定的测试流程。
Playwright进阶指南 (6) | 自动化测试实战
|
21天前
|
人工智能 数据可视化 测试技术
AI 时代 API 自动化测试实战:Postman 断言的核心技巧与实战应用
AI 时代 API 自动化测试实战:Postman 断言的核心技巧与实战应用
265 11
|
16天前
|
Java 测试技术 API
自动化测试工具集成及实践
自动化测试用例的覆盖度及关键点最佳实践、自动化测试工具、集成方法、自动化脚本编写等(兼容多语言(Java、Python、Go、C++、C#等)、多框架(Spring、React、Vue等))
58 6
|
16天前
|
Java 测试技术 数据安全/隐私保护
通过yaml文件配置自动化测试程序
通过yaml文件可以将自动化测试环境,测试数据和测试行为分开,请看一下案例
37 4
|
2月前
|
XML Ubuntu Java
如何在Ubuntu系统上安装和配置JMeter和Ant进行性能测试
进入包含 build.xml 的目录并执行:
106 13
|
2月前
|
前端开发 Java jenkins
Jmeter压力测试工具全面教程和使用技巧。
JMeter是一个能够模拟高并发请求以检查应用程序各方面性能的工具,包括但不限于前端页面、后端服务及数据库系统。熟练使用JMeter不仅能够帮助发现性能瓶颈,还能在软件开发早期就预测系统在面对真实用户压力时的表现,确保软件质量和用户体验。在上述介绍的基础上,建议读者结合官方文档和社区最佳实践,持续深入学习和应用。
500 10

热门文章

最新文章