7

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()
}
Viktor M.
  • 4,393
  • 9
  • 40
  • 71

5 Answers5

11

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

Ido Salomon
  • 205
  • 2
  • 5
  • Thing is that `com.querydsl:querydsl-apt:${queryDslVersion}:jpa` looks like catches only `@Entity` annotations. Or how I can generate Q classes with `@EntityQuery` annotatios? – Viktor M. Jan 28 '20 at 15:34
  • Oops, sorry, I meant to edit the JPA parts to SQL. Does that solve your problem? – Ido Salomon Jan 28 '20 at 22:56
  • 1
    Doesn't generate any Q classes in build/generated folder(just /generated folder doesn't appear as well) during build process. – Viktor M. Jan 29 '20 at 09:37
  • Ok, I found the problem, I were need to define proper classifier for `querydsl-apt`. Correct classifier for default `queryDslAnnotationProcessor` is `general`. So `"com.querydsl:querydsl-apt:${queryDslVersion}:general"`. Correct your answer and I will tick your answer as correct one. – Viktor M. Jan 29 '20 at 10:13
  • 1
    Added javax.annotations as annotationProcessor to your answer otherwise `Unable to load class 'javax.annotation.Generated'.` Thanks for you help! =) – Viktor M. Jan 29 '20 at 10:20
  • This answer appears to be only applicable to IntelliJ users since it using the idea plugin. How is this done without InelliJ? – James Oct 12 '22 at 20:03
7

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.

fakefla
  • 156
  • 2
  • 5
2

This worked for me (Please follow the exact same order in the dependency)

sourceSets {
  generated {
    java {
      srcDirs = ['build/generated/sources/annotationProcessor/java/main']
    }
  }
}


dependencies {
    api 'com.querydsl:querydsl-jpa:4.4.0'
    annotationProcessor 'org.projectlombok:lombok'
    annotationProcessor('com.querydsl:querydsl-apt:4.4.0:jpa')
    annotationProcessor('javax.annotation:javax.annotation-api')

}
Deepak
  • 1,506
  • 1
  • 14
  • 13
2

for sinle gradle project just add next lines to the same build.gradle

for multi module gradle project add next lines to build.gradle of module where are jpa entities:

implementation("com.querydsl:querydsl-core:${queryDslVersion}") 
annotationProcessor(
        "com.querydsl:querydsl-apt:${queryDslVersion}:jpa",
        "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final",
        "javax.annotation:javax.annotation-api:1.3.2")

and next line to build.gradle of module where are jpa repositories:

implementation("com.querydsl:querydsl-jpa:${queryDslVersion}")
Ivan
  • 2,316
  • 2
  • 24
  • 22
1

This works!!!

  ext {
        queryDslVersion = '4.2.1'
        
    }
    
    sourceSets {
        main {
            java {
                srcDirs = ['src/main/java', 'build/generated/sources/annotationProcessor/java/main']
            }
        }
    }   
    
    
     dependencies {
            compile("com.querydsl:querydsl-core:${queryDslVersion}")
            compile("com.querydsl:querydsl-jpa:${queryDslVersion}")
        }
        
        dependencies {
        
            compile "com.querydsl:querydsl-jpa:${queryDslVersion}"
            compileOnly 'org.projectlombok:lombok:1.16.18'
            annotationProcessor(
                "com.querydsl:querydsl-apt:${queryDslVersion}:jpa",
                "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final",
                "javax.annotation:javax.annotation-api:1.3.2",
                "org.projectlombok:lombok"
            )
Vaishnavi
  • 419
  • 5
  • 9