在缺少模块和没有单元测试的项目中,由于缺少执行数据文件而跳过JaCoCo执行

时间:2018-08-31 12:23:52

标签: maven jacoco jacoco-maven-plugin

如何创建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/

上下载了一个工作示例。

,它也有相同的问题。

1 个答案:

答案 0 :(得分:0)

给予

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中进行报告:

report for class A

并在b/target/site/jacoco中进行报告:

report for class B