将Gradle apt插件与QueryDSL,lombok& mapstruct

时间:2017-07-02 23:21:58

标签: gradle lombok annotation-processing gradle-apt-plugin

我想让gradle apt plugin与之合作:

  • QueryDSL
  • Mapstruct
  • 龙目

以下是我的尝试:

plugins {
    id 'net.ltgt.apt' version '0.10'
}

description = "Bignibou Common"

apply plugin: 'org.springframework.boot'

dependencyManagement {
    dependencies {
        dependency "org.elasticsearch:elasticsearch:${elasticsearchVersion}"
    }
}

dependencies {

    compile("org.springframework.boot:spring-boot-starter-data-jpa") {
        exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
    }
    compile("org.springframework.boot:spring-boot-starter-mail")
    compile('org.springframework.security:spring-security-core')
    compile('org.hibernate:hibernate-validator')
    compile("org.hibernate:hibernate-java8")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

    //Spring cloud
    compile("org.springframework.cloud:spring-cloud-spring-service-connector")
    compile("org.springframework.cloud:spring-cloud-localconfig-connector")
    compile("org.springframework.cloud:spring-cloud-cloudfoundry-connector")

    // Relational Database
    compile("org.postgresql:postgresql:${postgresqlVersion}")
    compile("org.flywaydb:flyway-core")

    // Connection pooling
    compile("com.zaxxer:HikariCP")

    //Shield
    compile("org.elasticsearch.client:x-pack-transport:${elasticsearchVersion}")
    compile("org.elasticsearch:elasticsearch:${elasticsearchVersion}")
    compile("org.apache.logging.log4j:log4j-api")
    compile("org.apache.logging.log4j:log4j-core")

    // QueryDSL
    compile("com.querydsl:querydsl-core:${queryDslVersion}")
    compile("com.querydsl:querydsl-jpa:${queryDslVersion}")

    compileOnly('org.projectlombok:lombok')
    compileOnly('org.mapstruct:mapstruct-jdk8:1.2.0.Beta3')

    apt "com.querydsl:querydsl-apt:${queryDslVersion}", 'org.mapstruct:mapstruct-processor:1.2.0.Beta3', 'org.projectlombok:lombok'

    // Jackson
    compile("com.fasterxml.jackson.core:jackson-core")
    compile("com.fasterxml.jackson.core:jackson-annotations")

    compile("org.apache.httpcomponents:httpclient:${httpClientVersion}")
    compile("org.jasypt:jasypt:${jasyptVersion}")

}

sourceSets {

    main {
        output.dir("build/generated-mail-templates")
    }
}

bootRepackage {
    enabled = false
}

task npmInstall(type: Exec) {
    description "npm install"
    commandLine 'npm', 'install'
}

task processMailTemplates {
    description "Processes mail templates"
    dependsOn npmInstall

    outputs.upToDateWhen { false }

    doLast {
        def templateSrcDir = "src/main/templates/mail/"
        def templateDestDir = "build/generated-mail-templates/META-INF/templates/mail/"

        mkdir templateDestDir

        def templateNames = []

        fileTree(dir: templateSrcDir, include: '**/*.html').visit {
            FileVisitDetails details -> templateNames << details.file.name
        }

        templateNames.each { templateName -> inlineCss(templateSrcDir + templateName, templateDestDir + templateName) }
    }
}

static def inlineCss(src, dest) {
    def juice = 'node_modules/.bin/juice'
    def juiceResourcesDir = 'src/main/templates/misc/'
    def juiceArgs = "--options-file ${juiceResourcesDir}juiceOptions.json --css ${juiceResourcesDir}mailStyle.css"
    "${juice} ${juiceArgs} ${src} ${dest}".execute(null, new File('bignibou-common'))
}

compileJava {
    aptOptions.processors = ['com.querydsl.apt.jpa.JPAAnnotationProcessor']
}

processResources.dependsOn processMailTemplates

这是我得到的错误:

> Task :bignibou-common:compileJava
Putting task artifact state for task ':bignibou-common:compileJava' into context took 0.001 secs.
Resolving dependency management for configuration 'apt' of project 'bignibou-common'
Resolving global dependency management for project 'bignibou-common'
Excluding []
Resolving dependency management for configuration 'compileClasspath' of project 'bignibou-common'
Resolving dependency management for configuration 'compileOnly' of project 'bignibou-common'
Resolving dependency management for configuration 'implementation' of project 'bignibou-common'
Resolving dependency management for configuration 'compile' of project 'bignibou-common'
Excluding []
Executing task ':bignibou-common:compileJava' (up-to-date check took 0.628 secs) due to:
  Task ':bignibou-common:compileJava' has additional actions that have changed
All input files are considered out-of-date for incremental task ':bignibou-common:compileJava'.
Excluding []
Compiling with JDK Java compiler API.
Note: Running JPAAnnotationProcessor

:bignibou-common:compileJava (Thread[Task worker,5,main]) completed. Took 0.727 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':bignibou-common:compileJava'.
> java.lang.NoClassDefFoundError: javax/persistence/Entity

表明javaCompile任务以某种方式丢失了编译类路径...

有人可以帮忙吗?

编辑:考虑到Thomas Broyer的建议,我能够做出以下更改:

compileJava {
    aptOptions.processors = ['com.querydsl.apt.jpa.JPAAnnotationProcessor', 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor', 'org.mapstruct.ap.MappingProcessor']
}

compileOnly('org.projectlombok:lombok')
compileOnly('org.mapstruct:mapstruct-jdk8:1.2.0.Beta3')

apt "com.querydsl:querydsl-apt:${queryDslVersion}"
apt "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final"
apt "org.mapstruct:mapstruct-processor:1.2.0.Beta3"
apt "org.projectlombok:lombok"

注意添加的hibernate-jpa-2.1-api apt依赖关系和aptOptions上定义的三个显式处理器。

非常感谢托马斯!

1 个答案:

答案 0 :(得分:3)

显然,使用querydsl-apt,您需要添加您正在使用的特定处理器所需的其他依赖项;在这种情况下,您需要将JPA API添加到apt配置。

另请注意,通过仅使用QueryDSL处理器显式配置aptOptions.processors,Lombok和MapStruct处理器将无法运行。