You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单模块Maven项目JaCoCo报告未在Maven Site展示,却生成空聚合报告

单模块Maven项目JaCoCo报告未在Site页面显示链接,反而出现空的Jacoco Aggregate链接

项目背景

  • 基于Java 11的单模块Jar库项目,继承自通用父pom,父子项目均无子模块
  • 父pom包含<pluginManagement>节点,父子pom均配置JaCoCo,子pom配置覆盖父pom
  • 父pom的<reporting>节点无JaCoCo配置

需求

在Maven Site页面的「项目报告」中生成JaCoCo覆盖率报告的入口链接

问题现象

  • 执行mvn clean test site后,完整的单元测试JaCoCo报告已生成在target/site/jacoco目录,但Site页面未显示该报告的链接
  • Site页面反而始终显示「Jacoco Aggregate」链接,点击后为空报告,target/site/jacoco-aggregate仅包含少量基础文件(含index.html)
  • JaCoCo数据文件jacoco.exec生成在正确路径target/code-coverage/data/

已尝试操作

  • 查阅官方文档,确认单模块项目中JaCoCo报告应自动加入Site页面,无需额外配置
  • 将父pom中JaCoCo插件从<build><pluginManagement>移至<build>节点,避免被误识别为多模块项目
  • 注释插件的自定义配置,仅保留prepare-agent和report核心目标,问题依旧

控制台警告(正常项目也会出现)

[INFO] Skipping JaCoCo execution due to missing execution data file.

父pom配置

<build>
  <pluginManagement>
    ...
  </pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.11</version>
      <executions>
        <execution>
          <id>default-prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
          <configuration>
            <skip>${jacoco.skip}</skip>
            <propertyName>argline.jacoco</propertyName>
            <destFile>${project.build.directory}/code-coverage/data/jacoco.exec</destFile>
            <includes>
              <include>${jacoco.include}</include>
            </includes>
            <excludes>
              <exclude>${jacoco.exclude}</exclude>
            </excludes>
          </configuration>
        </execution>
        <execution>
          <id>default-report</id>
          <goals>
            <goal>report</goal>
          </goals>
          <phase>test</phase>
          <configuration>
            <skip>${jacoco.skip}</skip>
            <dataFile>${project.build.directory}/code-coverage/data/jacoco.exec</dataFile>
            <outputDirectory>${project.build.directory}/code-coverage/report</outputDirectory>
            <includes>
              <include>${jacoco.include}</include>
            </includes>
              <excludes>
                <exclude>${jacoco.exclude}</exclude>
              </excludes>
          </configuration>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>

项目pom配置

<reporting>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.11</version>
      <configuration>
        <outputDirectory>${project.build.directory}/site/jacoco</outputDirectory>
      </configuration>
    </plugin>
  ...
  </plugins>
</reporting>
<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>${version.jacoco}</version>
      <executions>
        <!-- first execution : for preparing JaCoCo runtime agent -->
        <execution>
          <id>default-prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <!-- second execution : for creating code coverage reports -->
        <execution>
          <id>default-report</id>
          <goals>
            <goal>report</goal>
          </goals>
          <phase>test</phase>
          <configuration>
            <skip>${jacoco.skip}</skip>
            <dataFile>${project.build.directory}/code-coverage/data/jacoco.exec</dataFile>
            <outputDirectory>${project.build.outputDirectory}/site/jacoco</outputDirectory>
            <includes>
              <include>${jacoco.include}</include>
            </includes>
            <excludes>
              <exclude>${jacoco.exclude}</exclude>
            </excludes>
          </configuration>
        </execution>
      </executions>
    </plugin>
    ...
</build>

解决方案

问题根源

  1. 父pom中report目标的输出目录未指向Site报告的标准路径,导致Site插件无法识别报告位置
  2. 子pom重复配置JaCoCo插件,且<reporting>节点未明确指定报告目标,触发了JaCoCo的聚合报告逻辑(针对多模块设计)

修复步骤

1. 调整父pom的JaCoCo配置

将report目标的输出目录改为Site报告的标准路径,同时移除phase绑定,让其自动关联Site生命周期:

<build>
  <pluginManagement>
    ...
  </pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.11</version>
      <executions>
        <execution>
          <id>default-prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
          <configuration>
            <skip>${jacoco.skip}</skip>
            <propertyName>argline.jacoco</propertyName>
            <destFile>${project.build.directory}/code-coverage/data/jacoco.exec</destFile>
            <includes>
              <include>${jacoco.include}</include>
            </includes>
            <excludes>
              <exclude>${jacoco.exclude}</exclude>
            </excludes>
          </configuration>
        </execution>
        <execution>
          <id>default-report</id>
          <goals>
            <goal>report</goal>
          </goals>
          <!-- 移除phase配置,让report目标自动绑定到site生命周期 -->
          <configuration>
            <skip>${jacoco.skip}</skip>
            <dataFile>${project.build.directory}/code-coverage/data/jacoco.exec</dataFile>
            <!-- 修改为Site报告的标准目录 -->
            <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
            <includes>
              <include>${jacoco.include}</include>
            </includes>
            <excludes>
              <exclude>${jacoco.exclude}</exclude>
            </excludes>
          </configuration>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>

2. 简化子pom配置

移除<build>中重复的JaCoCo插件配置(直接继承父pom),并在<reporting>中明确指定报告目标,避免触发聚合报告:

<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>
<!-- 移除build中的JaCoCo插件重复配置 -->

3. 验证结果

执行mvn clean test site,此时Site页面的「项目报告」中会显示「JaCoCo」链接,指向target/site/jacoco下的完整覆盖率报告,「Jacoco Aggregate」链接会自动消失。

额外说明

  • 控制台的Skipping JaCoCo execution due to missing execution data file警告是Site生命周期中部分JaCoCo目标的正常检查逻辑,只要jacoco.exec生成正常且报告能显示,该警告可忽略。

内容的提问来源于stack exchange,提问作者dan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 20:57:03