对于我的小型 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
参数,您更改了 prepare-agent
所在的位置目标将写入执行数据文件。默认情况下,这是 ${project.build.directory}/jacoco.exec
,意思是(仍然默认)target/jacoco.exec
。然而,report
目标期望执行文件的路径在 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
maven-site-plugin
的相反。
关于java - JaCoCo报告格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330470/