Use Maven to fail builds on code quality

Submitted by Barnaby Golden on Wed, 10/08/2011 - 09:57

I'm a great believer in getting quality in as early as possible in the development cycle. Checks that run automatically in an IDE are best (for example the Checkstyle/PMD plug-ins in Eclipse).

But these have one drawback: they are not enforced.

One approach I have used successfully is to enforce code quality by failing the build if quality falls below a threshold. For example, in Maven builds I usually fail the build on Code Coverage, Checkstyle errors and PMD errors. This approach takes some tuning. You need to set the thresholds at appropriate levels, for example I set my code coverage threshold at 60%. You will also need to tune your Checkstyle and PMD configuration files so that the more pedantic checks do not drive you insane.

As an example of this approach, here is a sample POM file:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>mygroup</groupId>
<artifactId>myartifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myartifact</name>
<url>http://maven.apache.org</url>

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
 <plugins>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-pmd-plugin</artifactId>
   <version>2.5</version>
   <configuration>
    <targetJdk>1.6</targetJdk>
    <rulesets>
     <ruleset>${basedir}\resources\barnaby-pmd-config.xml</ruleset>
    </rulesets>
    <excludes>
     <exclude>**/HelpMojo.java</exclude>
    </excludes>
    <failurePriority>4</failurePriority>
    <verbose>false</verbose>
   </configuration>
   <executions>
    <execution>
    <phase>verify</phase>
    <goals>
     <goal>check</goal>
     <goal>cpd-check</goal>
    </goals>
    </execution>
   </executions>
  </plugin>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-checkstyle-plugin</artifactId>
   <version>2.6</version>
   <configuration>
    <failsOnError>true</failsOnError>
    <violationSeverity>warning</violationSeverity>
    <consoleOutput>true</consoleOutput>
    <configLocation>${basedir}\src\main\resources\barnaby-checkstyle-config.xml</configLocation>
   </configuration>
   <executions>
    <execution>
    <phase>verify</phase>
    <goals>
     <goal>check</goal>
    </goals>
    </execution>
   </executions>
  </plugin>
  <plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>cobertura-maven-plugin</artifactId>
   <version>2.4</version>
   <configuration>
    <check>
    <haltOnFailure>true</haltOnFailure>
    <branchRate>60</branchRate>
    <lineRate>60</lineRate>
    <totalBranchRate>60</totalBranchRate>
    <totalLineRate>60</totalLineRate>
    <packageLineRate>60</packageLineRate>
    <packageBranchRate>60</packageBranchRate>
    </check>
   </configuration>
   <executions>
    <execution>
    <phase>verify</phase>
    <goals>
     <goal>clean</goal>
     <goal>check</goal>
    </goals>
    </execution>
   </executions>
  </plugin>
 </plugins>
</build>

<reporting>
 <plugins>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-pmd-plugin</artifactId>
   <version>2.5</version>
   <configuration>
    <targetJdk>1.6</targetJdk>
    <rulesets>
     <ruleset>${basedir}\src\main\resources\barnaby-pmd-config.xml</ruleset>
    </rulesets>
    <excludes>
     <exclude>**/HelpMojo.java</exclude>
    </excludes>
   </configuration>
  </plugin>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-checkstyle-plugin</artifactId>
   <version>2.6</version>
   <configuration>
    <configLocation>${basedir}\src\main\resources\barnaby-checkstyle-config.xml</configLocation>
   </configuration>
  </plugin>
  <plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>cobertura-maven-plugin</artifactId>
   <version>2.4</version>
  </plugin>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-javadoc-plugin</artifactId>
   <version>2.7</version>
   <configuration>
   </configuration>
  </plugin>
 </plugins>
</reporting>

</project>

Fail builds on code quality to ensure you bake quality in to your product.


 

Target Audience

© Copyright 2018 bhgagile Limited

Paperblog

Software Development Blogs - BlogCatalog Blog Directory