我已经将
jacoco-maven-plugin
集成到我的项目中,基于这个优秀的指南:http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-使用 jacoco-maven-plugin 进行测试/
Jacoco 插件运行良好。但是,
maven-site-plugin
网站中不包含 Jacoco 报告。更具体地说:“项目报告”部分没有列出 Jacoco 报告。 Jacoco 报告本身可在 target/site/jacoco-ut
和 target/site/jacoco-it
目录中找到。
这就是我所做的(到目前为止还没有成功)。
首先,将
jacoco-maven-plugin
作为插件包含在我的 build
的 pom.xml
部分中,如上面引用的指南中所述。我正在使用 Jacoco 版本 0.6.4.201312101107。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<executions>
<!-- scissors... -->
<!-- report goal is bound to the pre-site phase -->
</executions>
</plugin>
其次,将
jacoco-maven-plugin
包含在我的report
的pom.xml
部分:没有成功。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
</plugin>
第三,尝试在
reportsets
部分中的jacoco-maven-plugin
中添加report
部分:没有成功。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
任何人都可以帮我制作
maven-site-plugin
引用 Jacoco 在网站“项目报告”部分生成的覆盖率报告吗?
我发现 jacoco 非常棘手,下面的配置对我有用,我把它从博客中的零碎组合起来 在属性中
<jacoco.reportPath>${main.basedir}/target/jacoco.exec</jacoco.reportPath>
<jacoco.itReportPath>${main.basedir}/target/jacoco-it.exec</jacoco.itReportPath>
构建内部
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.4.201312101107</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<propertyName>utCoverageAgent</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${sonar.jacoco.reportPath}</dataFile>
</configuration>
</execution>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>agent</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.itReportPath}</destFile>
<propertyName>itCoverageAgent</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${sonar.jacoco.itReportPath}</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<!-- Reporting plugins -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>${plugin.site.version}</version>
<configuration>
<attach>true</attach>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-changelog-plugin</artifactId>
<version>2.2</version>
<configuration>
<type>range</type>
<range>1</range>
<!--<displayFileDetailUrl>${project.scm.url}/tree/master/%FILE%</displayFileDetailUrl>-->
<headingDateFormat>MM-dd-yyyy</headingDateFormat>
<outputEncoding>${project.reporting.outputEncoding}</outputEncoding>
</configuration>
<reports>
<report>changelog</report>
</reports>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.7</version>
<configuration>
<dependencyDetailsEnabled>false</dependencyDetailsEnabled>
<dependencyLocationsEnabled>false</dependencyLocationsEnabled>
</configuration>
<!-- simpler configuration without reportSets available for usual cases -->
<!-- distribution-management, index, dependencies, help, issue-tracking, plugins, cim,
license, dependency-management, mailing-list, project-team, dependency-convergence,
scm, plugin-management, modules, summary -->
<reports>
<report>index</report>
<report>dependencies</report>
<report>issue-tracking</report>
<report>scm</report>
<report>summary</report>
</reports>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.4</version>
<configuration>
<aggregate>true</aggregate>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<reports>
<report>javadoc</report>
<report>aggregate</report>
</reports>
<configuration>
<failOnError>false</failOnError>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.4.201312101107</version>
</plugin>
</reportPlugins>
</configuration>
<executions>
<execution>
<id>attach-descriptor</id>
<goals>
<goal>attach-descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
希望有帮助!
我所做的是将两个执行文件(
jacoco-ut.exec
和jacoco-it.exec
)合并到一个名为jacoco.exec
的文件中,然后将该文件设置为插件的dataFile
。
首先,让我们定义这些属性:
<jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
<jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>
<jacoco.execution.data.file>${project.build.directory}/coverage-reports/jacoco.exec</jacoco.execution.data.file>
然后,连同生成报告的配置一起,包含此代码片段以创建合并文件:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-plugin.version}</version>
<executions>
...
<execution>
<id>merge</id>
<phase>verify</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${project.build.directory}/coverage-reports</directory>
<includes>
<include>*.exec</include>
</includes>
</fileSet>
</fileSets>
<destFile>${jacoco.execution.data.file}</destFile>
</configuration>
</execution>
<execution>
<id>report-merge</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${jacoco.execution.data.file}</dataFile>
</configuration>
</execution>
</executions>
</plugin>
最后,在
reporting
部分配置插件以从该合并文件生成报告:
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<dataFile>${jacoco.execution.data.file}</dataFile>
</configuration>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
...
</plugins>
</reporting>
您已经非常接近解决方案了,但是
reportSets
不是 build -> plugins -> plugin
的属性,而是 reporting -> plugins -> plugin
的属性。
因此,为了在生成的 Maven 站点中包含 JaCoCo 报告,您将指定 JaCoCo Maven 插件两次:
build -> plugins -> plugin
下启用 Java 代理(无需在此处添加 report
目标)。reporting -> plugins -> plugin
指定 reportSet
。最简单的片段是:
<!-- goes directly under tag "project" -->
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
虽然也可以通过自定义
maven-site-plugin
的输出(例如通过 site.xml
)手动添加 JaCoCo 报告的链接,但这不是最简单的方式,也不是预期的方式。 JaCoCo 的 ReportMojo
实现了 MavenMultiPageReport
,这允许 maven-site-plugin
将其检测为报告插件。然而,仅通过阅读文档,它的工作原理还远不清楚。
在
report
下指定 build -> plugins
目标时,maven-site-plugin
不知道 JaCoCo 报告是否存在。仅当在 reporting -> plugins
下给出时,它被称为 report
而不是 goal
,尽管它实际上是同一件事。此处指定时,(报告)插件将通过 maven-site-plugin
间接启动(而不是直接通过 Maven 构建)。然而,要使报告包含必要的信息,JaCoCo 输出必须在报告之前存在,并且只有当目标 prepare-agent
(或用于集成测试的 prepare-agent-integration
)已添加到 jacoco-maven-plugin
下的
build -> plugins
时才会出现这种情况。。唷! 😅
JaCoCo 有三个报告(可以双向触发):
report
对于单个 Maven 项目report-aggregate
用于多模块 Maven 项目report-integration
用于集成测试所有这些都会被
reportSet
自动检测到(并且在省略 maven-site-plugin
的情况下也会执行):
[INFO] --- site:3.12.1:site (default-site) @ java-playground ---
[INFO] configuring report plugin org.jacoco:jacoco-maven-plugin:0.8.11
[INFO] 3 reports detected for jacoco-maven-plugin:0.8.11: report, report-aggregate, report-integration
长话短说,一个简单的单个 Maven 项目的完整 pom.xml 可能如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>java-playground</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<executions>
<execution>
<id>prepare</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- no explicit report generation here!
(commented for illustration purposes)
<execution>
<id>report</id>
<phase>pre-site</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
-->
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
<!-- whatever you use for your tests -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
另请参阅: