Configure Checksytle for gradle project

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.

I integrated Checkstyle using Gradle plugin. The following are the steps to configure Checkstyle.

build.gradle: The following are changes that are done in build.gradle to incorporate Checkstyle:

......

ext {
	checkstyleToolVersion = "8.21"
}
apply plugin: "checkstyle"

.......

checkstyle {
	toolVersion = "${checkstyleToolVersion}"
	ignoreFailures = true
	configFile = file("config/checkstyle/google_checks.xml")
}

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

......
  1. I added 8.21 version of Checkstyle.
  2. I am using google checks version of rules for checking code style. But I had to comment out the following modules as I was getting an error.
    1. InvalidJavadocPosition.
    2. MissingJavadocMethod.
  3. I stored google_checks.xml under config/checkstyle folder under the root folder.
  4. I used checkstyle-author.xsl for generating report of Checkstyle. I stored this file under config/checkstyle under the root folder.
  5. Now when I run ./gradlew check, Checkstyle plugin runs rules and shows results under build/reports/checkstyle folder.

Leave a Reply

Your email address will not be published. Required fields are marked *