如何使用Jacoco检查多模块Maven项目的最小代码覆盖率? [英] How to check minimum code coverage for multi-module maven project with jacoco?
问题描述
我想使用jacoco maven plugin
在使用'check'
目标的构建过程中检查代码覆盖的最低级别.
I want to use jacoco maven plugin
for checking minimum level of code coverage during build process using 'check'
goal.
对于单模块项目,一切正常.
但是对于多模块,我想检查所有模块的平均代码覆盖率,但是check
目标分别检查每个模块.
For one-module project everything works fine.
But for multi-module I want to check average level of code coverage from all modules, but check
goal checks every module separately.
例如,模块1 具有 70%的代码覆盖率,模块2 具有 100%的代码覆盖率,在两个模块中所有行的代码覆盖率平均值为 85%. 而且我正在尝试将所有项目的代码覆盖率设置为80%,但是由于第一个模块,它失败了.
For example, module1 has 70% of code coverage, module2 has 100% code coverage, in average for all lines from both modules code coverage is 85%. And I am trying to set code coverage for all project to 80%, but it fails because of first module.
从pom:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</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>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
推荐答案
简短的回答:不可能(在撰写本文时)仅使用Maven和Jacoco.
Short answer: not possible (at the time of writing) using merely Maven and Jacoco.
JaCoCo Maven当前的目标仅在单个模块上起作用:测试在模块内执行,并且仅覆盖同一模块内的代码.为每个模块分别创建覆盖率报告.没有对跨模块覆盖率的内置支持,也没有对多个模块的合并报告的支持.
The current JaCoCo Maven goals work on single modules only: Tests are executed within the module and contribute coverage only to code within the same module. Coverage reports are created for each module separately. There is no built-in support for cross-module coverage or combined reports for multiple modules.
因此,仅使用Maven和Jacoco不能满足您的要求.但是,您可以在企业设置中使用一种通用方法:Sonarqube,它将处理生成的jacoco文件(即jacoco.exec
),并通过其Jacoco集成(最新版本提供的开箱即用)来汇总报告和治理.
Hence your requirements cannot be met merely using Maven and Jacoco. You can however use a common approach in enterprise settings: Sonarqube, which will process the generated jacoco files (i.e. jacoco.exec
) and aggregate reporting and governance via its Jacoco integration (provided out-of-the-box on its latest versions).
这篇关于如何使用Jacoco检查多模块Maven项目的最小代码覆盖率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!