JaCoCo 报告格式

JaCoCo Report Format

对于我的小型 Java/Maven 应用程序,我在 POM.xml 中使用 JaCoCo,如下所示:

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.7.6.201602180812</version>
      <configuration>
        <destFile>${basedir}/target/coverage-reports/jacoco.exec</destFile>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>prepare-agent</goal>
           </goals>
        </execution>
        <execution>
          <id>report</id>
          <phase>package</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

只要我不使用 <destFile> 参数,target/site/jacoco 文件夹中的默认报告就会以 XML、CSV 和 HTML 格式正确生成。但是当我使用 <destFile> 元素来更改生成报告的默认文件夹时,只会生成 jacoco.exec 文件,而不会生成其他任何文件。如何更改报告文件夹以及获取 csv、xml 和 html 格式的报告?

加上destFile parameter, you changed the location where the prepare-agent goal will write the execution data file. By default, this is ${project.build.directory}/jacoco.exec, meaning (still by default) target/jacoco.exec. However, the report goal expects the path to the execution file to be passed in the dataFile参数,当然默认是${project.build.directory}/jacoco.exec,这样他们就同步了。因此,如果要更改此执行文件的路径,则需要匹配这两个参数。为了不重复路径,您可以使用 Maven 属性 来做到这一点:

<properties>
  <jacoco.execution.file>${project.build.directory}/coverage-reports/jacoco.exec</jacoco.execution.file>
</properties>
<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.7.7.201606060606</version>
      <configuration>
        <destFile>${jacoco.execution.file}</destFile>
        <dataFile>${jacoco.execution.file}</dataFile>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>report</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

请注意,这不会更改 JaCoCo 报告的输出目录;这只是更改了执行文件的路径。为此,您可以使用 outputDirectory 参数:

Output directory for the reports. Note that this parameter is only relevant if the goal is run from the command line or from the default build lifecycle. If the goal is run indirectly as part of a site generation, the output directory configured in the Maven Site Plugin is used instead.

并添加以下配置元素:

<configuration>
  <!-- rest of your JaCoCo configuration -->
  <outputDirectory>${project.build.directory}/coverage-reports/jacoco</outputDirectory>
</configuration>

这将确保所有 HTML、XML 和 CSV 报告都在 target/coverage-reports/jacoco 下生成。请注意,启动 mvn site 时,此配置不会用作 Maven 站点生成的一部分。在站点生成期间,您需要配置 outputDirectory of the maven-site-plugin