Querydsl: How to both use `Querydsl` and `lombok` with gradle?

Created on 4 May 2019  ·  10Comments  ·  Source: querydsl/querydsl


java 11
gradle 5.4

idea 2018.3

This is my gradle config

plugins {
    id 'java'
    id 'idea'
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
    id "org.springframework.boot" version "2.1.4.RELEASE"
    id "io.franzbecker.gradle-lombok" version "3.0.0"
    id 'io.spring.dependency-management' version "1.0.7.RELEASE"
}

group 'cn.org.cczr'
version '1.0-SNAPSHOT'
//apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 11
targetCompatibility = 11

sourceSets {
    main {
        java {
            srcDir "$buildDir/generated/source/main"
        }
    }
}

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

querydsl {
    jpa = true
    querydslSourcesDir = "$buildDir/generated/source/main"
}

project.afterEvaluate {
    project.tasks.compileQuerydsl.options.compilerArgs = [
            "-proc:only",
            "-processor", project.querydsl.processors() +
                    ',lombok.launch.AnnotationProcessorHider$AnnotationProcessor'
    ]
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')

    implementation "com.querydsl:querydsl-jpa" // querydsl
    implementation "com.querydsl:querydsl-apt"  // querydsl
    implementation("org.projectlombok:lombok:1.18.6")

    annotationProcessor(
            "com.querydsl:querydsl-apt:4.1.4:jpa",
            "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final",
            "javax.annotation:javax.annotation-api",
            "org.projectlombok:lombok:1.18.6"
    )

    testImplementation('org.springframework.boot:spring-boot-starter-test')
    testImplementation group: 'junit', name: 'junit', version: '4.12'
    testImplementation("org.projectlombok:lombok:1.18.6")
    testAnnotationProcessor("org.projectlombok:lombok:1.18.6")
}

then when i use gradle build

it report error
Execution failed for task ':compileQuerydsl'.
Annotation processor 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor' not found

if i delete the lombok dependcies ,the querydsl even not work normal ,
it report error
can't find find symbol and the package don't exist

so how can i both use Querydsl and lombok with gradle?

and i think use querydsl with gradle is so difficulty,you must config so many option,
just like

sourceSets {
    main {
        java {
            srcDir "$buildDir/generated/source/main"
        }
    }
}

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

querydsl {
    jpa = true
    querydslSourcesDir = "$buildDir/generated/source/main"
}

project.afterEvaluate {
    project.tasks.compileQuerydsl.options.compilerArgs = [
            "-proc:only",
            "-processor", project.querydsl.processors() +
                    ',lombok.launch.AnnotationProcessorHider$AnnotationProcessor'
    ]
}

compare with maven config,it's too troublesome.
Is there a easier way?

Most helpful comment

I think the preferred way whatever it is should be indicated in the official querydsl doc as this is the case with maven and ant integration. gradle seems to me far more relevant nowadays than ant is...

All 10 comments

This worked for me (only querydsl-related lines are shown for brevity):

buildscript {
    ext {
        springBootVersion = '2.1.3.RELEASE'
        queryDslVersion = '4.2.1'
    }
}

plugins {
    id 'idea'
}

idea {
    module {
        sourceDirs += file('generated/')
        generatedSourceDirs += file('generated/')
    }
}

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

    // Lombok
    compileOnly "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
    implementation("org.projectlombok:lombok:${lombokVersion}")

    annotationProcessor("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")

    /* TEST */
    // Querydsl
    testCompile "com.querydsl:querydsl-jpa:${queryDslVersion}"
    testAnnotationProcessor("com.querydsl:querydsl-apt:${queryDslVersion}:jpa")

    // Lombok
    testImplementation("org.projectlombok:lombok:${lombokVersion}")
    testAnnotationProcessor("org.projectlombok:lombok:${lombokVersion}")
    testCompileOnly("org.projectlombok:lombok:${lombokVersion}")

}

Emphasis is on compileOnly for lombok, the annotation processors and the generated source dir. By the way, adding mapstruct works similarly.

I hope it helps. :)

@IdoSalomon pls see another solution here. Seems like this is the issue.

This worked for me (only querydsl-related lines are shown for brevity):

buildscript {
    ext {
        springBootVersion = '2.1.3.RELEASE'
        queryDslVersion = '4.2.1'
    }
}

plugins {
    id 'idea'
}

idea {
    module {
        sourceDirs += file('generated/')
        generatedSourceDirs += file('generated/')
    }
}

dependencies {
    // QueryDSL
    compile("com.querydsl:querydsl-core:${queryDslVersion}")
    compileOnly "com.querydsl:querydsl-jpa:${queryDslVersion}"
    annotationProcessor("com.querydsl:querydsl-apt:${queryDslVersion}:jpa")
    implementation("com.querydsl:querydsl-jpa:${queryDslVersion}")

    // Lombok
    compileOnly "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
    implementation("org.projectlombok:lombok:${lombokVersion}")
}

Emphasis is on compileOnly for both lombok and querydsl.

I hope it helps. :)

Thanks for you help,
yours config can work well

i think ,it is crucial that both use "compileOnly" and "annotationProcessor "
and appoint the sourcedir .

the querydsl gradle plugin is unnecessary .because it's can't work with java 11 and gradle 5.0.

so hard!!!!

I'm getting a error msg:

  • What went wrong:
    Execution failed for task ':compileJava'.
    > java.lang.NoClassDefFoundError: javax/persistence/Entity

Any tips?

@isaaclcl You seem to miss the annotation processor for it. You might be overriding it in your attempts to add querydsl. You can try explicitly defining all three:

dependencies {
    // For javax/persistence/Entity
    annotationProcessor("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")

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

    // Lombok
    compileOnly "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
}

Let me know if you need further assistance.

After several mouths , i find a perfect method to solve this question.

First , don't use plugin 'com.ewerk.gradle.plugins.querydsl', it's deprecated ,not complie with gradle 5.0+.

You must use plugin 'java-library' ,this is a gradle core plugin which maintenance by gradle officer,so you don't need worry it will deprecated .

For more information about java-library.you can see it https://docs.gradle.org/current/userguide/java_library_plugin.html

That's my config

plugins{
    id 'java-library'
}

dependencies {
    api(
            "com.querydsl:querydsl-jpa:4.2.1",
    )

    implementation(
            platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"),
            ......
            "org.projectlombok:lombok:1.18.6"
    )
    annotationProcessor(
            platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"),
            "javax.persistence:javax.persistence-api",
            "javax.annotation:javax.annotation-api",
            "org.projectlombok:lombok:1.18.6",
            "com.querydsl:querydsl-apt:4.2.1:jpa"
    )
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
}

jar {
    manifest {
        attributes(
                "Manifest-Version":"1.0",
                "Main-Class": "tv.acgn.advertisment.publish.PublishApplication"
        )
    }

    archiveBaseName ='ad-publish'
    version = '1.0.0.-SNAPSHOT'
}

the crucial is two attribute in dependencies task: api 、annotationProcessor

you must use "com.querydsl:querydsl-jpa:4.2.1" in api
and
"javax.persistence:javax.persistence-api",

"javax.annotation:javax.annotation-api",

"com.querydsl:querydsl-apt:4.2.1:jpa"
in annotationProcessor

like this:

dependencies {
    api(
            "com.querydsl:querydsl-jpa:4.2.1",
    )

   .......
    annotationProcessor(
            platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"),
            "org.projectlombok:lombok:1.18.6",
            "javax.persistence:javax.persistence-api",
            "javax.annotation:javax.annotation-api",
            "com.querydsl:querydsl-apt:4.2.1:jpa"
    )

}


'javax.* ' need not point out version if you both dependies 'springdatajpa' or hibernate,
because of these two 'javax.* ' has clear version and import by springdatajpa/hibernate.

Usually now you can correctly both use querydsl and spring-data-jpa、lombok,
but i find sometime the project even report error:can't find symbol when you go build project and project use the querydsl generated 'Q-Class',
if this situation happen ,you can appoint the source dir to solve it.

sourceSets {
    main {
        java {
            srcDirs = ["$projectDir/src/main/java", "$projectDir/build/generated"]
        }
    }
}

It works like a magic.
annotationProcessor("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")

Thanks for the reply, you guys are awesome.

@isaaclcl You seem to miss the annotation processor for it. You might be overriding it in your attempts to add querydsl. You can try explicitly defining all three:

dependencies {
    // For javax/persistence/Entity
    **annotationProcessor("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")**

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

    // Lombok
    compileOnly "org.projectlombok:lombok:${lombokVersion}"
    **annotationProcessor "org.projectlombok:lombok:${lombokVersion}"**
}

Let me know if you need further assistance.

Annotation processor issue is related to https://github.com/querydsl/querydsl/issues/2522.

I think the preferred way whatever it is should be indicated in the official querydsl doc as this is the case with maven and ant integration. gradle seems to me far more relevant nowadays than ant is...

Was this page helpful?
0 / 5 - 0 ratings