如何让Cobertura失败M2构建以实现低代码覆盖率

时间:2008-10-13 04:35:31

标签: java maven-2 build-automation code-coverage cobertura

如果行或分支覆盖率低于给定阈值,我正在尝试将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>
        <!-- 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>

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

我已经尝试使用谷歌搜索和阅读Cobertura文档,但没有运气(后者至少可以说是稀疏)。

3 个答案:

答案 0 :(得分:17)

据我所知,如果<haltOnFailure>元素设置为true且任何指定的检查失败,那么Cobertura将导致构建失败这就是你要求的。但实际上,如果您未指定该元素,则此元素默认为true,因此您无需将其添加到configuration checks。使构建低于任何覆盖阈值是(或至少应该是)默认行为。

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

实际上,我已经在我的pom中添加了<execution>元素。我可能会错误地解释cobertura:check文档中“默认绑定到生命周期阶段:verify ”的部分,但是没有<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 -->
            <!-- 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>

在使用上述插件配置修补新生成的maven项目(mvn clean install)上运行mvn archetype:create时,我得到以下结果:

$ 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] ------------------------------------------------------------------------
$ 

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

答案 1 :(得分:3)

将以下内容添加到&lt; check /&gt;配置。

<haltOnFailure>true</haltOnFailure>

答案 2 :(得分:-1)

mvn全新安装-Dcobertura.skip = true