26

我们有一个 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 消息。

4

6 回答 6

52

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

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


最后一点:确保在build/plugins部分而不是build/pluginManagement/plugins部分中配置 Jacoco 插件执行。该pluginManagement部分用于管理和统一版本或配置,但如果相应的插件未在build/plugins.
根据官方 Maven POM 参考

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

(注:粗体字是我的)

于 2016-03-30T09:46:33.897 回答
6
  • JaCoCo 报告是从执行数据文件中创建的。
  • 如果此文件不存在,则 JaCoCo 报告目标将跳过报告创建。
  • 因此必须创建执行数据文件。

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

于 2019-04-18T10:24:42.127 回答
3

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

于 2016-09-28T08:44:55.360 回答
1

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

所以基本上,解决方案是添加原始 argLine 并使用此处@{argLine}所述的附加 arg 行:

<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},但我认为这些步骤不是必需的(至少在我的情况下不是)。

于 2022-01-31T16:00:14.570 回答
0

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

            <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>
于 2020-08-14T10:26:28.843 回答
0
<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>
于 2020-05-21T18:00:47.130 回答