Specify excludes to Checkstyle task

I have the following in my build:

checkstyle {
            sourceSets = [ subProject.sourceSets.main ]
            configFile = rootProject.file( 'shared/config/checkstyle/checkstyle.xml' )
            showViolations = false
            ignoreFailures = true
        }
        // exclude generated sources
        checkstyleMain.exclude '**/generated-src/**'
        // because cfg package is a mess mainly from annotation stuff
        checkstyleMain.exclude '**/org/hibernate/cfg/**'
        checkstyleMain.exclude '**/org/hibernate/cfg/*'
          findbugs {
            sourceSets = [ subProject.sourceSets.main, subProject.sourceSets.test ]
            ignoreFailures = true
        }
        // exclude generated sources
        findbugsMain.exclude '**/generated-src/**'

In both of these cases the attempt to exclude generated sources has no effect. The other excludes work.

Why wont the ‘/generated-src/’ “ANT style exclude pattern” work? How can I tell these Gradle tasks to exlude generated sources? The generated sources are part of the indicated source sets, but in a separate directory (target/generated-src/**)?

I think the reason why it’s not picked up is, that ‘generated-src’ is the root folder of your sources and not a package within your sources. The exclude statement does evaluate the path to your source folder, just relative paths within the source folder. One workaround is to generated these sources into a dedicated package that can be used in the exclude statement. Another workaround is, to set the source of the checkstyleMain explicitly:

checkstyleMain.source = "src/main/java"

cheers, René

Out of curiosity how do y’all expect builds to handle “static analysis tools” and generated sources?

Is it better (in Gradle team’s eyes) to use a dedicated SourceSet perhaps for generated sources?