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



    环境说明以及准备

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

    更多环境配置请参考: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
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    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>
      	
    	<pluginGroup>org.sonarsource.scanner.mavenpluginGroup>
      pluginGroups>
    
    • 1
    • 2
    • 3
    • 4
    	<profiles>
    		<profile>
    			<id>sonarid>
    			<activation>
    				<activeByDefault>trueactiveByDefault>
    			activation>
    			<properties>
    				
    				<sonar.host.url>
    				  http://localhost:9000
    				sonar.host.url>
    			properties>
    		profile>
    	 profiles>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

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

            <dependency>
                <groupId>org.jacocogroupId>
                <artifactId>jacoco-maven-pluginartifactId>
                <version>0.8.1version>
                <type>pomtype>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
                <plugin>
                    <groupId>org.jacocogroupId>
                    <artifactId>jacoco-maven-pluginartifactId>
                    <version>0.8.8version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agentgoal>
                            goals>
                        execution>
                        <execution>
                            <id>reportid>
                            <phase>testphase>
                            <goals>
                                <goal>reportgoal>
                            goals>
                        execution>
                    executions>
                plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

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

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

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

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

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

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

            <sonar.exclusions>
                
                src/main/java/com/example/redisdemo/config/**,
                
                src/main/java/com/example/redisdemo/config/*Config.*
            sonar.exclusions>
            <sonar.coverage.exclusions>**/generated/**sonar.coverage.exclusions>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    四. IDEA安装SonarLint插件

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

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

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

    在这里插入图片描述

  • 相关阅读:
    什么是跨域?跨域问题如何解决?看完豁然开朗。
    uniapp踩坑之项目:隐藏显示密码功能
    AKK菌——下一代有益菌
    Matlab:合并来自各自变量的日期和时间
    java基础1
    解决Zotero不显示标签的问题
    JavaScript从入门到精通 |循环
    C语言之初阶总结篇
    51.Sentinel微服务保护
    从SpringBoot到SpringCloudAlibaba简明教程(一):初识SpringBoot及其基础项目构建
  • 原文地址:https://blog.csdn.net/weixin_44009447/article/details/132646864