maven - Jacoco 不生成覆盖率报告

标签 maven jacoco maven-surefire-plugin jacoco-maven-plugin

我使用 maven-sure fire 插件执行测试,使用 Jacoco 插件生成覆盖率报告。 Jacoco 不提供覆盖率报告,而是失败并显示如下所示的调试日志。

[信息] --- jacoco-maven-plugin:0.8.0:report (jacoco-site) @ util --- [INFO] 由于缺少执行数据文件而跳过 JaCoCo 执行。

这是 maven sure-fire 插件的外观。

             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <parallel>classes</parallel>
                     <threadCount>8</threadCount>
                     <forkCount>4</forkCount>
                     <encoding>UTF-8</encoding>
                    <inputEncoding>UTF-8</inputEncoding>
                    <outputEncoding>UTF-8</outputEncoding>
                    <argLine>-Xms256m -Xmx512m -XX:MaxPermSize=128m -ea
                        -Dfile.encoding=UTF-8</argLine>
                     </configuration>
            </plugin>

这是 Jacoco 插件的样子。

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.0</version>
                <configuration>
                    <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
                    <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                        <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

最佳答案

引用 http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html :

If your project already defines VM arguments for test execution, be sure that they will include property defined by JaCoCo.

One of the ways to do this in case of maven-surefire-plugin - is to use syntax for late property evaluation:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <argLine>@{argLine} -your -extra -arguments</argLine>
  </configuration>
</plugin>

Another way is to define "argLine" as a Maven property rather than as part of the configuration of maven-surefire-plugin:

<properties>
  <argLine>-your -extra -arguments</argLine>
</properties>
...
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <!-- no argLine here -->
  </configuration>
</plugin>

所以要么定义

<argLine>-Xms256m -Xmx512m -XX:MaxPermSize=128m -ea
                    -Dfile.encoding=UTF-8</argLine>

作为属性:

<build>
  <properties>
    <argLine>-Xms256m -Xmx512m -XX:MaxPermSize=128m -ea
                    -Dfile.encoding=UTF-8</argLine>
  </properties>
...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.10</version>
    <configuration>
      <!-- no argLine here -->

或者添加@{argLine}:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.10</version>
  <configuration>
    <argLine>@{argLine} -Xms256m -Xmx512m -XX:MaxPermSize=128m -ea
                    -Dfile.encoding=UTF-8</argLine>

关于maven - Jacoco 不生成覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49581414/

相关文章:

maven - 无法更改 Maven 本地存储库路径

java - 如何在java中执行cURL语句从kibana获取JSON

java - jacoco 是否检测到未使用的类

java - SpringJUnit4ClassRunner 在 Spring5 中损坏了吗?

maven - 如何重定向 maven surefire 插件 stdout*deferred 临时文件远离/tmp 文件夹

java - maven-release-plugin 部署目标在 git 存储库中具有顶级文件夹的项目上失败

java - Maven + Vert.x - "package junit.framework does not exist'

intellij-idea - Intellij 的 'all classes in scope' 没有覆盖

android - 使用 testCoverageEnabled = true 运行 Android 测试时出现 java.lang.VerifyError

java - Surefire Maven 插件 : "Corrupted STDOUT by directly writing to native stream in forked JVM"