using checkstyle in gradle

The checkstyle plugin brings code quality checks into the gradle build. This way, if team members are using disparate IDEs or have different settings, consistent coding standards are enforced in the build.

Here’s how to add checkstyle to your Java build in two steps:
1) In build.gradle, add this:

    apply plugin: ‘checkstyle’


    checkstyle {
       configFile = new File(rootDir, “checkstyle.xml“)
    }

The configFile setting is not necessary if you use the default location, which is config/checkstyle/checkstyle.xml. More configuration options are listed in the gradle userguide.


If yours is a multiproject build, put that configuration in a subprojects block in the parent build.gradle. This uses the parent’s rootDir/checkstyle.xml so the checkstyle configuration is consistent between projects.

2) Create checkstyle.xml. For reasonable default settings, google it and steal someone else’s. I took mine from a google-api repo and stripped it down. Here’s a really basic example, which checks only for tab characters and unused imports:


<!DOCTYPE module PUBLIC
    “-//Puppy Crawl//DTD Check Configuration 1.3//EN”
     “http://www.puppycrawl.com/dtds/configuration_1_3.dtd”&gt;


 
   
 

Visit the checkstyle site to find a million more options for checks.