FindBugs is a tool for finding "bug patterns" in Java code by analysis of compiled .class files.
To install FindBugs for use in StackyHack, download the .zip file containing the latest release from the FindBugs home page into a directory such as "java-lib", and unzip it. Then, define an environment variable called FINDBUGS_HOME to point to the distribution directory. For example, "c:\java-lib\findbugs-1.0.0". (Note that the file path should have no embedded spaces in it.)
Example 4.25, “findbugs.build.xml” shows the findbugs.build.xml file, which defines the Ant targets used to invoke the FindBugs tool and the FindBugs sensor.
Example 4.25. findbugs.build.xml
<project name="stackyhack.findbugs" default="findbugs">
<description>
Provides the FindBugs tool and the Hackystat FindBugs sensor.
</description>
<import file="build.xml"/>
<property environment="env"/>
<property name="findbugs.dir" location="${build.dir}/findbugs" />
<target name="findbugs" depends="findbugs.tool, findbugs.report, findbugs.sensor" description="Runs the FindBugs tool, report, and sensor."/>
<target name="findbugs.tool" depends="compile"
description="Runs FindBugs over code to check for problems.">
<!-- Fail this target if FindBugs is not installed. -->
<available file="${env.FINDBUGS_HOME}/lib/findbugs.jar" property="findbugs.available"/>
<fail unless="findbugs.available" message="Error: FINDBUGS_HOME not set or findbugs.jar not found."/>
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpath="${env.FINDBUGS_HOME}/lib/findbugs-ant.jar" />
<!-- Run FindBugs. -->
<mkdir dir="${findbugs.dir}"/>
<findbugs
home="${env.FINDBUGS_HOME}"
failOnError="${findbugs.failOnError}"
warningsProperty="findbugs.warningsfound"
output="xml:withMessages"
outputFile="${findbugs.dir}/findbugs.xml" >
<auxClasspath>
<fileset file="${env.JUNIT_HOME}/junit-4.1.jar"/>
</auxClasspath>
<sourcePath>
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
</sourcePath>
<class location="${build.dir}/classes" />
</findbugs>
</target>
<target name="findbugs.report" description="Generate an HTML report on FindBugs.">
<xslt in="${findbugs.dir}/findbugs.xml"
style="${env.FINDBUGS_HOME}/src/xsl/default.xsl"
out="${findbugs.dir}/findbugs-default.html" />
</target>
<target name="findbugs.sensor" description="Sends CodeIssue data to Hackystat using the Findbugs sensor.">
<!-- Define the findbugs sensor taskdef, failing the build if the sensor is not installed. -->
<available classname="org.hackystat.sensor.findbugs.FindBugsSensor" property="findbugs.sensor.available"/>
<fail unless="findbugs.sensor.available" message="Error: Findbugs sensor not installed."/>
<taskdef name="hacky-findbugs" classname="org.hackystat.sensor.findbugs.FindBugsSensor" />
<!-- Send the code issue information to the hackystat server. -->
<hacky-findbugs verbose="${hackystat.verbose.mode}">
<fileset file="${findbugs.dir}/findbugs.xml"/>
</hacky-findbugs>
</target>
</project>
The findbugs.build.xml defines the <findbugs.tool> target to parse the source code and generate an XML file called findbugs.xml containing a report on the "bug patterns" encountered. Note how the <sourcePath> element must contain a <fileset> that specifies the actual Java files to be processed. This is necessary for the FindBugs sensor to process the resulting XML file correctly. If your <sourcePath> element simply indicates the directory containing the Java files, for example, the sensor will not work. The <findbugs.report> target converts the XML file into HTML. The <findbugs.sensor> target uses the Hackystat FindBugs sensor to read in the findbugs.xml file generated by FindBugs and send the CodeIssue data to Hackystat. The <findbugs> target simply invokes these three targets in sequence.
Example 4.26, “findbugs.tool invocation” shows the output from invoking the <findbugs.tool> target.
Example 4.26. findbugs.tool invocation
C:\svn-csdl\StackyHack>ant -f findbugs.build.xml findbugs.tool Buildfile: findbugs.build.xml compile: findbugs.tool: [findbugs] Running FindBugs... [findbugs] Warnings generated: 1 [findbugs] Output saved to C:\svn-csdl\StackyHack\build\findbugs/findbugs.xml BUILD SUCCESSFUL Total time: 3 seconds
After FindBugs has been invoked to generate data regarding source code violations, the FindBugs sensor can be invoked to read in the resulting XML file produced by FindBugs and send the resulting data to Hackystat as Code Issue sensor data.
To accomplish this, you must first install the FindBugs sensor, which can process the XML files output by FindBugs. General instructions on installing sensors are provided in Chapter 2, Client-side configuration: Tool sensor installation; instructions for specific sensors are provided in Chapter 26, Sensors. After installation, your hackyInstaller FindBugs configuration window should look something like the screen image in Figure 4.16, “ HackyInstaller configuration screen for FindBugs sensor ”.
Example 4.27, “findbugs.sensor invocation” illustrates the invocation of the FindBugs sensor.
Example 4.27. findbugs.sensor invocation
C:\svn-csdl\StackyHack>ant -f findbugs.build.xml findbugs.sensor Buildfile: findbugs.build.xml findbugs.sensor: [hacky-findbugs] Sensor enabled?: true [hacky-findbugs] Processing file: C:\svn-csdl\StackyHack\build\findbugs\findbugs.xml [hacky-findbugs] Hackystat data on 1 Code Issues sent to http://hackystat.ics.hawaii.edu/ (1 secs.) BUILD SUCCESSFUL Total time: 2 seconds
Once you've invoked the sensor, you can verify that this data was received at the server by using the List Sensor Data command. Figure 4.17, “ List Sensor Data with StackyHack FindBugs data ” illustrates what this page might look like after receiving the StackyHack data.
A separate CodeIssue sensor data entry is sent for each of the violations detected by FindBugs, along with supplemental information about the file it was found in and so forth.