【问题标题】:Generating a JaCoCo code coverage report with Maven使用 Maven 生成 JaCoCo 代码覆盖率报告
【发布时间】:2019-04-16 21:21:08
【问题描述】:

不明白,我尝试用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 测试时,它什么也没做。甚至没有错误或其他东西。它说为我的测试建立成功但 Maven 似乎没有看到 JaCoCo。如果我尝试执行 mvn jacoco:report 无论如何我有一条消息:Skipping JaCoCo execution due to missing execution data file.

【问题讨论】:

    标签: maven code-coverage jacoco


    【解决方案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

    当你为jacoco-maven-plugin 执行mvn test 时,还要检查maven 日志。欲了解更多信息,请运行mvn -X test

    【讨论】:

    • 谢谢,我的插件在pluginManagement里面,所以没用。
    【解决方案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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多