Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to get gradle apt plugin to work with:

  • QueryDSL
  • Mapstruct
  • Lombok

Here is what I have attempted:

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

Here is the error I get:

> 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

Indicating that the javaCompile task has somehow lost the compile classpath...

Can someone please help?

edit: Taking into account Thomas Broyer's advice, I was able to come up with the following changes:

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"

Notice the added hibernate-jpa-2.1-api apt dependency and the three explicit processors defined on the aptOptions.

Thanks a lot Thomas!

like image 251
balteo Avatar asked Nov 18 '25 01:11

balteo


1 Answers

Apparently, with querydsl-apt, you need to add the additional dependencies required by the specific processor you're using; in this case you need to add the JPA API to the apt configuration.

Also, note that by configuring aptOptions.processors explicitly with only the QueryDSL processor, the Lombok and MapStruct processors won't run.

like image 185
Thomas Broyer Avatar answered Nov 20 '25 18:11

Thomas Broyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!