The Gradle Checkstyle plugin only passes .java files to Checkstyle by default. Here’s how to make sure no files are missed.

Many Checkstyle checks only unleash their full potential if really all files are passed to Checkstyle. For example, our ModuleDirectoryLayout check does not make much sense if it can see only .java files. Like all FileSetChecks, it can operate on any type of file, and actually should in order to do its job right.

How to configure Gradle to pass all files to Checkstyle is not immediately obvious from the documentation, but it’s actually not complicated either:

Option 1: Do it only for one particular source set, for example main:

tasks['checkstyleMain'].setSource(project.sourceSets.main.allSource);

Option 2: Do it for all Checkstyle tasks for all source sets:

project.extensions.findByName('checkstyle').sourceSets.each { SourceSet s ->
    Checkstyle task = (Checkstyle) tasks.findByName(s.getTaskName('checkstyle', null));
    task.setSource(s.allSource);
    getLogger().info('Reconfigured task \'' + task.name +
        '\' to include all files in sourceSet \'' + s.name + '\'');
}

The key operation in both examples above is the call to setSource(). The Gradle Checkstyle plugin is hardcoded to use the allJava property, which includes only the Java files. Fortunately, we can overwrite this as shown above.
Note that the classpath is not affected, because the non-Java files usually don’t get compiled into .class files.

This post is based on Gradle 5.2.1 and Checkstyle 8.18.