【问题标题】:Integrating JaCoCo with SONAR for unit and integration test coverage将 JaCoCo 与 SONAR 集成以实现单元和集成测试覆盖率
【发布时间】:2025-12-03 01:05:01
【问题描述】:

有没有人尝试配置 JaCoCo 以将单元和集成测试的覆盖率转储到 2 个不同的文件中,以便 SONAR 使用它们,使用 ANT 构建?

【问题讨论】:

    标签: java ant sonarqube jacoco


    【解决方案1】:

    这是一个可行的解决方案,可为单元测试和集成测试生成报告。此解决方案使用append 策略。

    请注意,为了在apply 策略上正常工作,阶段应按顺序执行(如果mvn testmvn verify -DskipUnitTests 将并行执行,则可能无法正常工作)。

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco.version}</version>
    
        <configuration>
            <skip>${skipTests}</skip>
    
            <!-- This line is very important, because allows to append the results to the `jacoco_aggregation.exec` report. -->
            <append>true</append>
    
            <!-- DestFile is the file defined, where the `prepare-agent` will pe publishing the result of analysis,
                 for the defined `phase`. In our case is executed initially and to the `pre-integration-test` phase. -->
            <destFile>${project.build.directory}/coverage-reports/jacoco_aggregation.exec</destFile>
    
            <!-- DataFile is the path from where the `report` goal will be reading the report
                 in order to create the `outputDirectory` .xml file. -->
            <dataFile>${project.build.directory}/coverage-reports/jacoco_aggregation.exec</dataFile>
    
            <!-- To the goal `report`, will be reading the report from `dataFile` path and will be publishing the result,
                 to the `outputDirectory` path as an .xml file.
                 In our case, the report is generated for two times:
                     - first time after the `test` phase
                     - second time after the `post-integration-test` phase -->
            <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
        </configuration>
    
        <executions>
            <!-- Prepares the property pointing to the JaCoCo runtime agent which
                 is passed as VM argument when Maven the Surefire plugin is executed. -->
            <execution>
                <id>pre-unit-test</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
                <configuration>
                    <!-- Sets the name of the property containing the settings for JaCoCo runtime agent. -->
                    <propertyName>surefireArgLine</propertyName>
                </configuration>
            </execution>
            
            <!-- Ensures that the code coverage report `jacoco.xml` is generated once the unit tests were executed.
                 Executes the `report` goal after the `post-integration-test` phase. -->
            <execution>
                <id>post-unit-test</id>
                <phase>test</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
    
            <!-- Prepares the property pointing to the JaCoCo runtime agent which
                 is passed as VM argument when Maven the Failsafe plugin is executed. -->
            <execution>
                <id>pre-integration-test</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
                <configuration>
                    <!-- Sets the name of the property containing the settings for JaCoCo runtime agent. -->
                    <propertyName>failsafeArgLine</propertyName>
                </configuration>
            </execution>
            
            <!-- Ensures that the code coverage report `jacoco.xml` is generated once the integration tests were executed.
                 Executes the `report` goal after the `post-integration-test` phase. -->
            <execution>
                <id>post-integration-test</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <!-- Sets the VM argument line used when unit tests are run. -->
            <argLine>${surefireArgLine}</argLine>
        </configuration>
    </plugin>
    
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <configuration>
            <!-- Sets the VM argument line used when integration tests are run. -->
            <argLine>${failsafeArgLine}</argLine>
        </configuration>
    </plugin>
    

    生成报告后,您可以执行声纳命令来应用报告。

    mvn sonar:sonar -Dsonar.coverage.jacoco.xmlReportPaths="target/site/jacoco/jacoco.xml"
    

    【讨论】: