Integrating findbugs into a Play framework 2 project (Java)

This post shows how to integrate findbugs into a Play framework 2 / Java project.

Play 2.0 uses sbt (simple-build-tool) for build management, so this should allow to run findbugs.

An sbt plugin that integrates findbugs is findbugs4sbt by @johofer. Unfortunately, this was not available for sbt 0.11 which is used by Play 2.0 (sbt 0.11.3 to be precise).

So I created a fork of findbugs4sbt for sbt 0.11 (in branch sbt-0.11.x), which produces the needed sbt findbugs plugin.

Update: These changes are now merged into the main findbugs4sbt plugin.

As the plugin is not published to any public maven repo, the normal procedure would be to run sbt publish-local and configure the plugin in project/plugins.sbt. So that not everyone has to do this, I uploaded to resulting plugin jar to bitbucket so that you can just download the jar and put it into project/lib/. Then it’s picked up by sbt automatically and doesn’t need to be configured in project/plugins.sbt.

Update: The findbugs4sbt version for sbt 0.11.3 is now also available in the sonatype maven repo. So the plugin can now be added in project/plugins.sbt via

addSbtPlugin("de.johoop" % "findbugs4sbt" % "1.1.7")

and you don’t have to download and put the plugin jar into project/lib/.

To use the findbugs plugin in your Play/sbt project it has to be configured in project/Build.scala:

import de.johoop.findbugs4sbt.FindBugs._

object ApplicationBuild extends Build {

  val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA,
    settings = Defaults.defaultSettings ++ findbugsSettings)

This allows running

sbt findbugs

and puts the findbugs.xml report into target/scala-2.9.1/findbugs/.

As playframework compiles resources like routes and views to scala classes they will also be analyzed by findbugs and reported with some warnings (nothing serious, only issues regarding naming conventions). To exclude them from the findbugs analysis the findbugs4sbt configuration in Build.scala can be extended like this:

val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA,
  settings = Defaults.defaultSettings ++ findbugsSettings).settings(
    findbugsExcludeFilters := Some(
        <!-- See docs/examples at -->
          <Class name="~views\.html\..*"/>
          <Class name="~Routes.*"/>
          <Class name="~controllers\.routes.*"/>

For further configuration you should check out the findbugs4sbt documentattion.