【问题标题】:Skipping JaCoCo execution due to missing execution data file, in a project with modules and no unit tests由于缺少执行数据文件,在具有模块且没有单元测试的项目中跳过 JaCoCo 执行
【发布时间】:2018-08-31 12:23:52
【问题描述】:

如何创建 jacoco.ext 文件?

哪个 mvn 查询?

我的项目在 pom.xml 中有这个

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <show>private</show>
                <nohelp>true</nohelp>
                <additionalparam>-Xdoclint:none</additionalparam>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <reuseForks>true</reuseForks>
                <forkCount>1</forkCount>

                <argLine>${argLine}</argLine>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.2</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>

                <execution>
                    <id>post-unit-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <!-- Sets the path to the file which contains the execution data. -->

                        <dataFile>target/jacoco.exec</dataFile>
                        <!-- Sets the output directory for the code coverage report. -->
                        <outputDirectory>target/jacoco-ut</outputDirectory>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
            </configuration>
        </plugin>



    </plugins>
</build>

项目本身没有单元测试。但它有 5 个带有单元测试的模块。

感谢您的帮助。我使用的是 jacoco maven 插件的 0.8.2 版本。

我也在这里检查了很多问题。

我还在https://www.mkyong.com/maven/jacoco-java-code-coverage-maven-example/上下载了一个工作示例

它也有同样的问题。

【问题讨论】:

    标签: maven jacoco jacoco-maven-plugin


    【解决方案1】:

    给定

    a/src/main/java/A.java:

    class A {
      A() {
        System.out.println("Hello from A");
      }
    }
    

    a/src/test/java/ATest.java:

    public class ATest {
      @org.junit.Test
      public void example() {
        new A();
      }
    }
    

    b/src/main/java/B.java:

    class B {
      B() {
        System.out.println("Hello from B");
      }
    }
    

    b/src/test/java/BTest.java:

    public class BTest {
      @org.junit.Test
      public void example() {
        new B();
      }
    }
    

    pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>org.example</groupId>
      <artifactId>example</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>pom</packaging>
    
      <modules>
        <module>a</module>
        <module>b</module>
      </modules>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.2</version>
            <executions>
              <execution>
                <id>default</id>
                <goals>
                  <goal>prepare-agent</goal>
                  <goal>report</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
    </project>
    

    a/pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <parent>
        <groupId>org.example</groupId>
        <artifactId>example</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
    
      <artifactId>a</artifactId>
    
    </project>
    

    b/pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <parent>
        <groupId>org.example</groupId>
        <artifactId>example</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
    
      <artifactId>b</artifactId>
    
    </project>
    

    mvn clean verify 的执行将产生 在a/target/site/jacoco报告:

    并在b/target/site/jacoco报告:

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 2021-05-29
      • 2022-06-14
      • 2016-07-18
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多