【问题标题】:JaCoCo with Maven - missing execution data file带有 Maven 的 JaCoCo - 缺少执行数据文件
【发布时间】:2016-07-18 05:14:12
【问题描述】:

我们有一个 Maven 多模块项目,由一个父 (HelloWorld) 和不同的子 (HelloWorldServices 和 HelloWorldPresentation) 组成,并使用 Jenkins 来构建。

运行测试成功后的错误是

[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (default-cli) @ HelloWorldServices ---
[INFO] Skipping JaCoCo execution due to missing execution data file:/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec

前面几行

[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (default-cli) @ HelloWorldServices ---
[INFO] argLine set to -javaagent:/var/lib/jenkins/.m2/repository/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec

这就是我定义父 pom JaCoCo 插件的方式:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
        <destfile>${project.artifactId}/target/jacoco.exec</destfile>
        <datafile>${project.artifactId}/target/jacoco.exec</datafile>
    </configuration>

    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我在任何 pom 中都没有明确提到过万无一失。我还尝试了您在任何地方都能找到的将 argLine 放入配置的方法,但结果都相同。 JaCoCo .exec 文件从未被创建,无论我做什么。至于目标,我使用

mvn clean install jacoco:prepare-agent jacoco:report

因为当我省略 jacoco 目标时,它甚至不显示 INFO 消息。

【问题讨论】:

  • 改用mvn clean jacoco:prepare-agent install(两个阶段之间的代理)
  • 几天前我遇到了同样的问题,就我而言,问题是我没有遵循命名约定。测试文件应命名为 *Test.java。一旦我遵循命名约定,问题就消失了。
  • 一个非常 good answer 为我解决了问题。

标签: java maven maven-3 pom.xml jacoco-maven-plugin


【解决方案1】:

您不应该在安装阶段之后而是之前调用代理,所以不要调用:

mvn clean install jacoco:prepare-agent jacoco:report

你应该调用

mvn clean jacoco:prepare-agent install jacoco:report

主要原因是:代理不会参与构建生命周期,test 阶段已经作为install 阶段的一部分执行,然后 Maven 将按照命令行调用执行代理,但它为时已晚。


您可能还应该将上面的插件配置更改为:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

注意:我删除了配置部分,因为它实际上指向默认值。此外,这里的 XML 元素是区分大小写的,所以您的 datafile 元素被简单地忽略了,它应该是 dataFile 。这同样适用于destFile

prepare-agent 目标已经使用${project.build.directory}/jacoco.exec 作为默认的destFile 值,同样适用于report 目标的dataFile 值。此更改的主要原因将是更灵活和标准的构建,不依赖 artifactId 作为项目名称(默认,但仍然不是强制性的)并使用更通用的 ${project.build.directory} 属性而不是直接指向 target .


最后说明:确保在 build/plugins 部分而不是 build/pluginManagement/plugins 部分中配置 Jacoco 插件执行。 pluginManagement 部分用于管理和统一版本或配置,但如果相应的插件未在 build/plugins 下声明,它将被忽略
根据official Maven POM reference

pluginManagement:是一个可以在侧面插件中看到的元素。插件管理以几乎相同的方式包含插件元素,除了不是为这个特定的项目构建配置插件信息,它旨在配置从这个项目继承的项目构建。 但是,这只会配置子组件中实际在 plugins 元素中引用的插件。孩子们有权覆盖pluginManagement 定义。

(注意:粗体是我的)

【讨论】:

  • 仅调用 mvn install 时我需要做什么才能使这项工作正常进行?如果我没有明确调用 jacoco 目标,它们将不会被执行。
  • @dasLort 您使用的是哪个版本的 Maven?我无法通过示例多模块项目重现您的问题,我没有收到任何跳过警告并且正确获得了 jacoco 报告
  • Maven 3.3.9 和 (Jenkins) Maven 集成插件 2.12.1。所以你说你可以mvn install,它会用上面的配置调用jacoco?
  • 我发现编辑 META-INF/plexus/default-bindings.xml 文件可以解决问题。可悲的是,我认为在使用 Jenkins Maven 插件时这个会在一个罐子里。而且,我没有找到说的jar(不在maven-core-3.0.jar中)
  • 这个答案太到位了!我花了几个小时查看其他未提供此类信息的答案。我的问题现在已经解决了,非常感谢!
【解决方案2】:

我认为“destfile”和“datafile”区分大小写,所以尝试用“destFile”和“dataFile”替换它们,也许它会工作:)

【讨论】:

    【解决方案3】:
    • JaCoCo 报告是从执行数据文件中创建的。
    • 如果此文件不存在,则 JaCoCo 报告目标将跳过报告创建。
    • 因此必须创建执行数据文件。

    无法创建执行数据文件的原因如下
    - 不存在测试。
    - 忽略所有测试。
    - Surefire 插件丢失。
    - JaCoCo 的 prepare-agent 目标未执行,这会将配置所需的 argLine 设置为 surefire。
    - Surefire 插件未配置 JaCoCo 的代理。

    【讨论】:

      【解决方案4】:
      <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.8.5</version>
          <configuration>
              <target>
                  <propertyfile file="lombok.config">
                      <entry key="config.stopBubbling" value="true" />
                      <entry key="lombok.addLombokGeneratedAnnotation" value="true" />
                  </propertyfile>
              </target>
              <excludes>
                  <exclude>**/domain/**</exclude>
                  <exclude>**/enumeration/**</exclude>
                  <exclude>**/exception/**</exclude>
              </excludes>
          </configuration>
          <executions>
              <execution>
                  <goals>
                      <goal>prepare-agent</goal>
                  </goals>
              </execution>
              <!-- attached to Maven test phase -->
              <execution>
                  <id>report</id>
                  <phase>test</phase>
                  <goals>
                      <goal>report</goal>
                  </goals>
              </execution>
              <!-- Add this checking -->
              <execution>
                  <id>jacoco-check</id>
                  <goals>
                      <goal>check</goal>
                  </goals>
                  <configuration>
                      <rules>
                          <rule>
                              <element>PACKAGE</element>
                              <limits>
                                  <limit>
                                      <counter>INSTRUCTION</counter>
                                      <value>COVEREDRATIO</value>
                                      <minimum>65%</minimum>
                                  </limit>
                              </limits>
                          </rule>
                      </rules>
                  </configuration>
              </execution>
          </executions>
      </plugin>
      

      【讨论】:

        【解决方案5】:

        我与你分享我的回应可能是别人为他工作

                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.8.5</version>
        <!--                <configuration>
                            <excludes>
                                <exclude>package you want exclude</exclude>
                            </excludes>
                        </configuration>-->
                        <executions>
                            <execution>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <!-- attached to Maven test phase -->
                            <execution>
                                <id>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.9</minimum>
                                                </limit>
                                            </limits>
                                        </rule>
                                    </rules>
                                </configuration>
                            </execution>-->
        
                        </executions>
                    </plugin>
        

        【讨论】:

          【解决方案6】:

          我今天刚刚处理了这个问题,发现当我在 Surefire 插件中设置 argLine 参数时会发生这种情况(我需要这样做以运行 JavaFx 相关测试),即使在你有 @987654323 之后它也会替换默认值@ 在你的jacoco-maven-plugin 目标中。

          因此,基本上,解决方案是添加原始 argLine 并使用 @{argLine} 附加您的其他 arg 行,如 here 所述:

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
              <argLine>@{argLine} --enable-native-access ALL-UNNAMED --add-modules jdk.incubator.foreign</argLine>
            </configuration>
          </plugin>
          

          在那篇文章中提到分配 jacoco 代理 argline 变量 ${surefire.argLine} 但我认为这些步骤不是必需的(至少在我的情况下不是)。

          【讨论】:

            猜你喜欢
            • 2013-08-09
            • 2022-06-14
            • 2021-05-29
            • 1970-01-01
            • 2016-04-27
            • 1970-01-01
            • 2018-09-05
            相关资源
            最近更新 更多