Jacoco 聚合报告正在生成 Exec 文件而不是 XML

Vik*_*rde 7 java maven jacoco-maven-plugin

我们有一个多模块 Maven 项目。我们用的是java8。

\n
-Parent\n-- Child 1\n-- Child 2\n-- Coverage-Report\n
Run Code Online (Sandbox Code Playgroud)\n

所有项目都有单元测试用例,运行良好,覆盖率超过 85%。父 pom.xml 文件具有 jacoco-profile 和 jacoco-maven-plugin 插件,如下所示

\n
      <plugin>\n            <groupId>org.jacoco</groupId>\n            <artifactId>jacoco-maven-plugin</artifactId>\n            <version>0.8.6</version>\n            <configuration>\n                <destFile>${sonar.coverage.jacoco.xmlReportPaths}</destFile>\n                <append>true</append>\n            </configuration>\n            <executions>\n                <execution>\n                    <id>prepare-agent</id>\n                    <goals>\n                        <goal>prepare-agent</goal>\n                    </goals>\n                </execution>\n                <execution>\n                    <id>report</id>\n                    <phase>verify</phase>\n                    <goals>\n                        <goal>report</goal>\n                    </goals>\n                </execution>\n            </executions>\n        </plugin>\n
Run Code Online (Sandbox Code Playgroud)\n

在Coverage-Report模块中,只有pom.xml用于聚合报告。它的配置文件为 jacoco 如下

\n
    <profile>\n        <id>jacoco</id>\n        <activation/>\n        <build>\n            <plugins>\n                <plugin>\n                    <groupId>org.jacoco</groupId>\n                    <artifactId>jacoco-maven-plugin</artifactId>\n                    <executions>\n                        <execution>\n                            <id>report-aggregate</id>\n                            <phase>verify</phase>\n                            <goals>\n                                <goal>report-aggregate</goal>\n                            </goals>\n                        </execution>\n                    </executions>\n                </plugin>\n            </plugins>\n        </build>\n    </profile>\n
Run Code Online (Sandbox Code Playgroud)\n

要生成报告,我们使用以下命令

\n
mvn clean verify -Pjacoco\n
Run Code Online (Sandbox Code Playgroud)\n

我们确实看到父 pom.xml 中提到的文件正在生成

\n
<sonar.coverage.jacoco.xmlReportPaths>${maven.multiModuleProjectDirectory}/ocapi-promotion-service-coverage-report/target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>\n    \n
Run Code Online (Sandbox Code Playgroud)\n

然而,这个文件 jacoco.xml 不是正确的 XML。它有一些编码字符,如下所示。(该文件中的几行)

\n
\xef\xbf\xbd\xef\xbf\xbdca6191af5ae0-1a24a201}-\xef\xbf\xbdSy}-\xc6\x8c|pW\xef\xbf\xbd\\\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd;org/springframework/context/annotation/BeanAnnotationHelper\xef\xbf\xbdw~\xef\xbf\xbdj\xef\xbf\xbdzI\xef\xbf\xbdZMorg/springframework/boot/autoconfigure/condition/FilteringSpringBootCondition\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbde\xd6\xa2+org/sp\n
Run Code Online (Sandbox Code Playgroud)\n

在詹金斯上我们看到以下错误

\n
Caused by: java.nio.charset.MalformedInputException: Input length = 1\n
Run Code Online (Sandbox Code Playgroud)\n

我关注了声纳源社区https://community.sonarsource.com/t/coverage-test-data-importing-jacoco-coverage-report-in-xml-format/12151的一篇文章,其中提供了一些步骤。但我无法生成 XML 文件。请帮忙。

\n

小智 0

您在父级中设置的参数<destFile>不是报告 XML 文件,而是代理的执行文件,它始终是二进制文件。

为了让它做你想做的事:

  1. 删除<destFile>配置。
  2. 确保您的报告模块的 pom 具有其他模块作为依赖项。
  3. 跑步mvn clean verify -Pjacoco

然后<report-module-dir>/target/site/jacoco-aggregate/jacoco.xml将包含您的聚合 XML 报告。