2

I have parent pom which includes all dependencies which will be used by multiple child project. I also configured plugins in parent pom to be extended to child project. I also wanted the Jacoco code coverage report to be generated and enforce build failure when the coverage is lesser than the minimum threshold.

The Jacoco plugin which fails the build(child project) when configured in child pom but does not fail the build of child project when configured in parent pom.

Parent 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>

The above pom setup does not fail the build of the child project.

If I include the jacoco plugin in child pom then the build fails if the coverage is less than the threshold.

Child Pom with jacoco 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>

How to ensure the Jacoco configuration set in parent pom fails the build(child project) when the coverage is less than the threshold.

Thanks in advance.

Rajkumar S
  • 273
  • 1
  • 2
  • 8

1 Answers1

0

It is very unfortunate that JaCoCo does not provide a way to make an aggregate check for the entire multi-module project. It works for the individual modules only.

If reporting is enough, though, "report-aggregate" goal can be used. Good example here: jacoco simple integration test solution

Other source: https://github.com/jacoco/jacoco/wiki/MavenMultiModule

Leonardo Campos
  • 158
  • 1
  • 4
  • BTW, may be repeated from https://stackoverflow.com/questions/38763343/how-to-check-minimum-code-coverage-for-multi-module-maven-project-with-jacoco – Leonardo Campos May 12 '18 at 18:26