maven jacoco:没有生成代码覆盖率报告

day*_*mer 21 java code-coverage maven jacoco

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

我的项目基于 Java 1.8

以下是我的项目中的内容 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>  
Run Code Online (Sandbox Code Playgroud)

然后我跑 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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

然后我跑了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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

然后我看看target/site/jacoco/index.html并看到以下内容
在此输入图像描述

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

谢谢

Kol*_*l00 25

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

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

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


Orn*_*que 13

我遇到过同样的问题。问题是我在标签<plugins>内列出的jacoco 插件中添加了该插件<pluginManagment>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
       ...
        <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.4</version>
        <executions>
          <execution>
            <id>default-prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>jacoco-report</id>
            <phase>test</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
...
       
      </plugins>
    </pluginManagement>
Run Code Online (Sandbox Code Playgroud)

添加<plugins>为直接子项后,<build>一切正常。

<build>
....
 <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.4</version>
        <executions>
          <execution>
            <id>default-prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>jacoco-report</id>
            <phase>test</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
...
<build>
Run Code Online (Sandbox Code Playgroud)


Pie*_*ERT 12

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>
Run Code Online (Sandbox Code Playgroud)

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


use*_*602 7

以下对我来说没有任何问题

<plugin>
   <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.4</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>                   
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

注意:阶段是“测试”,以便在执行测试后生成报告


Boh*_*ian 6

这对我有用:

mvn clean install
mvn site
Run Code Online (Sandbox Code Playgroud)

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

.../target/site/jacoco/index.html
Run Code Online (Sandbox Code Playgroud)