'how to exclude a folder from checkstyle of Gradle on Intellij

I am new for IntelliJ and Gradle, but I have enough experience on Java and Eclipse. I generated some java classes from wsdl file. I put them under src/main/resources with a folder name - "generatedsources".

I try to prevent checkstyle for this folder and its subfolders like src/main/resources/generatedsources/* with gradle on IntelliJ.

I tried some lines such;

task checkstyle(type: Checkstyle) {
        source 'src'
    //    include '**/*.java'
    //    exclude '**/gen/**'
    //    exclude '**/R.java'
    //    exclude '**/BuildConfig.java'
        exclude 'src/main/resources/generatedsources/**'
    }

But I'm failed again.

build.gradle;

apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'checkstyle'
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'

sourceCompatibility = 1.7
version = '1.0'    

...

buildscript{
    repositories{
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'no.nils:wsdl2java:0.10'
    }
}

task checkstyle(type: Checkstyle) {
    source 'src'
//    include '**/*.java'
//    exclude '**/gen/**'
//    exclude '**/R.java'
//    exclude '**/BuildConfig.java'
    exclude 'src/main/resources/generatedsources/**'
}

EDIT - After recommendations(but still failed!):

apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'checkstyle'
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'

sourceCompatibility = 1.7
version = '1.0'

description = """BLABLABLA_application"""

war.archiveName = "BLABLABLA.war"

configurations{
    deployerJars
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.3'
    compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.3'
    compile group: 'org.springframework', name: 'spring-core', version:'4.0.0.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version:'4.0.0.RELEASE'
    compile 'log4j:log4j:1.2.17'
    compile 'com.sun.xml.ws:jaxws-rt:2.2.8'
    compile 'org.jvnet.jax-ws-commons.spring:jaxws-spring:1.9'
    compile group: 'org.springframework', name: 'spring-webmvc', version:'4.0.0.RELEASE'
    providedCompile group: 'javax.servlet', name: 'servlet-api', version:'2.5'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    deployerJars "org.apache.maven.wagon:wagon-http:2.2"
}

jar {
    manifest {
        attributes 'Implementation-Title': 'BLABLABLA', 'Implementation-Version': version
    }
}

uploadArchives {
    repositories {
        flatDir {
            dirs 'repos'
        }
    }
}


buildscript{
    repositories{
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'no.nils:wsdl2java:0.10'
    }
}

wsdl2java{
    wsdlsToGenerate = [
            ["$projectDir/src/main/resources/BLABLABLA.wsdl"]
    ]
    generatedWsdlDir = file("$projectDir/src/gen/java")
    wsdlDir = file("$projectDir/src/main/resources")
}

wsdl2javaExt {
    cxfVersion = "2.5.1"
}

tasks.withType(Checkstyle) {
    exclude '**/your/generated/package/goes/here**'
}

checkstyleMain.exclude '**/your/generated/package/goes/here**'

"exclude" in tasks.withType and "checkstyleMain" causes an error such as "cannot resolve symbol"!



Solution 1:[1]

When checkstyle plugin is applied custom tasks are delivered along with it (see here), so instead of adding custom task with type Checkstyle configure the shipped one. This is how it should be done:

tasks.withType(Checkstyle) {
    exclude '**/your/generated/package/goes/here**'
}

You can also configure a particular task, not all:

checkstyleMain.exclude '**/your/generated/package/goes/here**'

Also this is not good practice to put generated sources under src/main/resources. Typically such files goes to e.g. src/gen/java

Solution 2:[2]

I followed some comments and make it work add this in file build.gradle

checkstyle {
    config = rootProject.resources.text.fromFile("${project.projectDir}/checkstyle-8.34_google_checks.xml")
    toolVersion = '8.34'
    ignoreFailures = false
    maxWarnings = 0
}

checkstyleMain
    .exclude('com/examples/gen/api/*.java')
    .exclude('com/examples/gen/model/*.java')
    .exclude('com/examples/gen/auth/*.java')

Solution 3:[3]

I had lots of issues with checkStyle & pmd + QueryDSL A final solution for me was not to exclude generated sources, but clean them after assemble task: no sources - nothing to analyze! :)

assemble { finalizedBy cleanQuerydslSourcesDir }

Solution 4:[4]

You can only exclude a folder that is in the source set. That is something that had not been mentioned and it caused me to struggle since I am using Intellij. The generated files are in the build/ folder which is on the same level as src/. I tried excluding the build folder for a while but there was no effect. The only solution was to specify the source folder. Since the build folder is not under src (the source folder), it worked immediately.

checkstyleMain.source = "src/main/java"

I found the solution here: Specify excludes to Checkstyle task

Solution 5:[5]

For kotlin dsl use

tasks.withType<Checkstyle>() {
    exclude("**/com/generated/*.java")
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Opal
Solution 2 sendon1982
Solution 3 ????? ????????
Solution 4 Joseph Waweru
Solution 5 Juan Rada