猿问

Maven Jacoco 插件不生成报告

我一直在 Java Eclipse 中使用 Maven 项目为学校工作。我一直在使用 Eclemma 获取代码覆盖率数据,但我的教授想从命令行运行我的代码并从那里获取代码覆盖率报告。我一直在尝试让 Jacoco 工作,但我之前真的从未使用过 Maven 或 Pom.xmls,我很迷茫。这是我现在的样子


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId> yada yada yada </groupId>

  <artifactId> yada yada yada </artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.12</version>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>org.jacoco</groupId>

      <artifactId>org.jacoco.ant</artifactId>

      <version>0.8.1</version>

    </dependency>

  </dependencies>


  <build>

    <pluginManagement>

      <plugins>

        <plugin>

          <groupId>org.jacoco</groupId>

          <artifactId>jacoco-maven-plugin</artifactId>

          <version>0.8.1</version>

          <executions>

            <execution>

              <id>default-prepare-agent</id>

              <goals>

                <goal>prepare-agent</goal>

              </goals>

            </execution>


            <execution>

              <id>post-integration-test</id>

              <phase>post-integration-test</phase>

              <goals>

                <goal>report</goal>

              </goals>

              <configuration>


                <dataFile>target/jacoco.exec</dataFile>


                <outputDirectory>target/jacoco-ut</outputDirectory>

              </configuration>

            </execution>

当我执行


$> mvn clean test jacoco:report

或者只是jacoco:报告,我明白了


[INFO] --- jacoco-maven-plugin:0.8.1:report (default-cli) @ MyFileLocation ---


[INFO] Skipping JaCoCo execution due to missing execution data file.

不知道此时该怎么办...


HUX布斯
浏览 1682回答 2
2回答

BIG阳

要使用 JaCoCo 记录覆盖率,您需要使用 JaCoCo Java 代理执行测试。根据https://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html:准备一个指向 JaCoCo 运行时代理的属性,该代理可以作为 VM 参数传递给被测应用程序。默认情况下,它会设置argLine由 选择的属性maven-surefire-plugin,但是似乎您实际上maven-surefire-plugin并没有使用,而是使用了 的java目标exec-maven-plugin,其中首先不启动新的 JVM - 见https://www.mojohaus.org/exec-maven-plugin/java-mojo.html:在当前 VM 中执行提供的 java 类第二 - 不使用&nbsp;argLine因此,请确保在您的测试期间使用 JaCoCo 代理,例如使用exec目标exec-maven-plugin并传递argLine给它 - 请参阅https://www.mojohaus.org/exec-maven-plugin/exec-mojo.html#arguments

蛊毒传说

检查插件<plugins>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-compiler-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.2</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <source>1.8</source>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <target>1.8</target>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.sonarsource.scanner.maven</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>sonar-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.4.0.905</version>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jacoco</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jacoco-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>0.7.9</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <destFile>${sonar.jacoco.reportPath}</destFile>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <append>true</append>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>agent</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>prepare-agent</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins>仅添加此插件&nbsp;<plugin>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jacoco</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jacoco-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>0.7.9</version>&nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <destFile>${sonar.jacoco.reportPath}</destFile>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <append>true</append>&nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>agent</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>prepare-agent</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; </plugin>
随时随地看视频慕课网APP

相关分类

Java
我要回答