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

Jacoco无法正确识别报告路径,修改Maven配置后报错求助

Jacoco代码覆盖率报告生成失败排查(路径不存在错误)

之前代码覆盖率报告生成正常,给maven-surefire-plugin添加forkCount、reuseForks及argLine参数后,报告功能失效。按照方案将argLine改为${argLine} -Xmx1024m后,出现java.io.FileNotFoundException错误,Jacoco尝试写入的jacoco.exec路径不存在(预期路径为target/jacoco.exec),已排查一天未解决,求解决思路。

旧配置

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <executions>
        <execution>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>none</exclude>
                </excludes>
                <includes>
                    <include>**/*IntegrationTest</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.8</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>generate-code-coverage-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

新配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <forkCount>1</forkCount>
        <reuseForks>true</reuseForks>
        <argLine>-Xmx1024m</argLine>
    </configuration>
    <executions>
        <execution>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>none</exclude>
                </excludes>
                <includes>
                    <include>**/*IntegrationTest</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.8</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>generate-code-coverage-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

报错信息

java.io.FileNotFoundException: /app2/azdosagents/agent3/_work/21029/s/CCReport43F6D5EF/jacoco.exec (No such file or directory)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
at org.jacoco.agent.rt.internal_035b120.output.FileOutput.openFile(FileOutput.java:67)
at org.jacoco.agent.rt.internal_035b120.output.FileOutput.writeExecutionData(FileOutput.java:53)
at org.jacoco.agent.rt.internal_035b120.Agent.shutdown(Agent.java:137)
at org.jacoco.agent.rt.internal_035b120.Agent$1.run(Agent.java:54)

解决思路

  • 显式指定Jacoco输出路径:在jacoco-maven-plugin的prepare-agent执行中配置destFile,确保输出路径基于项目构建目录,避免fork进程路径偏移:
    <execution>
        <goals>
            <goal>prepare-agent</goal>
        </goals>
        <configuration>
            <destFile>${project.build.directory}/jacoco.exec</destFile>
        </configuration>
    </execution>
    
  • 固定Surefire fork进程的工作目录:在surefire-plugin配置中添加workingDirectory参数,指定为项目target目录,保证fork进程和主Maven进程工作目录一致:
    <configuration>
        <forkCount>1</forkCount>
        <reuseForks>true</reuseForks>
        <argLine>${argLine} -Xmx1024m</argLine>
        <workingDirectory>${project.build.directory}</workingDirectory>
    </configuration>
    
  • 验证${argLine}参数传递:添加Maven debug日志(执行命令加-X),确认fork进程的JVM参数包含Jacoco的agent配置,确保${argLine}没有被覆盖或丢失。
  • 检查目录权限与存在性:手动创建报错中的路径/app2/azdosagents/agent3/_work/21029/s/CCReport43F6D5EF/,测试是否能正常写入;若无法创建,调整Jacoco输出路径到有权限的目录。
  • 调整fork复用模式:将reuseForks改为false,避免复用fork进程导致的环境变量或工作目录继承问题。
  • 检查插件版本兼容性:尝试升级maven-surefire-plugin到最新稳定版(如3.2.5),确保与Jacoco 0.8.8版本兼容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 01:42:22