how to exclude a folder from checkstyle of Gradle on Intellij

séan35 picture séan35 · Mar 24, 2017 · Viewed 13.4k times · Source

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"!

Answer

Opal picture Opal · Mar 24, 2017

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