【问题标题】:Unable to run jacoco code coverage tool无法运行 jacoco 代码覆盖率工具
【发布时间】:2017-04-19 23:02:46
【问题描述】:

我有一个 maven 项目 (link),我想对其进行代码覆盖。

我在主项目 pom 文件上运行了命令mvn test -Pcoverage jacoco:prepare-agent jacoco:report,但没有生成报告。相反,我收到警告说

[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:report (post-test) @ pulsar-discovery-service ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:prepare-agent (default-cli) @ pulsar-discovery-service ---
[INFO] argLine set to -javaagent:/home/jai1/.m2/repository/org/jacoco/org.jacoco.agent/0.7.7.201606060606/org.jacoco.agent-0.7.7.201606060606-runtime.jar=destfile=/home/jai1/pulsar/pulsar-discovery-service/target/jacoco.exec
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:report (default-cli) @ pulsar-discovery-service ---
[INFO] Skipping JaCoCo execution due to missing execution data file.

有人可以建议我如何使用此pom file 生成代码覆盖率报告。我正在使用 apache-maven-3.3.9 和 testNG。

【问题讨论】:

标签: java maven code-coverage jacoco jacoco-maven-plugin


【解决方案1】:

Your pom.xml 包含

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <argLine> -Xmx2G -XX:MaxDirectMemorySize=8G
        -Dio.netty.leakDetectionLevel=advanced</argLine>
    </configuration>
  </plugin>

JaCoCo documentation for prepare-agent goal 状态

如果您的项目已经定义了用于测试执行的 VM 参数,请确保它们将包含 JaCoCo 定义的属性。

...将“argLine”定义为 Maven 属性,而不是 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>

【讨论】:

    【解决方案2】:

    以下对我们有用:

    设置一些属性:

    <properties>
        ...
        <!-- versions below are last ones that are Java 6 compatible -->
        <version.jacoco-agent>0.7.4.201502262128</version.jacoco-agent>
        <jacocoArgLine />
        <surefire.base.argLine>-XX:-UseSplitVerifier -XX:MaxPermSize=384m -Xmx1024m -Djava.awt.headless=true</surefire.base.argLine>
        ...
    </properties>
    

    coverage 个人资料:

        <profile>
            <id>coverage</id>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <version>${surefire.plugin.version}</version>
                            <configuration>
                                <argLine>${surefire.base.argLine} @{jacocoArgLine}</argLine>
                                <systemPropertyVariables>
                                    <java.awt.headless>true</java.awt.headless>
                                </systemPropertyVariables>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>prepare-agent</goal>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    

    【讨论】:

      【解决方案3】:
      It seems like you are missing below line in pom.xml 
      <destFile>
         ${project.build.directory}/coverage-reports/jacoco-unit.exec
      </destFile>
      

      请看下面的 pom :

       <plugin>
                 <groupId>org.jacoco</groupId>
                 <artifactId>jacoco-maven-plugin</artifactId>
                 <version>0.7.1.201405082137</version>
                  <executions>
                        <execution>
                            <id>prepare-jacoco-4-unit-tests</id>
                            <goals>
                                 <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
               <destFile>
                   ${project.build.directory}/coverage-reports/jacoco-unit.exec
               </destFile>                                             
               <propertyName>jacocoArgLine</propertyName>
               </configuration>
               </execution>
      
                  <execution>
                      <id>create-jacoco-reports-4-unit-tests</id>
                      <phase>prepare-package</phase>
                      <goals>
                          <goal>report</goal>
                      </goals>
                     <configuration>
                 <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>
                    ${project.build.directory}/coverage-reports/jacoco-unit.exec
                </dataFile>
                </configuration>
                </execution>
        </executions>
        </plugin>
      

      然后在 maven-surefire-plugin 中定义如下:

      <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-plugin</artifactId>
           <version>2.17</version>
           <configuration>
               <skipTests>${skipUnitTests}</skipTests>
               <argLine>${jacocoArgLine}</argLine>
           </configuration>
      </plugin>
      

      【讨论】:

      • 试过你在“github.com/jai1/pulsar/tree/temp”中所说的,但还是没有成功
      • 好的,让我尝试克隆您的项目并在本地尝试。谢谢。
      • 感谢您的宝贵时间 请注意——测试套件非常庞大,因此请在运行 mvn 作业之前删除测试(或仅保留一个),否则需要两个小时才能完成。寻找 。 -name "*Test.java" | xargs rm -f
      【解决方案4】:

      见下面一个完整的简单演示项目,展示了单元、集成、黄瓜测试。测试覆盖率通过 Jacoco 显示。

      • 多模块项目
      • 单元测试(通过 mvn clean install)
      • 集成测试(通过 mvn clean install -P integration-test)
      • Cucumber 测试(通过 mvn clean install) - 演示单元/it-testing
      • Jacoco - 测试覆盖率(聚合数据文件和聚合报告)
      • FindBugs - 代码质量

      享受sipmle demo project

      【讨论】:

      • 不错的尝试 - 但无法在项目 module3 上执行目标:无法解析项目 nl.deholtmans:module3:jar:1.0-SNAPSHOT 的依赖项:找不到工件 nl.deholtmans:module1:jar: 1.0-SNAPSHOT 我不赞成或反对
      猜你喜欢
      • 1970-01-01
      • 2018-01-06
      • 2013-02-15
      • 2016-11-11
      • 2019-01-02
      • 2012-11-02
      • 2014-11-09
      • 2018-05-28
      • 2012-06-11
      相关资源
      最近更新 更多