【发布时间】:2015-05-19 21:53:59
【问题描述】:
我想要实现的是将 SonarQube 分析集成到构建过程中,以便每当运行 mvn clean install 时,都会使用 SonarQube 分析代码。我们希望将其用于本地分析以及基于 Jenkins 的构建。如果发现新问题,那么构建应该会失败(我们想为此使用构建中断插件)。这样,开发人员就会知道他的代码将引入新问题,并且必须修复它们才能使构建工作。
当我运行mvn sonar:sonar 时,分析需要 30 秒,还可以。
但是,当我尝试将sonar 目标绑定到 Maven 构建阶段时,就会出现问题。我将sonar 绑定到verify 阶段。现在构建需要 5 分钟,这太长了。大约需要 1 分钟。在没有 SonarQube 分析的情况下,构建本身需要 30 秒。
注意(可能有助于找出问题所在):运行构建的项目中有多个模块,我想这就是问题所在。看起来sonar:sonar 目标被多次执行,每个子模块一次,并且整个项目被多次分析(不仅仅是子模块)。所以,我们有 4 个子模块,在构建过程中生成了 5 次报告。
相反,我们只想分析整个项目一次,而不是 5 次。在为所有模块生成 cobertura 报告之后,在构建结束时运行此 1 分析也很重要。
那么,如何将 SonarQube 分析集成到构建中,以便它只分析我的多模块项目一次,最后在为所有子模块生成 cobertura 报告之后?
父 pom 中的 SonarQube 插件属性:
<!-- Sonar plugin properties -->
<sonar.jdbc.url>jdbc:url</sonar.jdbc.url>
<sonar.analysis.mode>preview</sonar.analysis.mode>
<sonar.issuesReport.html.enable>true</sonar.issuesReport.html.enable>
<sonar.issuesReport.console.enable>true</sonar.issuesReport.console.enable>
<sonar.host.url>sonar.host:9000</sonar.host.url>
<sonar.language>java</sonar.language>
<sonar.buildbreaker.skip>false</sonar.buildbreaker.skip>
<sonar.qualitygate>Sonar%20way%20with%20Findbugs</sonar.qualitygate>
<sonar.preview.includePlugins>buildbreaker</sonar.preview.includePlugins>
<sonar.exclusions>file:**/target/**</sonar.exclusions>
<branch>development</branch>
项目pom中的插件配置:
<!-- Run cobertura analysis during package phase -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Run sonar analysis (preview mode) during verify phase. Cobertura reports need to be generated already -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sonar</goal>
</goals>
</execution>
</executions>
</plugin>
【问题讨论】:
标签: java maven sonarqube cobertura static-code-analysis