【问题标题】:Maven Jacoco Code coverage plugin in parent pom does not check minimum coverage父 pom 中的 Maven Jacoco 代码覆盖率插件不检查最小覆盖率
【发布时间】:2018-04-07 17:59:03
【问题描述】:

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

Jacoco 插件在子 pom 中配置时构建(子项目)失败但在父 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>

儿童玩具

    <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配置失败构建(子项目)。

提前致谢。

【问题讨论】:

    标签: jacoco-maven-plugin parent-pom


    【解决方案1】:

    很遗憾,JaCoCo 没有提供对整个多模块项目进行聚合检查的方法。它仅适用于单个模块。

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-11
    • 2019-01-02
    • 2012-11-02
    • 2017-05-31
    • 2014-11-09
    • 2016-06-03
    • 1970-01-01
    相关资源
    最近更新 更多