使用Maven生成JaCoCo代码覆盖率报告 [英] Generating a JaCoCo code coverage report with Maven

查看:186
本文介绍了使用Maven生成JaCoCo代码覆盖率报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白,我尝试使用最简单的JaCoCo和Maven生成代码覆盖率报告.

I don't understand, I try to generate code coverage report with JaCoCo and Maven, the simplest.

我的pom.xml中有以下插件:

I have the following plugin in my pom.xml :

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
      <execution>
        <id>prepare-agent</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
      </execution>
      <execution>
        <id>report</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>report</goal>
        </goals>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <!-- Sets the path to the file which contains the execution data. -->

          <dataFile>target/jacoco.exec</dataFile>
          <!-- Sets the output directory for the code coverage report. -->
          <outputDirectory>target/my-reports</outputDirectory>
        </configuration>
      </execution>
    </executions>
    <configuration>
      <systemPropertyVariables>
        <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
      </systemPropertyVariables>
  </configuration>
</plugin>

当我尝试进行mvn测试时,它什么也没做.甚至没有错误.它说我的测试建立成功,但是Maven似乎没有看到JaCoCo.如果仍然尝试执行mvn jacoco:report,则会显示一条消息:由于缺少执行数据文件而跳过JaCoCo执行.

And when I try to do a mvn test, it just doesn't do anything. Not even an error or something. It say BUILD SUCESS for my tests but Maven seems to not see JaCoCo. If I try to execute mvn jacoco:report anyway I have a message : Skipping JaCoCo execution due to missing execution data file.

推荐答案

以下配置应足够:

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>${jacoco.version}</version>
      <executions>
        <execution>
          <id>prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>report</id>
          <phase>test</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

然后可以在 target/site/jacoco/

在您的情况下无法使用它的原因:

Reasons why it does not work in your case:

  • 插件配置位于 pluginManagement
  • 该插件位于个人资料

对于 jacoco-maven-plugin 执行 mvn test 时,还请检查Maven日志.有关更多信息,请运行 mvn -X test

Also check the maven log when you execute mvn test for jacoco-maven-plugin. For more information run mvn -X test

这篇关于使用Maven生成JaCoCo代码覆盖率报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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