You need to enable JavaScript to run this app. 如何通过Maven执行SonarQube代码分析且不将结果存入数据库?
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Maven执行SonarQube代码分析且不将结果存入数据库?

解决方案
  • 可通过SonarScanner的干跑(Dry Run)模式实现需求:该模式下仅在本地完成代码规则校验,不会向SonarQube服务器写入任何数据,既不会留存分析执行记录,也不会生成项目历史数据,完全适配临时代码分析的场景。
  • 配置方式分两种,可根据使用习惯选择:

1. 修改pom.xml永久生效(适合默认都跑临时分析的场景)

在你现有sonar profile的properties节点中追加干跑相关配置,修改后的完整配置如下:

<profile>
    <id>sonar</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <sonar.host.url>
          http://myserver:9000
        </sonar.host.url>
        <!-- 干跑模式配置,SonarQube 7.2及以上版本使用以下配置 -->
        <sonar.scanner.dryRun>true</sonar.scanner.dryRun>
        <!-- 可选:开启本地HTML分析报告,方便查看扫描出的问题 -->
        <sonar.html.report.enable>true</sonar.html.report.enable>
        <sonar.html.report.location>${project.build.directory}/sonar-report.html</sonar.html.report.location>
    </properties>
</profile>

如果使用的是SonarQube 7.2以下的旧版本,将<sonar.scanner.dryRun>true</sonar.scanner.dryRun>替换为<sonar.analysis.mode>preview</sonar.analysis.mode>,本地报告的属性替换为<sonar.issuesReport.html.enable>true</sonar.issuesReport.html.enable>和<sonar.issuesReport.html.location>${project.build.directory}/sonar-report.html</sonar.issuesReport.html.location>即可。

2. 运行时临时加参(适合偶尔跑临时分析、默认要跑正式分析的场景)

不需要修改pom文件,直接在Eclipse的Maven运行配置的Goals字段填写以下命令即可:

sonar:sonar -Dsonar.scanner.dryRun=true

旧版本SonarQube对应的临时命令为:

sonar:sonar -Dsonar.analysis.mode=preview
  • 干跑模式下仅会从SonarQube服务器拉取规则配置、质量阈值等只读信息,所有扫描结果都会输出到本地控制台或指定的HTML报告中,不会在服务器数据库留下任何执行记录。
  • 针对已经留存的无用分析执行记录,可直接在SonarQube服务端的项目管理页面,选择对应项目后进入「分析」标签页,批量删除不需要的历史执行记录即可。
  • 如果需要提交正式版本的分析,只需移除干跑相关配置或去掉运行命令中的干跑参数,再执行扫描即可正常将结果存入服务器。

内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.17 23:24:02