父pom中的Maven Jacoco Code覆盖插件不会检查最小覆盖范围

时间:2017-10-26 11:14:45

标签: jacoco-maven-plugin parent-pom

我有父pom,其中包含多个子项目将使用的所有依赖项。我还在父pom中配置了插件以扩展到子项目。 我还希望生成Jacoco代码覆盖率报告,并在覆盖率小于最小阈值时强制构建失败。

在子pom 中配置时,构建(子项目)失败的Jacoco插件,但在父pom中配置时不会使子项目的构建失败

家长pom

<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>microservice</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ms-parent</name>
<description>Parent pom will include all the dependencies commonly used 
across the microservices    </description>
<properties>
    <spring.version>4.3.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>

</dependencies>
<build>

  <finalName>ParentPOM</finalName>
  <pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.9</version>
        <configuration>
            <useProjectReferences>false</useProjectReferences>
        </configuration>
    </plugin>
    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
               <systemPropertyVariables>
                <jacoco-agent.destfile> 
                  ${project.build.directory}/coverage.exec
                </jacoco-agent.destfile>
               </systemPropertyVariables>
            </configuration>
        </plugin> 

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.7.201606060606</version>

            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals><goal>prepare-agent</goal></goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <phase>prepare-package</phase>
                    <goals><goal>report</goal></goals>
                </execution>
                <execution>
        <id>check</id>
        <goals>
            <goal>check</goal>
        </goals>
        <configuration><rules><rule>
         <element>CLASS</element>
         <limits>
          <limit>
            <counter>LINE</counter>
            <value>COVEREDRATIO</value>
            <minimum>0.99</minimum>
          </limit>
          <limit>
            <counter>BRANCH</counter>
            <value>COVEREDRATIO</value>
            <minimum>0.99</minimum>
          </limit>
        </limits>
        <excludes>

        </excludes>
      </rule></rules></configuration>
    </execution>
            </executions>
        </plugin>
</plugins>
</pluginManagement>
</build>

</project>

Child Pom

    <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>microservice</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>

  </parent>
  <groupId>childmodule</groupId>
  <artifactId>module</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>cipher Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>

  </dependencies>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <!-- inherits config from parent: can override if required -->
        </plugin>

    </plugins>
</build>
</project>

上面的pom设置不会使子项目的构建失败。

如果我在子pom中包含jacoco插件,那么如果覆盖率低于阈值,则构建将失败。

使用jacoco插件的儿童Pom

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <systemPropertyVariables>
        <jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
        </systemPropertyVariables>
    </configuration>
</plugin> 

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.7.201606060606</version>

    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals><goal>prepare-agent</goal></goals>
        </execution>
        <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <goals><goal>report</goal></goals>
        </execution>
        <execution>
            <id>check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration><rules><rule>
             <element>CLASS</element>
             <limits>
              <limit>
                <counter>LINE</counter>
                <value>COVEREDRATIO</value>
                <minimum>0.99</minimum>
              </limit>
              <limit>
                <counter>BRANCH</counter>
                <value>COVEREDRATIO</value>
                <minimum>0.99</minimum>
              </limit>
            </limits>
            <excludes>

            </excludes>
            </rule></rules></configuration>
        </execution>
    </executions>
</plugin>

当覆盖率低于阈值时,如何确保父pom中的Jacoco配置集失败了构建(子项目)。

提前致谢。

1 个答案:

答案 0 :(得分:0)

非常不幸的是,JaCoCo没有提供对整个多模块项目进行汇总检查的方法。它仅适用于各个模块。

如果报告足够,可以使用"report-aggregate"目标。 这里的好例子是:jacoco simple integration test solution

其他来源: https://github.com/jacoco/jacoco/wiki/MavenMultiModule