9

如果线路或分支覆盖率低于给定阈值,我正在尝试将我的 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 文档,但没有运气(至少可以说后者很少)。

4

3 回答 3

17

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

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

实际上,我已经<execution>在我的 pom.xml 中添加了一个元素。我可能误解了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>

在使用上述插件配置修补mvn clean install的新生成的 maven 项目(带有 )上运行时,我得到以下结果: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 并停止了构建。我看不出与您的插件配置有任何区别,我无法重现您描述的行为。

于 2009-10-24T15:06:37.870 回答
3

将以下内容添加到 <check/> 配置中。

<haltOnFailure>真</haltOnFailure>
于 2008-10-13T08:31:11.977 回答
-1

mvn clean install -Dcobertura.skip=true

于 2018-07-03T12:49:34.870 回答