如何让Cobertura失败M2构建以实现低代码覆盖率 [英] How to get Cobertura to fail M2 build for low code coverage

查看:152
本文介绍了如何让Cobertura失败M2构建以实现低代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果行或分支覆盖率低于给定阈值,我正在尝试将WAR项目构建配置为失败。我一直在使用优秀书籍 Java Power Tools 第455页上提供的配置,但没有成功。这是我的项目Maven 2 POM的相关片段:

 < build> 
...
< plugins>
< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> cobertura-maven-plugin< / artifactId>
< version> 2.2< / version>
< configuration>
< check>
<! - 每级门槛 - >
< lineRate> 80< / lineRate>
< branchRate> 80< / branchRate>
<! - 项目范围的阈值 - >
< totalLineRate> 90< / totalLineRate>
< totalBranchRate> 90< / totalBranchRate>
< / check>
< executions>
< execution>
< goals>
< goal> clean< / goal>
< goal> check< / goal>
< / goals>
< / execution>
< execution>
< id> coverage-tests< / id>
<! - 验证阶段发生在安装之前 - >
< phase>验证< / phase>
< goals>
< goal> clean< / goal>
< goal> check< / goal>
< / goals>
< / execution>
< / executions>
< instrumentation>
<排除>
< exclude> au / ** / *常数。*< / exclude>
< /排除>
< ignores>
< ignore> au / ** / *常数。*< / ignore>
< / ignores>
< / instrumentation>
< / configuration>
< / plugin>
...
< / plugins>
...
< / build>

正如我所说,报道报告工作正常,问题是安装目标不是'如果线或分支覆盖率低于我指定的阈值,则应该失败。有没有人有这个工作,如果有的话,你的POM是什么样的,你使用的是哪个版本的Cobertura和Maven?我正在使用Maven 2.0.9和Cobertura 2.2。



我尝试使用谷歌搜索和阅读Cobertura文档,但没有运气(后者至少可以说是稀疏的如果< haltOnFailure> $ c>元素设置为true并且任何指定的检查失败,然后Cobertura将导致构建失败这是您要求的。但实际上,如果你没有指定它,那么这个元素默认为 true ,所以你不必将它添加到你的配置检查。在任何覆盖阈值以下构建失败是(或至少应该是)默认行为。



编辑:我做了一些进一步测试, haltOnFailure 似乎按预期工作在我的环境上(Maven 2.2.1。以及插件的版本2.3,2.2,2.1,即Linux上的cobertura版本1.9.2,1.9,1.8)。我正在使用下面的结果更新此答案。



实际上,我添加了< execution> 我的朋友的元素。我可能会误解 cobertura:check 的文档部分它说默认绑定到生命周期阶段:验证 但是,没有< execution> 元素, cobertura:check 不是在我的构建的验证阶段触发。在我用于cobertura-maven-plugin的设置下方:

 < project> 
...
< build>
...
< plugins>
...
< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> cobertura-maven-plugin< / artifactId>
< version> 2.3< / version>
< configuration>
< check>
<! - < haltOnFailure> true< / haltOnFailure> - ><! - optional - >
<! - 每级门槛 - >
< lineRate> 80< / lineRate>
< branchRate> 80< / branchRate>
<! - 项目范围的阈值 - >
< totalLineRate> 90< / totalLineRate>
< totalBranchRate> 90< / totalBranchRate>
< / check>
< / configuration>
< executions>
< execution>
< phase>验证< / phase>
< goals>
<! - < goal> clean< / goal> - ><! - 如果取消注释,则有效 - >
< goal> check< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>
< / plugins>
< / build>
< / project>

运行 mvn clean install 使用上面提到的插件配置修补新生成的maven项目( mvn archetype:create ):

  $ mvn archetype:create -DgroupId = com.mycompany.samples -DartifactId = cobertura-haltonfailure-testcase 
...
$ mvn clean install
[INFO]扫描项目...
[INFO] ---------------------------------- --------------------------------------
[INFO]建筑物cobertura-haltonfailure- testcase
[INFO]任务段:[清理,安装]
[INFO] -------------------------- ----------------------------------------------
[INFO] [clean:clean {execution:default-clean}]
[INFO]删除目录/ home / pascal / Projects / cobertura-haltonfailure-testcase / target
[INFO] [resources:resources {执行:default-resources}]
[警告]使用平台编码(实际上是UTF-8)来复制已过滤的资源s,即构建依赖于平台!
[INFO]跳过不存在的resourceDirectory / home / pascal / Projects / cobertura-haltonfailure-testcase / src / main / resources
[INFO] [compiler:compile {execution:default-compile}]
[INFO]将1个源文件编译为/ home / pascal / Projects / cobertura-haltonfailure-testcase / target / classes
[INFO] [resources:testResources {execution:default-testResources}]
[警告]使用平台编码(实际上是UTF-8)来复制过滤后的资源,即构建依赖于平台!
[INFO]跳过不存在的resourceDirectory / home / pascal / Projects / cobertura-haltonfailure-testcase / src / test / resources
[INFO] [compiler:testCompile {execution:default-testCompile}]
[INFO]将1个源文件编译为/ home / pascal / Projects / cobertura-haltonfailure-testcase / target / test-classes
[INFO] [surefire:test {execution:default-test}]
[INFO] Surefire报告目录:/ home / pascal / Projects / cobertura-haltonfailure-testcase / target / surefire-reports

---------------- ---------------------------------------
TESTS
- -------------------------------------------------- ---
运行com.mycompany.samples.AppTest
测试运行:1,失败:0,错误:0,跳过:0,已过去时间:0.09秒

结果:

测试运行:1,失败:0,错误:0,跳过:0

[INFO] [jar:jar {execution:default-jar}]
[INFO]建筑罐子:/home/pascal/Projects/cobertura-haltonfailure-testcase/target/cobertura-haltonfailure-testcase-1.0- SNAPSHOT.jar
[INFO]准备cobertura:check
[警告]删除:从分叉生命周期检查,以防止递归调用。
[INFO] [resources:resources {execution:default-resources}]
[警告]使用平台编码(实际上是UTF-8)来复制过滤后的资源,即构建依赖于平台!
[INFO]跳过不存在的resourceDirectory / home / pascal / Projects / cobertura-haltonfailure-testcase / src / main / resources
[INFO] [compiler:compile {execution:default-compile}]
[INFO]无需编译 - 所有类都是最新的
[INFO] [cobertura:instrument {执行:默认}]
[INFO] Cobertura 1.9.2 - GNU GPL许可证(无担保) - 请参阅COPYRIGHT文件
将1个文件检测到/ home / pascal / Projects / cobertura-haltonfailure-testcase / target / generated-classes / cobertura
Cobertura:保存1个类的信息。
工具时间:337ms

[INFO]仪表成功。
[INFO] [resources:testResources {execution:default-testResources}]
[警告]使用平台编码(实际上是UTF-8)来复制过滤后的资源,即构建依赖于平台!
[INFO]跳过不存在的resourceDirectory / home / pascal / Projects / cobertura-haltonfailure-testcase / src / test / resources
[INFO] [compiler:testCompile {execution:default-testCompile}]
[INFO]无需编译 - 所有类都是最新的
[INFO] [surefire:test {execution:default-test}]
[INFO] Surefire报告目录:/ home / pascal / Projects / cobertura-haltonfailure-testcase / target / surefire-reports

----------------------------- --------------------------
TESTS
--------------- ----------------------------------------
运行com.mycompany.samples .AppTest
测试运行:1,失败:0,错误:0,跳过:0,已过去时间:0.098秒

结果:

测试运行:1 ,失败:0,错误:0,跳过:0

[INFO] [cobertura:check {执行:默认}]
[INFO] Cobertura 1.9.2 - GNU GPL许可证(否)保修) - 请参阅COPYRIGHT文件
Cobertura:1类课程的已加载信息。

[错误] com.mycompany.samples.App检查失败。线路覆盖率0.0%低于80.0%
项目未通过检查。总线路覆盖率0.0%低于90.0%

[INFO] ---------------------------- --------------------------------------------
[错误] BUILD ERROR
[INFO] ---------------------------------------- --------------------------------
[INFO]覆盖率检查失败。见上面的消息。
[INFO] ------------------------------------------- -----------------------------
[INFO]有关更多信息,请使用-e开关$ b $运行Maven b [INFO] ---------------------------------------------- --------------------------
[INFO]总时间:18秒
[INFO]完成时间:周六10月24 21:00:39 CEST 2009
[INFO]最终记忆:17M / 70M
[INFO] ---------------------- --------------------------------------------------
$

我没有用maven 2.0.9测试,但在我的机器上, haltOnFailure 生成一个BUILD ERROR并停止构建。我没有看到您的插件配置有任何差异,我无法重现您描述的行为。


I'm trying to configure my WAR project build to fail if the line or branch coverage is below given thresholds. I've been using the configuration provided on page 455 of the excellent book Java Power Tools, but with no success. Here's the relevant snippet of my project's Maven 2 POM:

<build>
...
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <check>
        <!-- Per-class thresholds -->
        <lineRate>80</lineRate>
        <branchRate>80</branchRate>
        <!-- Project-wide thresholds -->
        <totalLineRate>90</totalLineRate>
        <totalBranchRate>90</totalBranchRate>
      </check>
      <executions>
        <execution>
          <goals>
            <goal>clean</goal>
            <goal>check</goal>
          </goals>
        </execution>
        <execution>
          <id>coverage-tests</id>
          <!-- The "verify" phase occurs just before "install" -->
          <phase>verify</phase>
          <goals>
            <goal>clean</goal>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
      <instrumentation>
        <excludes>
      <exclude>au/**/*Constants.*</exclude>
        </excludes>
        <ignores>
      <ignore>au/**/*Constants.*</ignore>
        </ignores>
      </instrumentation>
    </configuration>
  </plugin>
  ...
</plugins>
...
</build>

As I say, the coverage report works fine, the problem is that the "install" goal isn't failing as it should if the line or branch coverage is below my specified thresholds. Does anyone have this working, and if so, what does your POM look like and which version of Cobertura and Maven are you using? I'm using Maven 2.0.9 and Cobertura 2.2.

I've tried Googling and reading the Cobertura docs, but no luck (the latter are sparse to say the least).

解决方案

To my knowledge, if the <haltOnFailure> element is set to true and any of the specified checks fails, then Cobertura will cause the build to fail which is what you're asking for. But actually, this element defaults to true if you do not specify it so you don't have to add it to your configuration checks. Failing the build below any coverage threshold is (or at least should be) the default behavior.

EDIT: I did some further testing and haltOnFailure seems to be working as expected on my environment (Maven 2.2.1. and versions 2.3, 2.2, 2.1 of the plugin i.e. versions 1.9.2, 1.9, 1.8 of cobertura on Linux). I'm updating this answer with the result below.

Actually, I've added an <execution> element to my pom. I may be misinterpreting the part of cobertura:check's documentation that says it "Binds by default to the lifecycle phase: verify" but, without the <execution> element, cobertura:check wasn't triggered during the verify phase of my build. Below the setup I've use for the cobertura-maven-plugin:

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <check>
            <!--<haltOnFailure>true</haltOnFailure>--><!-- optional -->
            <!-- Per-class thresholds -->
            <lineRate>80</lineRate>
            <branchRate>80</branchRate>
            <!-- Project-wide thresholds -->
            <totalLineRate>90</totalLineRate>
            <totalBranchRate>90</totalBranchRate>
          </check>
        </configuration>
        <executions>
          <execution>
            <phase>verify</phase>
            <goals>
              <!--<goal>clean</goal>--><!-- works if uncommented -->
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

I get the following result when running mvn clean install on a freshly generated maven project (with mvn archetype:create) patched with the plugin configuration mentioned above:

$ mvn archetype:create -DgroupId=com.mycompany.samples -DartifactId=cobertura-haltonfailure-testcase
...
$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building cobertura-haltonfailure-testcase
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory /home/pascal/Projects/cobertura-haltonfailure-testcase/target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.samples.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.09 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/cobertura-haltonfailure-testcase-1.0-SNAPSHOT.jar
[INFO] Preparing cobertura:check
[WARNING] Removing: check from forked lifecycle, to prevent recursive invocation.
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [cobertura:instrument {execution: default}]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Instrumenting 1 file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/generated-classes/cobertura
Cobertura: Saved information on 1 classes.
Instrument time: 337ms

[INFO] Instrumentation was successful.
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.samples.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.098 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [cobertura:check {execution: default}]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 1 classes.

[ERROR] com.mycompany.samples.App failed check. Line coverage rate of 0.0% is below 80.0%
Project failed check. Total line coverage rate of 0.0% is below 90.0%

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Coverage check failed. See messages above.
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18 seconds
[INFO] Finished at: Sat Oct 24 21:00:39 CEST 2009
[INFO] Final Memory: 17M/70M
[INFO] ------------------------------------------------------------------------
$ 

I didn't test with maven 2.0.9, but on my machine, haltOnFailure generates a BUILD ERROR and halt the build. I don't see any differences with your plugin configuration, I can't reproduce the behavior you describe.

这篇关于如何让Cobertura失败M2构建以实现低代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆