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

问题描述 投票:0回答:2

我不明白,我尝试使用最简单的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测试时,它只是没有做任何事情。甚至不是错误或其他什么。对于我的测试,它说BUILD SUCESS,但Maven似乎没有看到JaCoCo。如果我尝试执行mvn jacoco:报告,我还有一条消息:Skipping JaCoCo execution due to missing execution data file.

maven code-coverage jacoco
2个回答
1
投票

以下配置应该足够了:

<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执行jacoco-maven-plugin时,还要检查maven日志。有关更多信息,请运行mvn -X test


0
投票

这应该工作。只需从命令“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/

© www.soinside.com 2019 - 2024. All rights reserved.