java 在 maven 中配置 JaCoCo 时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26025455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:09:45  来源:igfitidea点击:

Trouble configuring JaCoCo in maven

javamavenjacocojacoco-maven-plugin

提问by Dubius

I am trying to do a simple JaCoCo report through Maven and I keep getting the same error. Here is a snippet of my plugin.

我正在尝试通过 Maven 做一个简单的 JaCoCo 报告,但我一直收到同样的错误。这是我的插件的一个片段。

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.2.201409121644</version>
    <executions>
        <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.01</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

When I run mvn clean install jacoco:checkI get the following

当我运行时,mvn clean install jacoco:check我得到以下信息

Failed to execute goal org.jacoco:jacoco-maven-plugin:0.7.2.201409121644:check (default-cli) on project ###########: The parameters 'rules' for goal org.jacoco:jacoco-maven-plugin:0.7.2.201409121644:check are missing or invalid -> [Help 1]

无法执行目标 org.jacoco:jacoco-maven-plugin:0.7.2.201409121644:check (default-cli) on project ###########:目标 org.jacoco:jacoco 的参数“规则” -maven-plugin:0.7.2.201409121644:check are missing or invalid -> [Help 1]

I've tried changing the version from 0.6.3 to 0.7.2 and every version in between. As far as I can tell this looks like valid config for any of those versions above 0.6.3 and was even originally taken from their own examples found at the below link (I just removed everything but the check goal):

我尝试将版本从 0.6.3 更改为 0.7.2 以及介于两者之间的每个版本。据我所知,对于 0.6.3 以上的任何版本,这看起来都是有效的配置,甚至最初取自在以下链接中找到的他们自己的示例(我只是删除了除检查目标之外的所有内容):

http://www.eclemma.org/jacoco/trunk/doc/maven.html

http://www.eclemma.org/jacoco/trunk/doc/maven.html

If I run with the -Xoption I get the following stack trace:

如果我使用该-X选项运行,则会得到以下堆栈跟踪:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.jacoco:jacoco-maven-plugin:0.7.2.201409121644:check (default-cli) on project science-open: The parameters 'rules' for goal org.jacoco:jacoco-maven-plugin:0.7.2.201409121644:check are missing or invalid
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:220)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.apache.maven.plugin.PluginParameterException: The parameters 'rules' for goal org.jacoco:jacoco-maven-plugin:0.7.2.201409121644:check are missing or invalid
    at org.apache.maven.plugin.internal.DefaultMavenPluginManager.populatePluginFields(DefaultMavenPluginManager.java:584)
    at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getConfiguredMojo(DefaultMavenPluginManager.java:537)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:120)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 25 more

What am i doing wrong?

我究竟做错了什么?

回答by guido

This is because you launch maven with the explicit goal:

这是因为您以明确的目标启动 Maven:

mvn ... jacoco:check

Running like this, the <configuration>section inside <execution>is not read; to make it work, use the default maven phase to which the jacoco:checkgoal is bound, which is verify

这样运行,<configuration>里面的section<execution>是不读的;要使其工作,请使用jacoco:check目标绑定的默认 maven 阶段,即verify

mvn clean verify

Or, alternatively, (but I cannot try this myself right now and I am not 100% sure), try using a default-prefix in the executions ids, like:

或者,或者,(但我现在不能自己尝试,我也不是 100% 确定),尝试default-在执行 ID 中使用前缀,例如:

   <execution>
        <id>default-jacoco-check</id>
        <goals>
            <goal>check</goal>
        </goals>
        [...]
   </execution>