Java maven jacoco:不生成代码覆盖率报告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25395255/
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-08-11 00:19:05  来源:igfitidea点击:

maven jacoco: not generating code coverage report

javamavencode-coveragejacoco

提问by daydreamer

I am trying to setup jacocofor my project's code coverage

我正在尝试设置jacoco我的项目的代码覆盖率

My project is based on Java 1.8

我的项目基于 Java 1.8

Here is how things look in my project's pom.xml

这是我的项目中的样子 pom.xml

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.5.10.201208310627</version>
        <configuration>
            <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>  

Then I run mvn testand see the following

然后我运行 mvn test并看到以下内容

$ mvn test
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building pennyapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.5.10.201208310627:prepare-agent (jacoco-initialize) @ pennyapp ---
[INFO] argLine set to -javaagent:/Users/harit/.m2/repository/org/jacoco/org.jacoco.agent/0.5.10.201208310627/org.jacoco.agent-0.5.10.201208310627-runtime.jar=destfile=/Users/harit/code/idea/pennyapp/target/jacoco.exec,append=true,output=file
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pennyapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pennyapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/harit/code/idea/pennyapp/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pennyapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/harit/code/idea/pennyapp/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pennyapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/harit/code/idea/pennyapp/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ pennyapp ---
[INFO] Surefire report directory: /Users/harit/code/idea/pennyapp/shippable/testresults
[INFO] Using configured provider org.apache.maven.surefire.junit4.JUnit4Provider

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
objc[13225]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Running HelloTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.1 sec - in HelloTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.523 s
[INFO] Finished at: 2014-08-19T17:56:33-07:00
[INFO] Final Memory: 10M/119M
[INFO] ------------------------------------------------------------------------

and then I run mvn jacoco:reportand I see

然后我跑mvn jacoco:report,我看到

$ mvn jacoco:report
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building pennyapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.5.10.201208310627:report (default-cli) @ pennyapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.176 s
[INFO] Finished at: 2014-08-19T17:56:51-07:00
[INFO] Final Memory: 11M/112M
[INFO] ------------------------------------------------------------------------

Then I look at target/site/jacoco/index.htmland see the following
enter image description here

然后我查看target/site/jacoco/index.html并看到以下内容
在此处输入图片说明

Question
- What is incorrect in the configuration?
- How can I generate report?

问题
- 配置中有什么不正确?
- 如何生成报告?

Thanks

谢谢

采纳答案by Kolargol00

Any particular reason why you are using an outdated version of the JaCoCo plugin? For Java 8 support, you have to use at least version 0.7.0 (see changelog).

您使用过时版本的 JaCoCo 插件的任何特殊原因?对于 Java 8 支持,您必须至少使用 0.7.0 版(请参阅更改日志)。

In your configuration, the report goal is bound to the verify phase, so running mvn testwon't generate any report because it does not run the verify phase (test phase comes before verify). You have to use mvn verifyto execute tests and generate the report.

在您的配置中,报告目标绑定到验证阶段,因此运行mvn test不会生成任何报告,因为它不运行验证阶段(测试阶段在验证之前)。您必须使用mvn verify来执行测试并生成报告。

The JaCoCo project provides example Maven configurations. You can try "this POM file for a JAR project runs JUnit tests under code coverage and creates a coverage report".

JaCoCo 项目提供了示例 Maven 配置。您可以尝试“此 JAR 项目的 POM 文件在代码覆盖率下运行 JUnit 测试并创建覆盖率报告”。

回答by Pierrick HYMBERT

JaCoco Maven Plugin is overriding Surefire argLine, in case you also need to override argLine, be sure to keep argLine variable:

JaCoco Maven 插件覆盖 Surefire argLine,如果您还需要覆盖 argLine,请确保保留 argLine 变量:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <argLine>-Xmx1024M ${argLine}</argLine> 
  </configuration>
</plugin>

Note you can change this property name, as describe in the jacoco plugin documentation.

请注意,您可以更改此属性名称,如jacoco 插件文档中所述

回答by Bohemian

This worked for me:

这对我有用:

mvn clean install
mvn site

Even though the minimum code coverage was not met and mvn clean installfailed, the mvn sitebuild succeeded and created the coverage report at:

即使未达到最低代码覆盖率并mvn clean install失败,mvn site构建成功并在以下位置创建了覆盖率报告:

.../target/site/jacoco/index.html