Search code examples
spring-bootgradlequerydslspring-data-r2dbc

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()
}

Solution

  • 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