Problem using the exclude pattern (Checkstyle plugin)

(Max Mustermann) #1

I’m using the ANTLR plugin which generates files at the /build/generated-src/antlr/main/ directory. I want to exclude these files from the checkstyle plugin. I tried

tasks.withType(Checkstyle) {
exclude ‘build**’

to exclude everything inside the build directory, but it does not work. The documentation says:

Adds an ANT style exclude pattern.

I found this SO answer according to which I think my pattern above should work.

How can I exclude files from being analyzed by Checkstyle?


(Max Mustermann) #2

I found this gradle documentation page which has the following example:

all files (including subdirectories) beneath src/main/webapp:

but (adapting to this)


doesn’t work, too.


(uklance) #3

The exclude pattern is relative to the root folder (in this case the root folder is $buildDir/generated-src/antlr/main. You can’t use the root folder in the exclude pattern (this information is not visible to the exclude pattern)

You might be able to use exclude(Closure) instead.


def genPath = file("$buildDir/generated-src").absolutePath
exclude { FileTreeElement el ->
   return el.file.absolutePath.startsWith(genPath)

(Max Mustermann) #4

Thanks, but when I try this I get

Execution failed for task ‘:checkstyleMain’.
Cannot add include/exclude specs to Ant node. Only include/exclude patterns are currently supported.

See also this issue Unfortunately I’m not familiar with Groovy and don’t know how to apply the workaround mentioned at the bottom of this issue.

But since you said that the exclude pattern in this case is relative to $buildDir/generated-src/antlr/main I tried exclude '*' which works, but I’m not sure if this is a good solution and transferrable if I want to exclude other paths as well…?

Where can read more about how the root folder is constructed?


(uklance) #5

I’m guessing somewhere in your build the following is happening (possibly in the antlr plugin) {
    srcDir "$buildDir/generated-src/antlr/main" 

So your java sources are now the combination of adding two FileTree together

  • src/main/java
  • $buildDir/generated-src/antlr/main

From the checkstyle docs here you can see that a checkstyleX task is created for each SourceSet

You can see here that the source property defaults to sourceSet.allJava


  1. Checkstyle.source is read only (you can’t set it to another FileTree)
  2. Because checkstyle delegates to ant you can only use simple patterns for exclude

A few options I can think of off the top of my head

  1. *** Simplest *** If your antlr sources are in a distinct package, just exclude that package via exclude 'com/foo/antlrpackage'
  2. Move $buildDir/generated-src/antlr/main to it’s own SourceSet (separate from main) and disable the checkstyleX task for the SourceSet
  3. Perhaps there’s an option to manually configure a Checkstyle task rather than applying the checkstyle plugin
1 Like

(uklance) #6

Whoops, I think I misread the docs here

FileTree source (read-only)

I thought that meant there was no setter for source. I’m now thinking that getSource() returns an immutable FileCollection.

Checkstyle extends SourceTask which has setSource(…). So a simple fix to your problem is

checkstyleMain {
    source = fileTree('src/main/java')