Popular Posts

Saturday, April 4, 2009

How to add FindBugs maven plugin to your project

FindBugs is a very useful static analyzer which inspects java bytecode for bug patterns. Static code inspection is important element in a good continuous integration process. Integrating FindBugs to your Maven build system is extremely simple as follows.

1. Add FindBugs maven2 plugin to the root pom of your maven project. You can add the plugin configuration element as a child of <reporting> element.

<reporting>

<plugins>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>findbugs-maven-plugin</artifactId>

<version>2.0</version>

<configuration>

<xmlOutput>true</xmlOutput>

<xmlOutputDirectory>C:\projects</xmlOutputDirectory>

</configuration>

</plugin>

</plugins>

</reporting>

Note: The xmlOutputDiectory is hard coded intentionally for demonstration purpose.

2. Go to the directory where the above pom.xml is placed and issue the following command.
mvn findbugs:findbugs

This will generate an XML report in the specified output directory. Here is an excerpt from such report.

<file classname="org.test.ExampleService">

<BugInstance type="OBL_UNSATISFIED_OBLIGATION" priority="Normal" category="EXPERIMENTAL" message="OBL: Method org.test.ExampleService.archiveFile(String, String) may fail to clean up stream or resource of type java.io.InputStream" lineNumber="69" />

<BugInstance type="OBL_UNSATISFIED_OBLIGATION" priority="Normal" category="EXPERIMENTAL" message="OBL: Method org.test.utils.ArchiveManipulator.extract(String, String) may fail to clean up stream or resource of type java.io.InputStream" lineNumber="114" />

<BugInstance type="OS_OPEN_STREAM" priority="Normal" category="BAD_PRACTICE" message="OS: org.test.utils.ArchiveManipulator.extractFromStream(InputStream, String) may fail to close stream" lineNumber="148" />

-------

<BugInstance type="RV_RETURN_VALUE_IGNORED_BAD_PRACTICE" priority="Normal" category="BAD_PRACTICE" message="RV: org.test.utils.ArchiveManipulator.extractFromStream(InputStream, String) ignores exceptional return value of java.io.File.mkdirs()" lineNumber="124" />

</file>

Thats all! For more information about plugin usage and configuration parameters, havea look at the plugin home page.

No comments: