【问题标题】:Merging sonar metrics across a maven multi-module project在 Maven 多模块项目中合并声纳指标
【发布时间】:2015-09-28 05:39:06
【问题描述】:

我继承了一个奇怪的 Maven 多模块项目。标准业务类和单元测试位于一个名为“代码”的模块中,我们为该模块提供了很好的简单工作声纳指标。第二个模块包含针对部署的 glassfish 服务器运行的各种集成样式测试,这些测试在 integ-test 模块中作为单元测试运行。

pom.xml // root level,
    code // business code and unit tests :-)
        moduleA
        moduleB
    integ-test // ;-(
        moduleA
        moduleB

我知道最简单的选择是将“integ-test”类移动到“代码”模块中,并将它们重构为真正的集成测试,但目前这不是一个选择。

我找到了一种方法来记录“integ-tests”模块的代码覆盖率,方法是使用 jacoco:dump 目标将 jacococ.exec 下载到 integ-test 模块。

我的 maven pom 结构现在看起来像这样

 pom.xml // defined jacoco verify -> report goal
     code // business code and unit test
         moduleA
            target/jacoco.exec - unit test coverage
            pom.xml
     pom.xml - defines standard jacoco prepare-agent goal
     integ-test //
         moduleA
            target/jacoco.exec - really integration test coverage
            pom.xml
     pom.xml- defines jacoco dump and merge

我目前的方法是合并“integ-test”模块中的所有 jacococ.exec 文件,并使用“destFile”值将这些详细信息保存为“code”模块作为 jacoco-it.exec 文件。然后,标准的 sonar:sonar 目标应该选择该文件并将其用作集成测试覆盖率。

 pom.xml
     integ-test
         moduleA
            target/jacoco.exec - really integration test coverage modA
         moduleB
            target/jacoco.exec - really integration test coverage modB
     code
         moduleA
         moduleB
     target/jacoco-it.exec - This should be the merged content of the two files above
     pom.xml

我的根级 pom.xml 定义了这个 jacoco-maven-plugin 配置

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <configuration>
        <append>true</append>
        <includes>
            <include>com/a/b/c/**</include>
        </includes>
    </configuration>
    <executions>

        <execution>
            <id>jacoco-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>

        <execution>
            <id>jacoco-dump</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>dump</goal>
            </goals>
        </execution>

        <execution>
            <id>jacoco-merge</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>merge</goal>
            </goals>
            <configuration>
                <fileSets>
                    <fileSet implementation="org.apache.maven.shared.model.fileset.FileSet">
                        <directory>${project.basedir}/integ-test</directory>
                        <includes>
                            <include>*.exec</include>
                        </includes>
                    </fileSet>
                </fileSets>
                <destFile>${sonar.jacoco.itReportPath}</destFile>
            </configuration>
        </execution>

        <execution>
            <id>jacoco-site</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>

我已将 jacoco:merge 目标链接到 maven 集成后测试阶段,以确保在运行测试后进行合并,但“jacoco-it.exec”文件始终在“integ-”中创建测试”文件夹。

有什么建议吗?

【问题讨论】:

  • 如果您正在寻找将多模块 Maven 项目指标合并到 Sonar 中,那么您正在做正确的事情,您可能需要进行一些调整,但如果您正在寻找跨项目/团队合并/获取组合的 Sonar 指标/按项目类型/按团队/按开发经理等,然后尝试 SonarQube 中的 Views Portfolio Management 插件。这是一个商业项目,但将每个分析的项目视为一个组件,然后您可以创建自定义视图、度量、过滤器以在 Sonar 本身中创建组合报告。

标签: maven sonarqube jacoco jacoco-maven-plugin


【解决方案1】:

如果指向主 pom 的目标目录中的 destFile 呢?只需定义

<configuration>
    <append>true</append>
    <destFile>${project.basedir}/../target/jacoco-it.exec</destFile>
    <propertyName>jacoco.integrationtest.arguments</propertyName>
</configuration>

在子项目中。所有覆盖数据都将附加在 jacoco-it.exec 文件中。 此文件由 sonarqube 拾取,您将看到覆盖范围为 IT 覆盖范围。 您也可以使用 jacoco.exec 作为文件名。

【讨论】:

    猜你喜欢
    • 2013-03-21
    • 2021-02-23
    • 1970-01-01
    • 2020-12-11
    • 2017-05-09
    • 2014-10-03
    • 2018-04-23
    • 2016-09-19
    • 2018-06-04
    相关资源
    最近更新 更多