使用Maven生成JaCoCo代码覆盖率报告

5

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

在我的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 test时,它什么也不做。甚至没有错误或其他提示。测试顺利通过,但Maven似乎没有看到JaCoCo。如果我尝试执行mvn jacoco:report,我会收到以下信息:由于缺少执行数据文件,跳过JaCoCo执行。

3个回答

6
以下配置应该就足够了:
<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/ 中找到。

以下是原因,解释为何在您的情况下无法工作:

  • 插件配置位于 pluginManagement 内部
  • 插件位于 profile 内部

在执行 mvn test 时,请还要检查 Maven 日志以查看 jacoco-maven-plugin。如需更多信息,请运行 mvn -X test


谢谢,我的插件在pluginManagement里面,所以它没有起作用。 - Nicryc

2
这应该可以运行。只需在命令行中运行"mvn clean test"即可。最初的回答。
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.2</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <!-- attached to Maven test phase -->
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

https://www.mkyong.com/maven/maven-jacoco-code-coverage-example/


0
我终于在Baeldung的帮助下成功了,非常感谢他们!主要是在配置部分我遇到了困难。
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.11</version>
    <executions>
        <execution>
            <phase>verify</phase>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
            <configuration>
                <dataFileIncludes>
                    <dataFileInclude>**/jacoco.exec</dataFileInclude>
                </dataFileIncludes>
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

对于一个Maven多模块项目,修改outputDirectory${maven.multiModuleProjectDirectory}/target/site/jacoco-aggregate/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接