You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置使mvn clean install自动触发sonar:sonar命令?

如何让mvn clean install自动触发Sonar扫描?

当然可以实现这个需求!你只需要把Sonar Maven插件绑定到Maven构建生命周期的某个阶段,这样执行mvn clean install时就会自动触发sonar:sonar目标了。

通常我们会把它绑定到verify阶段——这个阶段刚好在测试执行完成之后、install阶段之前,能确保代码质量扫描是基于通过测试的代码来进行的。

下面是调整后的完整插件配置:

<plugin>
    <groupId>org.sonarsource.scanner.maven</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
    <configuration>
        <skip>false</skip>
    </configuration>
    <executions>
        <execution>
            <id>sonar-scan</id>
            <phase>verify</phase>
            <goals>
                <goal>sonar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

几点补充说明:

  • 如果你想绑定到其他生命周期阶段(比如install阶段本身),只需要把<phase>标签里的verify改成对应的阶段名称就行。
  • 如果某次构建想临时跳过Sonar扫描,可以在命令行加上参数:mvn clean install -Dsonar.skip=true

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:03:29