How to add Checkstyle and FindBugs plugins in a gradle based project

(Prior knowledge in Gradle, Checkstyle, FindBugs is required to understand this blog)

Currently I am working on a gradle based project in WSO2 and I met with a requirement where I wanted to add Checkstyle and FindBugs plugins to my gradle script. So as the first step(which everyone do normally :D) I googled about this and found some solutions which are NOT clearly describing the topic. So I thought It would be ideal if I write a blog on this.

What is done by the Checkstyle plugin?

The Checkstyle plugin performs quality checks on your project’s Java source files using Checkstyle and generates reports from these checks.

What is done by the FindBugs plugin

The FindBugs plugin performs quality checks on your project’s Java source files using FindBugs and generates reports from these checks.

Adding these plugins to gradle project is pretty much straight forward and a super easy task. You only need to add few lines to your gradle build script. :)

Let’s get started.

Adding Checkstyle to gradle script

1. Checkstyle plugin — project layout

By default, the Checkstyle plugin expects the following project layout, but this can be changed:

config/checkstyle/checkstyle.xml — Checkstyle configuration file

config/checkstyle — Other Checkstyle configuration files (e.g.,suppressions.xml)

An example for a checkstyle.xml can be found here. This is the checkstyle file used by WSO2.

2. Enabling Checkstyle plugin

Add the following lines to build.gradle file.

apply plugin: 'checkstyle'
checkstyle {
toolVersion '7.8.1'
configFile file("config/checkstyle/checkstyle.xml")
}
checkstyleMain {
source ='src/main/java'
}
checkstyleTest {
source ='src/test/java'
}

toolVersion— The version of the code quality tool to be used.

configFile — The Checkstyle configuration file to use. You need to define the path to the configuration file. For this you need to you file phrase in order to define the path to file.

More properties on the Checkstyle plugin can be found here.

The Checkstyle plugin adds the following tasks to the project:

checkstyleMain: Runs Checkstyle against the production Java source files.

checkstyleTest: Runs Checkstyle against the test Java source files.

checkstyleSourceSet : Runs Checkstyle against the given source set’s Java source files.

Under the source in every task, you need to define the source you need to apply the task. (For more info on Tasks click here.)

3. Customize(optional step) the HTML report generated by the Checkstyle task

If you want to customize(optional) the HTML report generated by the Checkstyle task, it can be done using a XSLT stylesheet, for example to highlight specific errors or change its appearance:

tasks.withType(Checkstyle) {
reports {
xml.enabled false
html.enabled true
html.stylesheet resources.text.fromFile('config/xsl/checkstyle-custom.xsl')
}
}

View a sample Checkstyle stylesheet.

The plugin adds a number of tasks to the project that perform the quality checks. You can execute the checks by running gradle check.

Note that Checkstyle will run with the same Java version used to run Gradle.

Adding Findbugs to gradle script

1. Checkstyle plugin — project layout

Below described is the default project structure. But you can change it according to your will.

config/findbugs — Other FindBugs configuration files (e.g.,excludeFilter.xml)

An example for a excludeFilter.xml can be found here.

2. Enabling FindBugs plugin

Add the following lines to build.gradle file.

apply plugin: 'findbugs'
findbugs {
ignoreFailures = false
toolVersion = "3.0.1"
sourceSets=[sourceSets.main]
excludeFilter = file("config/findbugs/excludeFilter.xml")
reportsDir = file("$project.buildDir/reports/findbugs")
effort = "max"
}
tasks.withType(FindBugs) {
reports {
xml.enabled false
html.enabled true
}
}

ignoreFailures — Whether to allow the build to continue if there are warnings.

toolVersion — The version of the code quality tool to be used.

sourceSets— The source sets to be analyzed as part of the check and build tasks.

excludeFilter — The filename of a filter specifying bugs to exclude from being reported. For this you need to you file phrase in order to define the path to file.

reportDir — The directory where reports will be generated.

effort — The analysis effort level. The value specified should be one of min, default, or max. Higher levels increase precision and find more bugs at the expense of running time and memory consumption.

More properties on the FindBugs plugin can be found here.

The Checkstyle plugin adds the following tasks to the project:

findbugsMain: Runs FindBugs against the production Java source files.

findbugsTest: Runs FindBugs against the test Java source files.

findbugsSourceSet : Runs FindBugs against the given source set’s Java source files.

(For more info on Tasks click here.)

3. Customize(optional step) the HTML report generated by the FindBugs task

If you want to customize(optional) the HTML report generated by the FindBugs task, it can be done using a XSLT stylesheet, for example to highlight specific errors or change its appearance:

tasks.withType(FindBugs) {
reports {
xml.enabled false
html.enabled true
html.stylesheet resources.text.fromFile('config/xsl/findbugs-custom.xsl')
}
}

View a sample FindBugs stylesheet.

The plugin adds a number of tasks to the project that perform the quality checks. You can execute the checks by running gradle check.

Note that Findbugs will run with the same Java version used to run Gradle.

For a complete build.gradle file example go to this link.

That’s it. :D

You have successfully added Checkstyle and FindBugs plugins to your gradle project.