Windows配置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处理级别。

在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Keson Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值