4

I'm using Cobertura with Maven.

I'd like the build to fail if the coverage is below a given threshold, but I would like the site (including the Cobertura report) to still be generated. This is because developers will need to refer to the coverage report to see where they can add more coverage to fix the failed build.

Currently my pom looks like:

<project>
  <build>
    ...
    <plugins>
        <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>cobertura-maven-plugin</artifactId>
      <version>${cobertura.version}</version>
      <configuration>
    <check>
      <totalLineRate>${cobertura.check.totalLineRate}</totalLineRate>
    </check>
      </configuration>
      <executions>
    <execution>
      <goals>
        <goal>clean</goal>
        <goal>check</goal>
      </goals>
    </execution>
      </executions>
    </plugin>
        ...
    </plugins>
  </build>
  <reporting>
    <plugins>
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>cobertura-maven-plugin</artifactId>
  <version>${cobertura.version}</version>
</plugin>
    ...
    </plugins>
  </reporting>
</project>

If I run mvn clean verify site, then it generates the HTML coverage report if the coverage target is met, but it fails the build without generating the report if the coverage target is not met. How can I change it to always generate the report?

Rich
  • 15,048
  • 2
  • 66
  • 119

2 Answers2

4

Instead of failing the build if the code coverage target isn't met, is it possible to set it to mark the build as unstable instead? I know there are ways to do this through the Jenkins CI server, but I'm not sure if it's possible to accomplish this through pom.xml. Then again, "unstable" builds might be more Jenkins-specific, and might not exist as a possibility only through your pom file.

Mike
  • 7,994
  • 5
  • 35
  • 44
  • I'm using Jenkins/Hudson, so that would be an acceptable solution for me. However, I don't know how to do that? – Rich Nov 03 '11 at 13:52
  • Install the [Cobertura](https://wiki.jenkins-ci.org/display/JENKINS/Cobertura+Plugin) and [Static Analysis](https://wiki.jenkins-ci.org/display/JENKINS/Static+Code+Analysis+Plug-ins) plugins, and then enable the Cobertura plugin for your project. There are configuration options once the plugin is enabled to "change the weather" based on different metrics. – Mike Nov 03 '11 at 14:20
  • This is working tolerably well. The Static Analysis plugin isn't picking up my findbugs or checkstlye reports, because I am building a multi-module pom in one Hudson build, and they don't seem to search in subdirs. The Cobertura one is working fine, as it allows a user-specified search path. – Rich Nov 04 '11 at 11:24
  • Do you have the Findbugs and Checkstyle plugins installed as well? Those integrate with the Static Analysis plugin, but the SA plugin doesn't automagically pick up FB/CS reports. While you're at it, you might also take a look into PMD. – Mike Nov 04 '11 at 13:04
  • Yes, I have those installed, but it's still not finding my findbugs reports (they're under "subproj1/target/findbugs" and "subproj2/target/findbugs", rather than "target/findbugs", which is where I imagine it's looking). I've given up on this now and installed the "M2 Extra Steps Plugin" and have written my own shell script to check the output and fail the build on any findbugs issues. – Rich Nov 09 '11 at 11:21
  • In your Jenkins project configuration, you should have the "Publish FindBugs Analysis Results" box checked. Its value should be `**/target/findbugs.xml` and not just `target/findbugs.xml`. The extra `**` tells it to look in all subdirectories that might be named `target` underneath the current dir, instead of just looking for a single subdir named `target` under cwd. In a Maven parent/sub-project relationship, this should pick up and aggregate all the findbugs analysis files. – Mike Nov 09 '11 at 12:01
0

A quick workaround: remove the check goal:

<executions>
    <execution>
        <goals>
            <goal>clean</goal>
        </goals>
    </execution>
</executions>

then run

mvn clean verify site cobertura:check

If you are using Hudson/Jenkins remove all checks from the pom.xml and install the Cobertura plugin to Hudson and configure the checks in the Hudson/Jenkins plugin.

palacsint
  • 28,416
  • 10
  • 82
  • 109