maven - 执行 JaCoCo 时获取 "Skipping JaCoCo execution due to missing execution data file"

标签 maven junit jacoco

我正在使用 Maven 3.0.3、JUnit 4.8.1 和 Jacoco 0.6.3.201306030806,并且我正在尝试创建测试覆盖率报告。

我有一个仅包含单元测试的项目,但无法运行报告,当我运行时,我反复收到错误:由于缺少执行数据文件而跳过 JaCoCo 执行:

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] 

知道我缺少什么配置吗?

最佳答案

jacoco-maven-plugin:0.7.10-SNAPSHOT

来自jacoco:prepare-agent上面写着:

One of the ways to do this in case of maven-surefire-plugin - is to use syntax for late property evaluation:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <argLine>@{argLine} -your -extra -arguments</argLine>
  </configuration>
</plugin>

注意 @{argLine}添加到 -your -extra -arguments .

谢谢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] Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test. Depending on the project packaging type by default a property with the following name is set:

  • tycho.testArgLine for packaging type eclipse-test-plugin and
  • argLine otherwise.

Note that these properties must not be overwritten by the test configuration, otherwise the JaCoCo agent cannot be attached. If you need custom parameters please append them. For example:

<argLine>${argLine} -your -extra -arguments</argLine>

Resulting coverage information is collected during execution and by default written to a file when the process terminates.

您应该更改 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>

关于maven - 执行 JaCoCo 时获取 "Skipping JaCoCo execution due to missing execution data file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18107375/

相关文章:

json - Jersey 2 InjectLink 被忽略

java - 如何在执行junit时模拟某个方法?

java - 使用私有(private)访问说明符访问具有参数的私有(private)方法

java - Jacoco ant 任务 - 没有 jacoco.exec 输出

eclipse - 替换产品 ico 文件时 Tycho 出错

maven - 从Gradle改造Mule项目

java - 未找到 Maven 属性文件

java - 如果抛出异常,测试会失败吗?

maven - 如何让 jacoco 知道源/类在模块之外

java - 如何关闭 SonarQube 4 中的代码覆盖率?