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

时间:2019-04-16 21:21:08

标签: maven code-coverage jacoco

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

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

npm -g install @angular/cli@1.5.* # For ^5.0.0

当我尝试进行mvn测试时,它什么也没做。甚至没有错误。它说我的测试建立成功,但是Maven似乎没有看到JaCoCo。如果我仍然尝试执行mvn jacoco:report,则会显示一条消息:<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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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/