如何在Intellij

时间:2017-03-24 08:35:06

标签: java intellij-idea gradle checkstyle

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

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

我尝试了一些这样的行;

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**'
tasks.withType中的

“exclude”和“checkstyleMain”会导致错误,例如“无法解析符号”!

4 个答案:

答案 0 :(得分:6)

当应用checkstyle插件时,自定义任务随之一起提供(请参阅here),因此,不要添加类型为Checkstyle的自定义任务,而是配置已发送的任务。这是应该如何做的:

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

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

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

将生成的资源放在src/main/resources下也不是一个好习惯。通常这样的文件转到例如src/gen/java

答案 1 :(得分:0)

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

组装{ 由cleanQuerydslSourcesDir最终确定 }

答案 2 :(得分:0)

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

答案 3 :(得分:0)

使用Kotlin DSL

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