码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
    
    • 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处理级别。

    在这里插入图片描述

  • 相关阅读:
    申克SCHENCK动平衡机显示器维修CAB700系统控制面板
    云架构的一些核心概念
    thinkphp5.1整合波场trc20
    【0109】PostgreSQL配置WAL Archive
    自定义Dynamics 365实施和发布业务解决方案 - 8. Dynamics 365门户简介
    R语言统计分析:置换检验
    使用WordPress搭建一个专属自己的博客
    BUG:阿里巴巴图标库引入链接后,icon有时候会不显示的话svg下载到本地使用
    将YOLOv8模型从PyTorch的.pt格式转换为TensorRT的.engine格式
    论如何直接用EF Core实现创建更新时间、用户审计,自动化乐观并发、软删除和树形查询(中)
  • 原文地址:https://blog.csdn.net/weixin_44009447/article/details/132646864
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号