Maven Jacoco插件未生成报告

时间:2018-07-30 07:45:57

标签: java maven code-coverage 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>
            <execution>
              <id>default-check</id>
              <goals>
                <goal>check</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
          <configuration>
            <source>1.5</source>
            <target>1.5</target>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.6.0</version>
          <configuration>
            <mainClass> yada yada yada </mainClass>
          </configuration>
          <executions>
            <execution>
              <id>run-selenium</id>
              <phase>integration-test</phase>
              <goals><goal>java</goal></goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.21.0</version>
          <configuration>
            <!-- nothing -->
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

执行时

$> mvn clean test jacoco:report

或者只是jacoco:report,我知道

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

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

不确定目前该怎么做...

2 个答案:

答案 0 :(得分:0)

检查插件

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.4.0.905</version>
        </plugin>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <configuration>
                <destFile>${sonar.jacoco.reportPath}</destFile>
                <append>true</append>
            </configuration>
            <executions>
                <execution>
                    <id>agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

仅添加此插件

 <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.9</version>
        <configuration>
            <destFile>${sonar.jacoco.reportPath}</destFile>
            <append>true</append>
        </configuration>
        <executions>
            <execution>
                <id>agent</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

答案 1 :(得分:0)

要使用JaCoCo记录覆盖率,您需要使用JaCoCo Java代理执行测试。

根据https://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html

  

准备一个指向JaCoCo运行时代理的属性,该属性可以作为VM参数传递给被测应用程序。

默认情况下,它设置argLine选择的属性maven-surefire-plugin,但是似乎您实际上并没有使用maven-surefire-plugin,而是使用了{{1} },

因此确保在测试期间使用JaCoCo代理,例如使用exec-maven-plugin的{​​{1}}目标并将argLine传递给它-请参见https://www.mojohaus.org/exec-maven-plugin/exec-mojo.html#arguments