如何从 Intellij 上的 Gradle 的检查样式中排除文件夹

2022-09-03 13:41:38

我是IntelliJ和Gradle的新手,但我在Java和Eclipse方面有足够的经验。我从wsdl文件生成了一些java类。我用文件夹名称将它们放在下面 - “生成的源”。src/main/resources

我试图阻止此文件夹及其子文件夹的checkstyle,就像IntelliJ上的gradle一样。src/main/resources/generatedsources/*

我尝试了一些这样的线;

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

但我又失败了。

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/**'
}

编辑 - 在建议之后(但仍然失败!

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**'

任务中的“排除”。withType 和 “checkstyleMain” 会导致错误,如“无法解析符号”!


答案 1

当应用插件时,自定义任务将与它一起交付(请参阅此处),因此,不要使用类型添加自定义任务,请配置附带的任务。这是应该如何完成的:checkstyleCheckstyle

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

您还可以配置特定任务,而不是全部:

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

此外,将生成的源放在 下也不是很好的做法。通常,此类文件会转到例如src/main/resourcessrc/gen/java


答案 2

我关注了一些评论,并使其工作将其添加到文件中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')

推荐