RSS
Rajender Aggarwal

Maven Integration with PMD & FindBugs

Wed, Jan 4, 2012

Rajender Aggarwal

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

In this blog, we will look at integration of Maven with PMD and FindBugs. Apart from PMS and FindBugs, there are various other tools such as Surefire and Corbetura that can be integrated with Maven. Lets look at them …

  • Maven Surefire Report Plugin : Generates the test results reports (TEST-*.xml) into HTML format.
  • Maven Corbetura Report Plugin : The Cobertura tool is a free and easy to use source code test coverage analyser. It helps you to discover where your source-code lacks in test coverage.
  • Maven PMD Plugin : Runs the PMD code analysis tool on your project’s source code and generate a site report with its results.
  • Maven FindBugs Report Plugin : FindBugs uses static analysis to inspect Java bytecode for occurrences of bug patterns. FindBugs is better in finding Threading issues than PMD.

Configuring PMD with Maven

At the PMD site there is comprehensive documentation available on what PMD will report about. In PMD there are rules and rulesets. Rulesets are groups of rules and other rulesets. By default PMD provides a ruleset that is packaged with the plugin. To customize this you need to create your own ruleset like described on the PMD site.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<rulesets>
<ruleset>{Location of PMD Rulse Set File};/ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>pmd</goal>
</goals>
</execution>
</executions>
</plugin>
Configuring FindBugs with Maven

FindBugs looks for bugs in Java programs. It is based on the concept of bug patterns. A bug pattern is a code idiom that is often an error. Bug patterns arise for a variety of reasons:

  • Difficult language features
  • Misunderstood API methods
  • Misunderstood invariants when code is modified during maintenance
  • Garden variety mistakes: typos, use of the wrong boolean operator

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<xmlOutput>true</xmlOutput>
<!– Optional directory to put FindBugs xdoc xml report –>
<xmlOutputDirectory>target/site</xmlOutputDirectory>
<threshold>High</threshold>
</configuration>
</plugin>

To conclude, integrating and generating reports with Maven is a great way to monitor the quality of the code. However, to get the real value, one must think about which reports to choose and how to use them. Adding too many reports will lead to information overload and may create confusion over what to fix.

Popularity: 16% [?]

, ,

Leave a Reply