Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QueryDSL annotation processor and gradle plugin

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()
}
like image 511
Viktor M. Avatar asked Jan 01 '23 08:01

Viktor M.


2 Answers

Generally speaking, you shouldn't use the QueryDSL plugin. In 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):

buildscript {
    ext {
        springBootVersion = '${springBootVersion}'
        queryDslVersion = '4.2.2'
        javaxVersion = '1.3.2'
    }
}

plugins {
    id 'idea'
}

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

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

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


    // Possibly annotation processors for additional Data annotations
    annotationProcessor("javax.annotation:javax.annotation-api:${javaxVersion}")

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

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

}

Additional information: https://github.com/querydsl/querydsl/issues/2444#issuecomment-489538997

like image 195
Ido Salomon Avatar answered Feb 12 '23 22:02

Ido Salomon


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)

plugins {
    kotlin("kapt") version "1.4.10"
}
dependencies {
    implementation("com.querydsl:querydsl-mongodb:4.4.0")
    kapt("com.querydsl:querydsl-apt:4.4.0:general")
}

Now, 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.

like image 42
fakefla Avatar answered Feb 12 '23 21:02

fakefla