JaCoCo与Maven-缺少执行数据文件 [英] JaCoCo with Maven - missing execution data file

查看:1156
本文介绍了JaCoCo与Maven-缺少执行数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a Maven multi module project consisting of a parent (HelloWorld) and different children (HelloWorldServices and HelloWorldPresentation) and use Jenkins to build.

运行成功的测试后的错误是

The error after running the successful test is

[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插件的方式:

This is how I defined the parent pom JaCoCo plugin:

<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中,我都没有明确提到surefire.我还尝试了随处可见的内容,将argLine放入配置中,但所有结果均相同.无论我做什么,都从未创建过JaCoCo .exec文件.至于目标,我使用

In no pom did I explicitly mention surefire. I also tried what you find everywhere to put the argLine in the configuration but all with the same result. The JaCoCo .exec file has never been created, no matter what I do. As for the goals, I use

mvn clean install jacoco:prepare-agent jacoco:report

自从我省略了jacoco目标之后,它甚至都没有显示INFO消息.

Since when I omit the jacoco goals, it doesn't even display the INFO message.

推荐答案

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

You should not invoke the agent after the install phase but before, so instead of invoking:

mvn clean install jacoco:prepare-agent jacoco:report

您应该调用

mvn clean jacoco:prepare-agent install jacoco:report

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

The main reason is: the agent will not participate to the build lifecycle, the test phase will already be executed as part of the install phase, then Maven will execute the agent as per command line invocation, but it will be too late.

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

You should probably also change the plugin configuration above to:

<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也是如此.

Note: I removed the configuration section, because it was actually pointing to default values. Moreover, XML elements are case sensitives here, so your datafile element was simply ignored, it should have been dataFile instead. The same applies to destFile.

prepare-agent 目标已经使用${project.build.directory}/jacoco.exec作为默认值,对 report 目标的dataFile值也是如此.进行此更改的主要原因将是更灵活,更标准的构建,而不是将artifactId作为项目名称(默认名称,但仍不是强制性的),而是使用更通用的${project.build.directory}属性代替,直接指向target

The prepare-agent goal is already using ${project.build.directory}/jacoco.exec as default destFile value, the same applied to the dataFile value for the report goal. The main reason for this change would be a more flexible and standard build, not relying on artifactId as project name (the default, but still not mandatory) and using the more generic ${project.build.directory} property instead to point directly at target.

最后的提示:确保在build/plugins部分而不是build/pluginManagement/plugins部分中配置Jacoco插件执行. pluginManagement部分用于治理和版本或配置的通用协调,但如果未在build/plugins下声明相应的插件,则会忽略.
根据 Maven官方POM参考

Final note: make sure to configure the Jacoco Plugin executions within the build/plugins section and not build/pluginManagement/plugins section. The pluginManagement section is meant for governance and common harmonization of versions or configurations, but it will be ignored if the corresponding plugin would not be declared under build/plugins.
As per official Maven POM reference

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

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.

(注意:粗体是我的)

这篇关于JaCoCo与Maven-缺少执行数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆