Maven Jacoco 插件未生成报告

问题描述 投票:0回答:2

我一直在 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:报告,我明白了

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

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

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

java maven code-coverage jacoco
2个回答
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>

0
投票

要使用 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
目标,其中

因此确保在测试期间使用 JaCoCo 代理,例如使用

exec
的目标并将
exec-maven-plugin
传递给它 - 请参阅
https://www.mojohaus.org/exec-maven-plugin/exec-mojo.html#arguments

© www.soinside.com 2019 - 2024. All rights reserved.