xml Maven JaCoCo 插件错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12269558/
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
Maven JaCoCo plugin error
提问by kirigiri
I have configured the Maven JaCoCo plugin as follows in my pom.xml file:
我在 pom.xml 文件中按如下方式配置了 Maven JaCoCo 插件:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jacoco.version>0.5.9.201207300726</jacoco.version>
</properties>
<profiles>
<profile>
<id>jacoco4</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration
<destfile>${project.build.directory}/target/jacoco.exec</destfile>
<datafile>${project.build.directory}/target/jacoco.exec</datafile>
<output>file</output>
<append>true</append>
</configuration>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
I'm using Windows 7 and the apache-maven-3.0.4 plugin. When I type mvn -P jacoco4 install, either from a cygwin terminal or from a command prompt terminal, Maven downloads and runs the JaCoCo plugin, but then the jacoco.execfile doesn't appear to have been created. Below is the error message:
我使用的是 Windows 7 和 apache-maven-3.0.4 插件。当我mvn -P jacoco4 install从 cygwin 终端或命令提示符终端键入 时,Maven 下载并运行 JaCoCo 插件,但该jacoco.exec文件似乎尚未创建。下面是错误信息:
[ERROR] Unable to read execution data file C:\Users\brownru\workspace64new\vps9\vps-fileserver\target\jacoco.exec: C:\Users\brownru\workspace64new\vps9\vps-fileserver\target\jacoco.exec (The system cannot find the file specified)
java.io.FileNotFoundException: C:\Users\brownru\workspace64new\vps9\vps-fileserver\target\jacoco.exec (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at org.jacoco.maven.ReportMojo.loadExecutionData(ReportMojo.java:251)
at org.jacoco.maven.ReportMojo.executeReport(ReportMojo.java:228)
at org.jacoco.maven.ReportMojo.execute(ReportMojo.java:217)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
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:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
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:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
This error message appears whether or not I include the destfileand datafilespecifiers in the configuration of the plugin:
无论我是否在插件配置中包含destfile和datafile说明符,都会出现此错误消息:
<destfile>${project.build.directory}/target/jacoco.exec</destfile>
<datafile>${project.build.directory}/target/jacoco.exec</datafile>
Can someone please tell me what I'm doing wrong?
有人可以告诉我我做错了什么吗?
回答by Sylvain
I had the same trouble with jacoco and maven. It was related to a parent pom overwriting the configuration of surefire. In this case, that plugin didn't used the argument (for jvm argument) defining the agent.
我在 jacoco 和 maven 上遇到了同样的问题。这与父 pom 覆盖了 Surefire 的配置有关。在这种情况下,该插件没有使用定义代理的参数(对于 jvm 参数)。
The solution was to put the "argLine" configuration element back
解决方案是将“argLine”配置元素放回原处
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
</configuration>
</plugin>
Full plugin conf looks like
完整的插件配置看起来像
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>unit-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>${maven.test.skip}</skip>
<argLine>${argLine}</argLine>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>${skipITs}</skip>
<argLine>${argLine}</argLine>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.10.201208310627</version>
<configuration>
<skip>${maven.test.skip}</skip>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
<output>file</output>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Hope it'll be usefull
希望它会很有用
回答by rogerdpack
OK I think I figured out what is going on.
好的,我想我知道发生了什么。
By default, the jacoco plugin "runs" before the test phase (typically it runs the prepare-agentgoal during the initializelifecycle phase), and when it runs, it just sets a maven property called "argLine" to something like -javaagent=jacoco.jar
默认情况下,jacoco 插件在测试阶段之前“运行”(通常它prepare-agent在初始化生命周期阶段运行目标),当它运行时,它只是将一个名为“argLine”的 maven 属性设置为类似-javaagent=jacoco.jar
ex:
前任:
[INFO] argLine set to -javaagent:/usernamed/.m2/repository/org/jacoco/org.jacoco.agent/ 0.5.6.201201232323/org.jacoco.agent-0.5.6.201201232323-runtime.jar=destfile=/path/to/target/jacoco.exec
[信息] argLine 设置为 -javaagent:/usernamed/.m2/repository/org/jacoco/org.jacoco.agent/ 0.5.6.201201232323/org.jacoco.agent-0.5.6.201201232323-runtime.jar=destfile=/path到/目标/jacoco.exec
By default, maven-surefire-plugin basically "prepends" this property (if it's set to anything) to its forked java test processes, so they get the goods. Ex: java ${argLine ends up here}> -jar /xxx/surefirebooter3741906822495182152.jar
默认情况下,maven-surefire-plugin 基本上将这个属性(如果它设置为任何东西)“前置”到它的分叉 java 测试进程,所以他们得到了货物。前任:java ${argLine ends up here}> -jar /xxx/surefirebooter3741906822495182152.jar
Typically (without jacoco), if you want to alsoadd something else of your own to that argLine (for instance, -Xmx1Gor the like), you just set it in the surefire configuration, like
通常(不含jacoco),如果你想还添加其他东西你自己到argLine(例如,-Xmx1G等),你只要把它在万无一失的配置,像
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx1G</argLine>
</configuration>
</plugin>
However, if you're using jacoco, you can't do it the normal way you do it by setting a global property, this wayinstead (this is the pom's root global properties):
但是,如果您使用 jacoco,则不能通过设置全局属性来按照正常方式进行操作,而是采用这种方式(这是 pom 的根全局属性):
<properties>
<argLine>-Xmx1G</argLine>
</properties>
If you do set the <configuration><argLine>then it basically overrides the system property, so jacoco arguments aren't passed down to the child process. So that's why you use the property way instead. If you specify a property argLinethen jacoco will just add its parameters to whatever you specify, then surefire will use it.
如果您确实设置了,<configuration><argLine>那么它基本上会覆盖系统属性,因此 jacoco 参数不会传递给子进程。所以这就是为什么你使用属性方式。如果您指定一个属性,argLine那么 jacoco 只会将其参数添加到您指定的任何内容中,然后 surefire 将使用它。
However, what if your parent pom has alreadyset the plugin's <configuration><argLine>to something? Or if you yourself are setting it? It will basically use that value instead of the property that jacoco is setting (you've specified a manual override).
但是,如果您的父 pom已经将插件设置<configuration><argLine>为某些内容怎么办?或者如果你自己设置它?它基本上会使用该值而不是 jacoco 正在设置的属性(您已指定手动覆盖)。
If you're specifying <configuration><argLine>yourself, you can change that argLine into a property (see above), and remove the <configuration><argLine>and it should work. If you can't control the parent, and the parent specifies something for argline, then you'll need to go the the <configuration><argLine>${argLine} -Xmx1G</argLine>route. This is to instruct it to ignore whatever the parent set this value to, and use argLine instead (the one jacoco sets for you). (It's unclear to me if there's an easy way to "add" to the value the parent pom has for this value, if anybody knows how feel free to comment here).
如果您指定<configuration><argLine>自己,您可以将该 argLine 更改为一个属性(见上文),然后删除它<configuration><argLine>,它应该可以工作。如果您无法控制父级,并且父级为 argline 指定了某些内容,那么您将需要走这<configuration><argLine>${argLine} -Xmx1G</argLine>条路线。这是为了指示它忽略父级将此值设置为的任何内容,并使用 argLine 代替(jacoco 为您设置的那个)。(我不清楚是否有一种简单的方法可以“添加”父 pom 对该值的值,如果有人知道如何在这里发表评论的话)。
But what if jacoco doesn't run for some target, or some profile? Then the variable ${argLine}never gets set, and you can run into an error like this:
但是,如果 jacoco 没有针对某个目标或某个配置文件运行怎么办?然后变量${argLine}永远不会被设置,你可能会遇到这样的错误:
Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.14:test failed: The forked VM terminated without saying properly goodbye. VM crash or System.exit called ? [ERROR] Command was/bin/sh -c cd ...java '${argLine}' ...
目标 org.apache.maven.plugins:maven-surefire-plugin:2.14:test 的执行默认测试失败:分叉的 VM 未正确说再见就终止了。VM 崩溃或 System.exit 调用?[错误] 命令是/bin/sh -c cd ...java '${argLine}' ...
Well it turns out that jacoco only "adds" to the property named argLine, when it runs. So you can safely add a <properties><argLine></argLine></properties>to your pom (unless you already have that set in the parent pom, then you don't need to add anything). If jacoco is ever invoked, it adds to it. If not, it's set to an empty string, which is OK. It's also unclear if there's a way to "add" to the parent's value for a property, so it's either inherit it, if you know it exists, or specify it as empty.
事实证明,jacoco 仅在运行时“添加”到名为 argLine 的属性。因此,您可以安全地将 a 添加<properties><argLine></argLine></properties>到您的 pom (除非您已经在父 pom 中设置了该设置,否则您不需要添加任何内容)。如果 jacoco 被调用过,它会添加到它。如果不是,则将其设置为空字符串,这是可以的。也不清楚是否有办法“添加”到父属性的值,所以它要么继承它,如果你知道它存在,要么将它指定为空。
So, in the end for me, since my upstream (inaccessible) parent pom declared it like
所以,最后对我来说,因为我的上游(无法访问)父 pom 声明它像
<configuration><argList>${argList}</argList></configuration>
<configuration><argList>${argList}</argList></configuration>
I was forced to basically follow that route, since it was set already in an (out of my control) parent pom, thusly:
我被迫基本上遵循这条路线,因为它已经设置在(不受我控制的)父 pom 中,因此:
<configuration><argList>${argList} -Xmx1G</argList></configuration>
<configuration><argList>${argList} -Xmx1G</argList></configuration>
NB that Intellij will complain and "not add any settings" to your unit test if you have this in your pom:
注意,如果你的 pom 中有这个,Intellij 会抱怨并且“不添加任何设置”到你的单元测试中:
<configuration><argLine>${argLine} -DcustomOption=X...</argLine>
<configuration><argLine>${argLine} -DcustomOption=X...</argLine>
but don't declare a property named argLine at all (even though it works on the command line mvn just fine). Fix/work around: declare an empty 'argLine' global property, and/or just move your -DcustomOption=Xto the property declaration instead, see above. If you "only" set it in the property declaration then for IntelliJ to pick it up you'll also need <configuration><argLine>${argLine}</argLine>...:|
但根本不要声明名为 argLine 的属性(即使它在命令行 mvn 上工作得很好)。修复/变通方法:声明一个空的 'argLine' 全局属性,和/或只是将您-DcustomOption=X的移动到属性声明,见上文。如果您“仅”在属性声明中设置它,那么对于 IntelliJ 选择它,您还需要 <configuration><argLine>${argLine}</argLine>...:|
Unfortunately even this wasn't enough, it hosed sonar, though worked with IntelliJ + maven. Not sure why. Changed the way I parsed times instead, so didn't have to mess with the zones (i.e. "got them right" in the code).
不幸的是,即使这还不够,尽管与 IntelliJ + maven 一起工作,但它还是声纳。不知道为什么。改变了我解析时间的方式,因此不必弄乱区域(即在代码中“正确处理”)。
回答by jimr
I also came across this problem: JaCoCo doesn't produce a 'jacoco.exec' output file, meaning that no code coverage analysis occurs.
我也遇到过这个问题:JaCoCo 不生成 'jacoco.exec' 输出文件,这意味着没有发生代码覆盖率分析。
In my case it was also due to using a custom argLine in the Maven Surefire Plugin which overrided the JaCoCo Maven Plugin argLine, causing JaCoCo not to be executed.
就我而言,这也是由于在 Maven Surefire 插件中使用自定义 argLine 覆盖了 JaCoCo Maven 插件 argLine,导致 JaCoCo 无法执行。
To fix this I used the JaCoCo optional parameter "propertyName" to export its argLine to a Maven property and included that in the Surefire argLine:
为了解决这个问题,我使用 JaCoCo 可选参数“propertyName”将其 argLine 导出到 Maven 属性并将其包含在 Surefire argLine 中:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<propertyName>jacoco.agent.argLine</propertyName>
</configuration>
...
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<argLine>-XX:-UseSplitVerifier ${jacoco.agent.argLine}</argLine>
</configuration>
</plugin>
However this caused a problem when individual tests were run in Netbeans. Because the JaCoCo plugin was not executed in this scenario the "jacoco.agent.argLine" variable wasn't initialised and Surefire failed before running any tests.
但是,当在 Netbeans 中运行单个测试时,这会导致问题。因为在这种情况下没有执行 JaCoCo 插件,所以“jacoco.agent.argLine”变量没有初始化,并且在运行任何测试之前 Surefire 失败。
Adding a blank property "jacoco.agent.argLine" to the pom solved the issue when running single tests, but this also stopped JaCoCo from exporting its argLine when it was being executed, effectively disabling JaCoCo.
在 pom 中添加一个空白属性“jacoco.agent.argLine”解决了运行单个测试时的问题,但这也阻止了 JaCoCo 在执行时导出其 argLine,从而有效地禁用了 JaCoCo。
The final part of the solution that I've used was to add a profile which creates the blank property, and activates only when a single test is specified:
我使用的解决方案的最后一部分是添加一个创建空白属性的配置文件,并且仅在指定单个测试时激活:
<profiles>
<profile>
<activation>
<property>
<name>test</name>
</property>
</activation>
<properties>
<jacoco.agent.argLine></jacoco.agent.argLine>
</properties>
</profile>
</profiles>
回答by Sanchi Girotra
I faced the same problem and fixed by using below command in place of jacoco:report -
我遇到了同样的问题,并通过使用以下命令代替 jacoco:report 进行了修复 -
mvn -X org.jacoco:jacoco-maven-plugin:report
Referred - How to configure JaCoCo maven plugin from command line
回答by Andrzej Jozwik
I use configuration:
我使用配置:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<skip>${skipTests}</skip>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<phase>initialize</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Update:Configuration generated by sonar (sonar-pom.xml):
更新:声纳生成的配置(sonar-pom.xml):
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludedGroups>server,ignore,integration</excludedGroups>
</configuration>
</execution>
</executions>
<configuration>
<excludedGroups>server,ignore,integration</excludedGroups>
<argLine>-javaagent:/tmp/jacocoagent3671192291664069011.jar=destfile=target/jacoco.exec,excludes=*_javassist_*</argLine>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
One problem - how to define "jacocoagent3671192291664069011.jar" for each build. It should be in:
一个问题 - 如何为每个构建定义“jacocoagent3671192291664069011.jar”。它应该在:
$M2_HOME/repository/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar

