获取 Gradle apt 插件以与 QueryDSL、lombok 和 mapstruct 一起工作的问题

Issue getting Gradle apt plugin to work with QueryDSL, lombok & mapstruct

我正在尝试让 gradle apt plugin 与以下人员一起工作:

这是我尝试过的:

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 上定义的三个显式处理器。

非常感谢托马斯!

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

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