我正在使用 Maven 3.0.3、JUnit 4.8.1 和 Jacoco 0.6.3.201306030806,并且正在尝试创建测试覆盖率报告。
我有一个只有单元测试的项目,但我无法运行报告,运行时我反复收到错误:Skipping JaCoCo execution due to missing execution data file
:
mvn clean install -P test-coverage
这是我的 pom 的配置方式:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<reuseForks>true</reuseForks>
<argLine>-Xmx2048m</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<reuseForks>true</reuseForks>
<argLine>-Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
...
<profile>
<id>test-coverage</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
<configuration>
<destfile>${basedir}/target/coverage-reports/jacoco-unit.exec</destfile>
<datafile>${basedir}/target/coverage-reports/jacoco-unit.exec</datafile>
</configuration>
<executions>
<execution>
<id>prepare-unit-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>prepare-integration-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<propertyName>itCoverageAgent</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
我所有的测试都成功运行。以下是 Maven 的一些输出:
[INFO] --- jacoco-maven-plugin:0.6.2.201302030002:prepare-agent (prepare-unit-tests) @ myproject ---
[INFO] argLine set to -javaagent:/Users/davea/.m2/repository/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=/Users/davea/Dropbox/workspace/myproject/target/jacoco.exec
[INFO]
...
Tests run: 14, Failures: 0, Errors: 0, Skipped: 0
[INFO]
...
[INFO]
[INFO] --- jacoco-maven-plugin:0.6.2.201302030002:prepare-agent (prepare-integration-tests) @ myproject ---
[INFO] itCoverageAgent set to -javaagent:/Users/davea/.m2/repository/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=/Users/davea/Dropbox/workspace/myproject/target/jacoco.exec
[INFO]
[INFO] --- maven-failsafe-plugin:2.14.1:integration-test (default) @ myproject ---
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-failsafe-plugin:2.14.1:verify (default) @ myproject ---
[INFO] Failsafe report directory: /Users/davea/Dropbox/workspace/myproject/target/failsafe-reports
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- jacoco-maven-plugin:0.6.2.201302030002:report (jacoco-site) @ myproject ---
[INFO] Skipping JaCoCo execution due to missing execution data file
[INFO]
任何想法我缺少什么配置?
destFile
并让它写入默认的 target/jacoco.exec 文件。
jacoco-maven-plugin:0.7.10-SNAPSHOT
从 jacoco:prepare-agent 说:
在 maven-surefire-plugin 的情况下执行此操作的方法之一是使用语法进行后期属性评估:
请注意添加到 -your -extra -arguments
的 @{argLine}
。
感谢 Slava Semushin 注意到这一变化并感谢 reporting in the comment。
jacoco-maven-plugin:0.7.2-SNAPSHOT
在 jacoco:prepare-agent 之后说:
[org.jacoco:jacoco-maven-plugin:0.7.2-SNAPSHOT:prepare-agent] 准备一个指向 JaCoCo 运行时代理的属性,该属性可以作为 VM 参数传递给被测应用程序。默认情况下,根据项目打包类型设置具有以下名称的属性:tycho.testArgLine 用于打包类型 eclipse-test-plugin 和 argLine 否则。请注意,这些属性不能被测试配置覆盖,否则无法附加 JaCoCo 代理。如果您需要自定义参数,请附加它们。例如:
您应该更改 maven-surefire-plugin
插件配置中的以下行(注意 <argLine>
中的 ${argLine}
):
<argLine>-Xmx2048m</argLine>
至
<argLine>${argLine} -Xmx2048m</argLine>
还对其他插件 maven-failsafe-plugin
进行必要的更改并替换以下内容(再次注意 ${argLine}
):
<argLine>-Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
至
<argLine>${argLine} -Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
我遇到了一个返回相同错误的不同问题。
Skipping JaCoCo execution due to missing execution data /target/jacoco.exec
事实是,返回此错误的原因有很多。我们在 Stack Overflow 上试验了不同的解决方案,但发现这个 resource 是最好的。它消除了 Jacoco 可能返回相同错误的许多不同的潜在原因。
对我们来说,解决方案是在配置中添加一个准备代理。
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
我想大多数用户会因为不同的原因体验它,所以看看前面提到的资源吧!
*Tests.java
时,我的 surefire 插件只在寻找名为 **/*Test.java
的文件
*Test.java
类和一个用 @Test
注释的测试方法,这样 Jacoco 就可以做点什么。 2. 在我的项目的 Travis 环境变量中,SONART_TOKEN=*****
中有错字,应该命名为 SONAR_TOKEN=*****
。请参阅 Travis 文档 here,搜索 or define SONAR_TOKEN in your Repository Settings
。在我修复它之后,构建成功运行。 You may view my gitbhub project in question。
由于项目中缺少测试,还可能出现“由于缺少执行数据文件而跳过 JaCoCo 执行”错误。例如,当您启动新项目并且根本没有 *Test.java 文件时。
可能存在一些其他 argline 设置或 pom 中的插件可能会覆盖 jacoco 执行顺序设置的情况。
argLine 设置为 -javaagent:/Users/davea/.m2/repository/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=/Users/davea/Dropbox/workspace/myproject/target/jacoco.exec
例子之一
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Dnet.sf.ehcache.disabled=true</argLine>
</configuration>
</plugin>
从这些插件中去掉 argLine 后,jacoco 开始正常工作了。
我知道这个问题已经很老了,但是如果像我这样的人来这里寻找答案,那么这可能会有所帮助。我已经能够克服上述错误。
1) 从插件 maven-surefire-plugin 中删除以下代码
<reuseForks>true</reuseForks>
<argLine>-Xmx2048m</argLine>
2)添加以下目标:
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
FWhat tdrury 说:
将您的插件配置更改为:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
<executions>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>prepare-integration-tests</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
编辑:刚刚注意到一件重要的事情,destFile 和 dataFile 似乎区分大小写,所以它应该是 destFile,而不是 destfile。
我已经尝试了所有答案,但只有以下建议组合对我有用。为什么?我有非常具体的要求:
当从命令行运行构建时,JaCoCo 会生成报告: mvn clean verify (Maven 3.6.0) Intellij IDEA (2019.01) 也运行我的测试 这一切都在存在另一个 javaagent 的情况下工作在surefire插件中定义
解决方案 - 如 surefire
FAQ (my fixed configuration) 中所述,在 surefire
配置中使用“后期替换”maven 属性 @{...}
预先添加 argLine
值
如何使用 argLine 中其他插件设置的属性?在运行任何插件之前,Maven 会对 pom.xml 中的 ${...} 值进行属性替换。所以 Surefire 永远不会在其 argLine 属性中看到占位符。由于版本 2.17 使用这些属性的替代语法,@{...} 允许在执行插件时延迟替换属性,因此将正确选择已被其他插件修改的属性。
第一次尝试失败 - jacoco
的 prepare-agent
目标配置中的 define jaCoCoArgLine 属性 - 场景未能满足我的第二个要求,IntelliJ IDEA 无法找出我在项目中用于静态方法模拟的 jmockit 代理时间>
刚刚遇到同样的问题。
我有一个名为 HelloWorld
的类,我为它创建了一个名为 HelloWorldTests
的测试类,然后我得到了输出 Skipping JaCoCo execution due to missing execution data file.
然后我尝试更改我的 pom.xml
以使其工作,但尝试失败。
最后,我只需将 HelloWorldTests
重命名为 HelloWorldTest
,就成功了!
所以我猜想,默认情况下,jacoco 只识别名为 XxxTest
的测试类,这表明它是 Xxx
的测试类。因此,只需将您的测试类重命名为这种格式就可以了!
使用 maven-surefire-plugin 或 maven-failsafe-plugin 时,您不能使用 0 的 forkCount 或将 forkMode 设置为 never,因为这会阻止使用 javaagent 集执行测试并且不会记录覆盖率。
参考:https://www.eclemma.org/jacoco/trunk/doc/maven.html
这是我的gist
我遇到了同样的问题。我将 Jacoco 版本从 0.6 更新到 0.8 并更新了 surefire 插件。以下命令在 site/jacoco/ 文件夹中生成 Jacoco 报告:
mvn clean jacoco:prepare-agent install jacoco:report
我的maven配置如下:
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.0</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<forkedProcessExitTimeoutInSeconds>60</forkedProcessExitTimeoutInSeconds>
<forkCount>1</forkCount>
</configuration>
</plugin>
</plugins>
我挣扎了好几天。我尝试了此线程中建议的所有不同配置。它们都不起作用。最后,我发现只有重要的配置是 prepare-agent 目标。但是你必须把它放在正确的阶段。我看到很多例子把它放在“预集成测试”中,这是一种误导,因为它只会在单元测试之后执行。所以单元测试不会被检测。
正确的配置应该只使用默认阶段,(不要明确指定阶段)。通常,您不需要集中在 maven-surefire-plugin 周围。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
尝试使用:
mvn jacoco:report -debug
查看有关您的报告流程的详细信息。
我这样配置我的 jacoco:
<configuration>
<dataFile>~/jacoco.exec</dataFile>
<outputDirectory>~/jacoco</outputDirectory>
</configuration>
然后 mvn jacoco:report -debug
使用默认配置显示它,这意味着 jacoco.exec
不在 ~/jacoco.exec
中。错误显示 missing execution data file
。
所以只需使用默认配置:
<execution>
<id>default-report</id>
<goals>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
一切正常。
我知道这已经晚了,但仅适用于遇到此问题且似乎没有任何解决方法的其他人(就像我一样)。确保在您的 pom 中,您在 plugins
内对 jacoco 的配置不在 pluginManagement
内。 maven 的某种默认设置似乎是将 plugins
放在 pluginManagement
中。在您尝试添加像 jacoco 这样的详细配置之前,这几乎不会引起注意。为了添加这些,您需要将它们添加到 pluginManagement
之外,否则它们将被有效地忽略。有关详细信息,请参阅下面的我的 poms。
我的旧 pom(给出了“跳过 jacoco ...”的消息):
<project>
...
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我的新 pom(现在可以编译一份工作 jacoco 报告):
<project>
...
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
如果您的项目路径在任何地方都有空格,则会发生这种情况,例如:
/home/user/my projects/awesome project
不生成报告。如果是这种情况,请从目录名称中删除这些空格,例如:
/home/user/my-projects/awesome-project
或将项目移动到沿途没有空格的目录。
关于插件配置,我只需要基本如下:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
我添加了一个具有以下功能的具有 1 个域类的 Maven/Java project:
使用插件 Surefire 和 Failsafe 进行单元或集成测试。
找虫子。
通过 Jacoco 测试覆盖率。
Jacoco 结果在哪里?测试并运行“mvn clean”后,您可以在“target/site/jacoco/index.html”中找到结果。在浏览器中打开此文件。
享受!
我试图使项目尽可能简单。该项目将来自这些帖子的许多建议放在一个示例项目中。谢谢你,贡献者!
执行表示将 jacoco 数据放入 /Users/davea/Dropbox/workspace/myproject/target/jacoco.exec 但您的 maven 配置正在 ${basedir}/target/coverage-reports/jacoco-unit 中查找数据。执行。
我的回复很晚,但对于其他用户在您的情况下,您必须配置故障安全插件以使用保存在 itCoverageAgent 变量中的命令行代理配置。举个例子
<configuration>
<argLine>${itCoverageAgent}</argLine>
</configuration>
在您的 maven 配置中,jacoco 在 prepare-agent 阶段准备命令行参数,但故障安全插件不使用它,因此没有执行数据文件。
有时执行是第一次运行,当我们执行 maven clean install 时,它不会在之后生成。问题是在主 pom 文件的 maven-compiler-plugin 下对 skipMain 和 skip 属性使用 true。如果它们是作为任何问题或建议的一部分引入的,请将它们删除。
在我的例子中,prepare 代理有一个不同的 destFile
配置,但相应的报告必须配置一个 dataFile
,但是这个配置丢失了。添加 dataFile
后,它开始正常工作。
我遇到了类似的错误,因为跳过了测试执行。
我使用类似的系统参数运行我的构建:-Dmaven.test.skip.exec=true
将此系统参数设置为 false
解决了该问题。
Jacoco 执行数据文件是一个 jacoco.exec 文件,它是在准备代理目标运行时生成的。如果未生成它或未在配置中设置正确的路径,您将收到该错误消息。因为 Jacoco 使用它来构建测试覆盖率。当您使用 jacoco maven 插件和 surfire 或 failsafe 时,通常会发生这种情况。为确保生成 jacoco.exec 文件,您必须在 pom.xml 文件中添加 argline,而不是在 surfire 配置中,而是在 pom.xml 文件的属性标签中。
<properties>
<argLine>-Xmx2048m</argLine>
</properties>
不定期副业成功案例分享
@{argLine}
有一点不同的建议。mvn clean package sonar:sonar -U -DargLine="-Dxxx=yyy"
中有自定义参数。我没有明确声明 maven-surefire-plugin 并设置配置。我只是在 maven 命令行中添加 argLine 占位符,例如mvn clean package sonar:sonar -U -DargLine="@{argLine} -Dxxx=yyy"
。现在生成 jacoco.exec 文件并在声纳中生成覆盖率报告。