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

y1aodyip  于 11个月前  发布在  Maven
关注(0)|答案(3)|浏览(152)

我不明白,我试着用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:report,我会收到一条消息:Skipping JaCoCo execution due to missing execution data file.

nfeuvbwi

nfeuvbwi1#

以下配置应该足够了:

<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

在为jacoco-maven-plugin执行mvn test时,还要检查maven日志。

dzhpxtsq

dzhpxtsq2#

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

jk9hmnmh

jk9hmnmh3#

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/

相关问题