当前位置: 首页 > news >正文

Windows配置SonarQube代码审查工具详细步骤(附带IDEA SonarLint插件使用)

文章目录

    • 环境说明以及准备
    • 一. SonarQube的下载与安装
    • 二. 添加SonarQube项目
    • 三. 使用Maven命令上传代码到SonarQube
    • 四. IDEA安装SonarLint插件


环境说明以及准备

本篇博客使用的SonarQube版本为9.8,注意JDK 1.8已经不能支持

NameVersionDownLoad Link
SonarQube9.8https://www.sonarsource.com/products/sonarqube/downloads/historical-downloads/
JDK11https://pan.quark.cn/s/06848544167c
PostgreSQL14.2https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

更多环境配置请参考:https://docs.sonarsource.com/sonarqube/9.8/requirements/prerequisites-and-overview/

在这里插入图片描述

一. SonarQube的下载与安装

1.下载9.8版本压缩包:
在这里插入图片描述
2.解压缩后找到/conf文件夹下的sonar.properties文件

在这里插入图片描述
3.增加postgresql连接配置

sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube
sonar.jdbc.username=postgres
sonar.jdbc.password=postgres

在这里插入图片描述
postgresql配置:

在这里插入图片描述

4.找到\bin\windows-x86-64文件夹下的StartSonar.bat文件,双击启动

在这里插入图片描述
在这里插入图片描述

5.启动成功后,访问http://localhost:9000/
初始用户名和密码 admin -> admin

在这里插入图片描述6.修改初始密码

在这里插入图片描述

7.修改完密码之后你就能成功看到SonarQube的页面

在这里插入图片描述

二. 添加SonarQube项目

SonarQube可以从你的远程Git仓库中读取代码并扫描。
本篇博客主要说明如何在本地通过手动添加并上传到SonarQube进行代码审查

1.点击Manually,创建一个新的project

在这里插入图片描述
2.注意这里的Project Key是你代码pom.xml文件中的artifactId
我这里随便找了个demo项目作为示例:

在这里插入图片描述

在这里插入图片描述
3.生成token,token名称你可以自己定,我们使用maven命令上传到SonarQube的时候需要带上这个token
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三. 使用Maven命令上传代码到SonarQube

1.修改Maven的setting.xml文件,增加sonarqube配置,具体可以参考 SonarQube maven配置

在这里插入图片描述
2.setting.xml配置代码

  <pluginGroups><!-- sonarqube --><pluginGroup>org.sonarsource.scanner.maven</pluginGroup></pluginGroups>
	<profiles><profile><id>sonar</id><activation><activeByDefault>true</activeByDefault></activation><properties><!-- Optional URL to server. Default value is http://localhost:9000 --><sonar.host.url>http://localhost:9000</sonar.host.url></properties></profile></profiles>

在这里插入图片描述
在这里插入图片描述
3.提交代码中需要在pom.xml文件中添加jacoco-maven-plugin插件来帮助你生成test的覆盖率report

        <dependency><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.1</version><type>pom</type></dependency>
            <plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.8</version><executions><execution><goals><goal>prepare-agent</goal></goals></execution><execution><id>report</id><phase>test</phase><goals><goal>report</goal></goals></execution></executions></plugin>

4…然后在你代码目录下(pom.xml文件同级目录),运行以下命令即可:
将上述SonarQube提示的maven命令简化成一行命令:

mvn clean verify sonar:sonar \-Dsonar.projectKey=redis-demo \-Dsonar.host.url=http://localhost:9000 \-Dsonar.login=sqp_70f1378aff945b99a854006d4b532f0081cc49f2

注意这里Dsonar.login的值就是上面拿到的token

mvn clean verify sonar:sonar -Dsonar.projectKey=redis-demo -Dsonar.host.url=http://localhost:9000 -Dsonar.login=sqp_70f1378aff945b99a854006d4b532f0081cc49f2

5.运行成功后,你就能在SonarQube上面看见一些report

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
6.如果你有一些代码不想让SonarQube扫描,你还可以添加以下配置

        <sonar.exclusions><!-- 指定需要排除的包 -->src/main/java/com/example/redisdemo/config/**,<!-- 排除以Config结尾的类 -->src/main/java/com/example/redisdemo/config/*Config.*</sonar.exclusions><sonar.coverage.exclusions>**/generated/**</sonar.coverage.exclusions>

在这里插入图片描述

四. IDEA安装SonarLint插件

1.在Settings中的Plugins查找SonarLint插件并安装

在这里插入图片描述
2.安装完成后重启IDEA,选中需要检查的文件右键SonarLint-> Analyze with SonarLint

在这里插入图片描述
3.在下面的控制台左侧就是扫出来全部有问题的代码,双击每个错误代码块,右侧会出现修改提示。在真实项目开发的背景下,我们在本地安装SonarLint插件能够及时发现一些代码问题,减少后续代码的维护时间。一般来说红色Critical的issue是一定需要解决的,其他级别的issue需要看项目定的issue处理级别。

在这里插入图片描述

http://www.lryc.cn/news/157148.html

相关文章:

  • 【Unity3D】UI Toolkit元素
  • Task :app:compileDebugKotlin FAILED
  • Android——数据存储(一)(二十一)
  • 机器学习课后习题 ---数学基础回顾
  • CS420 课程笔记 P4 - 以16进制形态编辑游戏文件
  • 计算机毕设之Python的高校成绩分析(含文档+源码+部署)
  • 【Sentinel】核心API-Entry与Context
  • HashMap源码阅读解惑
  • 如何解决前端传递数据给后端时精度丢失问题
  • 使用Maven创建父子工程
  • Vue+elementUI 导出word打印
  • 数学建模-点评笔记 9月3日
  • 使用Spring来管理对象关系映射(ORM)
  • 【消息中间件】详解三大MQ:RabbitMQ、RocketMQ、Kafka
  • 算法:删除有序数组中的重复项---双指针[3]
  • AR产业变革中的“关键先生”和“关键力量”
  • 通过 Blob 对二进制流文件下载实现文件保存下载
  • 微信小程序使用lime-echart踩坑记录
  • Unity 编辑器资源导入处理函数 OnPostprocessTexture :深入解析与实用案例
  • stable diffusion实践操作-宽高设置以及高清修复
  • 利用微调的deberta-v3-large来预测情感分类
  • opencv旋转图像
  • 容器资料: Docker和Singularity
  • 如何确认linux的包管理器是yum还是apt,确认之后安装其他程序的时候就需要注意安装命令
  • 数据分享|R语言分析上海空气质量指数数据:kmean聚类、层次聚类、时间序列分析:arima模型、指数平滑法...
  • MySQL 8.0.34安装教程
  • 用通俗易懂的方式讲解大模型分布式训练并行技术:概述
  • NodeJS入门以及文件模块fs模块
  • springboot集成Elasticsearch7.16,使用https方式连接并忽略SSL证书
  • 【已解决】pycharm 突然每次点击都开新页面,关不掉怎么办?