从你提供的配置文件片段来看,存在多个问题,导致 Jacoco 无法正常工作:
一、 查看 Jacoco.exec 路径的方法:
1. 直接查看 target 目录bash
# 在项目根目录执行
ls -la target/jacoco.exec
# 或者查找所有位置
find . -name "jacoco.exec" -type f
2. 通过 Maven 命令查看
# 显示详细的构建信息
mvn help:effective-pom | grep -A5 -B5 jacoco
3. 修复配置后查看
正确的配置应该是:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<skip>false</skip>
<output>file</output>
<append>true</append>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
修复步骤:
1. 修正 pom.xml 中的错误配置
2. 清理并重新构建:
mvn clean compile test
3. 查看生成的文件:
# 默认路径
echo "文件路径:target/jacoco.exec"
# 检查文件是否存在
if [ -f "target/jacoco.exec" ]; then
echo "✅ Jacoco 执行文件已生成"
ls -lh target/jacoco.exec
else
echo "❌ Jacoco 执行文件未找到"
fi
4. 生成报告:
mvn jacoco:report
报告会生成在:`target/site/jacoco/index.html`
## 💡 建议配置(推荐):
使用更简单的标准配置:
<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>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
修复配置后,运行 `mvn clean verify` 应该就能正常生成 Jacoco 报告了。