java 如何从 Intellij 上 Gradle 的 checkstyle 中排除文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42994778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:00:39  来源:igfitidea点击:

how to exclude a folder from checkstyle of Gradle on Intellij

javaintellij-ideagradlecheckstyle

提问by séan35

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/resourceswith a folder name - "generatedsources".

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

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

我尝试阻止此文件夹及其子文件夹的 checkstyle,例如src/main/resources/generatedsources/*IntelliJ 上的 gradle。

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;

构建.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.Hymanson', name: 'Hymanson-core-asl', version: '1.9.3'
    compile group: 'org.codehaus.Hymanson', name: 'Hymanson-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"!

tasks.withType 中的“exclude”和“checkstyleMain”会导致诸如“无法解析符号”之类的错误!

回答by Opal

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

checkstyle应用插件时,自定义任务会随它一起交付(请参阅此处),因此不要添加带有类型的自定义任务,而是Checkstyle配置已交付的任务。应该这样做:

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

此外,将生成的源放在src/main/resources. 通常这样的文件去例如src/gen/java

回答by Антон Ткаченко

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! :)

我在使用 checkStyle 和 pmd + QueryDSL 时遇到了很多问题 对我来说,最终的解决方案不是排除生成的源,而是在组装任务后清理它们:没有源 - 无需分析!:)

assemble { finalizedBy cleanQuerydslSourcesDir }

组装 { finalizedBy cleanQuerydslSourcesDir }