Home:ALL Converter>QueryDSL annotation processor and gradle plugin

QueryDSL annotation processor and gradle plugin

Ask Time:2020-01-28T22:11:10         Author:Viktor M.

Json Formatter

Cannot understand how to configure build.gradle for using querydsl annotation processor without any jpa/jdo/mongo. I want to use @QueryEntity annotation to generate Q classes so then I will be able to compose dynamic SQL queries using DSL support then convert query to plain text and provide it to Spring R2DBC DatabaseClient executor.

Is there a way what gradle querydsl apt plugin and querydsl annotation processor to use for generating Q classes with @QueryEntity annotations in build.gradle file?

I'm using gradle 5, Spring Data R2DBC, Spring Boot, plan to integrate queryDsl with annotation processsor.

That's my currect build.gradle:

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.8"
}

apply plugin: 'io.spring.dependency-management'

group = 'com.whatever'

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/milestone" }
}

ext {

    springR2dbcVersion = '1.0.0.RELEASE'
    queryDslVersion = '4.2.2'
}

dependencies {
    implementation("com.querydsl:querydsl-sql:${queryDslVersion}")
    implementation("com.querydsl:querydsl-apt:${queryDslVersion}")
    implementation('org.springframework.boot:spring-boot-starter-webflux')

    compileOnly('org.projectlombok:lombok')

    annotationProcessor('org.projectlombok:lombok')
    annotationProcessor('org.springframework.boot:spring-boot-configuration-processor')
    annotationProcessor("com.querydsl:querydsl-apt:${queryDslVersion}")
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation('io.projectreactor:reactor-test')
}

test {
    useJUnitPlatform()
}

Author:Viktor M.,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59950657/querydsl-annotation-processor-and-gradle-plugin
Ido Salomon :

Generally speaking, you shouldn't use the QueryDSL plugin.\nIn order to configure QueryDSL generation you just need the relevant querydsl module, the annotation processors and the generated source dir. For instance, with lombok integration, this configuration should work (you might need to play with the exact QueryDSL modules you need):\n\nbuildscript {\n ext {\n springBootVersion = '${springBootVersion}'\n queryDslVersion = '4.2.2'\n javaxVersion = '1.3.2'\n }\n}\n\nplugins {\n id 'idea'\n}\n\nidea {\n module {\n sourceDirs += file('generated/')\n generatedSourceDirs += file('generated/')\n }\n}\n\ndependencies {\n // QueryDSL\n compile \"com.querydsl:querydsl-sql:${queryDslVersion}\"\n annotationProcessor(\"com.querydsl:querydsl-apt:${queryDslVersion}:general\")\n\n // Lombok\n compileOnly \"org.projectlombok:lombok:${lombokVersion}\"\n annotationProcessor \"org.projectlombok:lombok:${lombokVersion}\"\n implementation(\"org.projectlombok:lombok:${lombokVersion}\")\n\n\n // Possibly annotation processors for additional Data annotations\n annotationProcessor(\"javax.annotation:javax.annotation-api:${javaxVersion}\")\n\n /* TEST */\n // Querydsl\n testCompile \"com.querydsl:querydsl-sql:${queryDslVersion}\"\n testAnnotationProcessor(\"com.querydsl:querydsl-apt:${queryDslVersion}:general\")\n\n // Lombok\n testImplementation(\"org.projectlombok:lombok:${lombokVersion}\")\n testAnnotationProcessor(\"org.projectlombok:lombok:${lombokVersion}\")\n testCompileOnly(\"org.projectlombok:lombok:${lombokVersion}\")\n\n}\n\n\nAdditional information: https://github.com/querydsl/querydsl/issues/2444#issuecomment-489538997",
2020-01-28T14:47:05
fakefla :

I want to leave this answer here as I struggled for several hours finding a solution that works for Kotlin (The question doesn't have a Java restriction as far as I can tell and there is really little information around for this). Kotlin supports annotation processing with the kapt plugin. In order to use this plugin for QueryDSL you need to 1. define the kapt plugin, and 2. specify the dependency that will do the processing, in this case com.querydsl:querydsl-apt. I used the general task for my plugin execution, but according to the documentation there are other options available (probably this can be useful for JPA, JDO, Hiberante with some extra tweaks)\nplugins {\n kotlin("kapt") version "1.4.10"\n}\n\ndependencies {\n implementation("com.querydsl:querydsl-mongodb:4.4.0")\n kapt("com.querydsl:querydsl-apt:4.4.0:general")\n}\n\nNow, whenever you run gradle build the annotation processing will trigger the DSL query class generation for your classes annotated with @QueryEntity. I hope it helps in case someone needs this for Kotlin.",
2020-11-23T06:52:46
Deepak :

This worked for me (Please follow the exact same order in the dependency)\nsourceSets {\n generated {\n java {\n srcDirs = ['build/generated/sources/annotationProcessor/java/main']\n }\n }\n}\n\n\ndependencies {\n api 'com.querydsl:querydsl-jpa:4.4.0'\n annotationProcessor 'org.projectlombok:lombok'\n annotationProcessor('com.querydsl:querydsl-apt:4.4.0:jpa')\n annotationProcessor('javax.annotation:javax.annotation-api')\n\n}\n",
2021-07-26T16:58:57
Vaishnavi :

\nThis works!!!\n\n ext {\n queryDslVersion = '4.2.1'\n \n }\n \n sourceSets {\n main {\n java {\n srcDirs = ['src/main/java', 'build/generated/sources/annotationProcessor/java/main']\n }\n }\n } \n \n \n dependencies {\n compile("com.querydsl:querydsl-core:${queryDslVersion}")\n compile("com.querydsl:querydsl-jpa:${queryDslVersion}")\n }\n \n dependencies {\n \n compile "com.querydsl:querydsl-jpa:${queryDslVersion}"\n compileOnly 'org.projectlombok:lombok:1.16.18'\n annotationProcessor(\n "com.querydsl:querydsl-apt:${queryDslVersion}:jpa",\n "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final",\n "javax.annotation:javax.annotation-api:1.3.2",\n "org.projectlombok:lombok"\n )\n",
2021-11-18T12:58:20
yy