Skip to content

bld extension to perform static code analysis with SpotBugs

License

Notifications You must be signed in to change notification settings

rife2/bld-spotbugs

Repository files navigation

bld Extension to Perform Static Code Analysis with SpotBugs

License Java bld Release Snapshot GitHub CI

To install the latest version, add the following to the lib/bld/bld-wrapper.properties file:

bld.extension-pmd=com.uwyn.rife2:bld-spotbugs

For more information, please refer to the extensions documentation.

To install a binary distribution of SpotBugs please refer to its installation instruction.

Check Source with SpotBugs

To check for bugs in the main source code, add the following to your build file:

@BuildCommand(summary = "Runs SpotBugs on this project")
public void spotbugs() throws Exception {
    new SpotBugsOperation()
            .fromProject(this)
            .home("/path/to/spotbugs/")
            .execute();
}
./bld compile spotbugs

The output will look something like:

[spotbugs] auxclasspath[build/main, lib/compile/foo-2.3.0.jar, ...]
[spotbugs] sourcepath[src/main/java, src/main/resources]
[spotbugs] analyze[build/main]
[spotbugs] Found 5 potential bugs in 2 classes
[spotbugs] file:///dev/example/src/main/java/com/example/Example.java:39
    DCN_NULLPOINTER_EXCEPTION (https://spotbugs.readthedocs.io/en/latest/bugDescriptions.html#dcn-nullpointer-exception)
    Method: hasSpace, Class: com.example.Example, Priority: 2, Rank: 17, Category: STYLE
        --> NullPointerException caught
[spotbugs] file:///dev/example/src/main/java/com/example/Sample.java:27
    EI_EXPOSE_REP (https://spotbugs.readthedocs.io/en/latest/bugDescriptions.html#ei-expose-rep)
    Method: getList, Field: mutableList, Class: com.example.Sample, Priority: 2, Rank: 18, Category: MALICIOUS_CODE
        --> May expose internal representation by returning reference to mutable object
...

To also check the test source code, add the following to your build file:

@BuildCommand(summary = "Runs SpotBugs on this project")
public void spotbugs() throws Exception {
    new SpotBugsOperation()
            .fromProject(this, true) // check src/main and src/test
            .spotBugsJar("/path/to/spotbugs/lib/spotbugs.jar")
            .execute();
}
./bld compile spotbugs

Please check the SpotBugsOperation documentation for all available configuration options.