8

I have a gradle script which generates querydsl classes from Mongo annotated entities. It was working so far, but after upgrade to Gradle 5 I have a problem with:

* What went wrong:
Execution failed for task ':myproject-common:compileQuerydsl'.
Annotation processor 'org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor' not found

Please find my gradle.build script below. Any ideas what could be wrong? I read that there was change in Gradle 5 that annotation processors are not used by default during compilation and annotationProcessor declaration should be added but when I add it to dependencies the same error occurs.

plugins {
    id 'org.springframework.boot' version '2.0.4.RELEASE'
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}
repositories {
    mavenCentral()
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
jar {
    enabled = true
    baseName = 'myproject-common'
    version =  '0.0.1-SNAPSHOT'
}
// do no package commons into fat jar
bootJar {
    enabled = false
}
querydsl {
    library = 'com.querydsl:querydsl-apt:4.1.4'
    querydslSourcesDir = 'src/main/querydsl'
    springDataMongo = true
}
sourceCompatibility = 11.0
targetCompatibility = 11.0
sourceSets {
    main {
        java {
            srcDirs = ['src/main/java', 'src/main/querydsl']
        }
    }
}
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.data:spring-data-mongodb")
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-    jsr310:2.8.6")
    compile("com.google.guava:guava:23.0")
    compile("commons-io:commons-io:2.5")
    compile("org.aspectj:aspectjweaver:1.8.9")
    compile("org.apache.commons:commons-lang3:3.5")
    compile("commons-collections:commons-collections:3.2.2")
    compile("org.javamoney:moneta:1.1")
    compile("com.fizzed:rocker-runtime:1.2.0")
    compile("com.querydsl:querydsl-core:4.1.4")
    compile("com.querydsl:querydsl-mongodb:4.1.4")
    compile("com.querydsl:querydsl-apt:4.1.4")
    compile("com.codepoetics:protonpack:1.15")

    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("org.assertj:assertj-core:3.7.0")
}
Michal Plewka
  • 291
  • 1
  • 2
  • 9

6 Answers6

9

This is my working configuration for JPA without using additional plugins. Gradle 5.3, openjdk 11.0.2.

plugins {
    id 'java-library'
}

ext {
    springBootVersion = '2.2.0.M1'
    queryDslVersion = '4.2.1'
}

dependencies {
    api(
            "com.querydsl:querydsl-jpa:$queryDslVersion"
    )

    implementation(
            platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"),
            'org.springframework.boot:spring-boot-starter-validation',
            'org.springframework.boot:spring-boot-starter-data-jpa',
            'org.liquibase:liquibase-core',
            'org.postgresql:postgresql'
    )

    annotationProcessor(
            platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"),
            'jakarta.persistence:jakarta.persistence-api',
            'jakarta.annotation:jakarta.annotation-api',
            "com.querydsl:querydsl-apt:$queryDslVersion:jpa"

    )
}

Please pay attention to the annotation processor. It has suffix ":jpa". Probably this is what you missed. To activate the same one for mongodb you should add ":morphia" suffix.

Please also look at these 2 dependencies:

'jakarta.persistence:jakarta.persistence-api'
'jakarta.annotation:jakarta.annotation-api'

This is a workaround for the issue described here: https://discuss.gradle.org/t/annotationprocessor-querydsl-java-lang-noclassdeffounderror/27107 They should be transitive dependencies of the annotation processor, but they aren't yet. Probably you will have to include some mongo dependencies to annotationProcessor too. Generated sources are located in \build\generated\sources\annotationProcessor\java\main

niebaraka
  • 101
  • 1
  • 3
  • Great first answer @niebaraka. FWIW a more conventional transitive dependency for the :jpa processor would be `annotationProcessor("javax.persistence:javax.persistence-api")` – Emerson Farrugia Apr 22 '19 at 10:42
  • @EmersonFarrugia I don't carefully follow the latest changes in EE world, but seems `jakarta.*` is now a preferable option over `javax.*` At least spring boot 2.0.0.M1 already includes mentioned dependencies, that's why I used them too. – niebaraka Apr 23 '19 at 19:06
  • agreed, though Spring Boot [2.2](https://github.com/spring-projects/spring-boot/blob/v2.2.0.M1/spring-boot-project/spring-boot-dependencies/pom.xml) will use them, the current 2.1.4 GA dependencies BOM doesn't mention them – Emerson Farrugia Apr 25 '19 at 10:16
7

I have finally found a workaround. Querydsl's lack of compatibility with Gradle 5 is reported here as a bug: https://github.com/ewerk/gradle-plugins/issues/108

Workaround is to add to gradle script:

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}
Michal Plewka
  • 291
  • 1
  • 2
  • 9
  • 1
    where exactly do we need to add this block? I got this exception after adding it : `org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method compileQuerydsl() for arguments [build_34f65uphys3x3neud0s7h85bz$_run_closure1$_closure14@47966d1f] on root project 'my-project' of type org.gradle.api.Project` – Darshan Mehta Jun 10 '20 at 11:45
1

I could solve the problem by adding the following two dependencies.

annotationProcessor "com.querydsl:querydsl-apt:4.2.1:jpa"
annotationProcessor 'javax.annotation:javax.annotation-api:1.3.1'

The second dependency was a hidden reason why it not worked for me.

Michael
  • 11
  • 1
1

I faced the same issue for Spring Boot Data JPA and Query DSL with Gradle 6.6.1. I tried so many things in my build.gradle file, including some of the suggestions in other responses to this question. I was able to come up with a build.gradle file with a minimal set of additions to the standard build file (standard in the sense of the one generated by https://start.spring.io). Here it is:

plugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
    id 'com.ewerk.gradle.plugins.querydsl' version "1.0.10"
}

group = 'org.code'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

querydsl {
    querydslDefault = true
    jpa = true
}

configurations {
    querydsl.extendsFrom implementation
}

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

ext {
    queryDslVersion = '4.4.0'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

The key addition is the Query DSL plugin:

id 'com.ewerk.gradle.plugins.querydsl' version "1.0.10"

However, it is not enough on its own. Other important additions are:

querydsl {
    querydslDefault = true
    jpa = true
}

configurations {
    querydsl.extendsFrom implementation
}

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}
Casper
  • 121
  • 5
0

The configuration i am using,it works:

    plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"

}

group = 'io.loremipsum'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'


repositories {
    mavenCentral()
}


querydsl {
    library = 'com.querydsl:querydsl-apt:4.1.4'
    querydslSourcesDir = 'src/main/querydsl'
    springDataMongo = true
}

sourceSets {
    main {
        java {
            srcDirs = ['src/main/java', 'src/main/querydsl']
        }
    }
}
// is required when gradle > 5.0
compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
    compile 'org.springframework.boot:spring-boot-starter-web'

    compile("com.querydsl:querydsl-core:4.1.4")
    compile("com.querydsl:querydsl-mongodb:4.1.4")
    compile("com.querydsl:querydsl-apt:4.1.4")

    compile 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
}

pay attention to the dependency declaration:

compile 'org.springframework.boot:spring-boot-starter-data-mongodb'

implementation dependency should not be used or will result in an exception:

Annotation processor 'org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor' not found
LoremIpsum
  • 1,652
  • 18
  • 27
  • I did not understand your comment "pay attention to the dependency declaration". Are we suppose to remove the spring mongo dependency? – JayC Jun 01 '20 at 14:40
-1

I see you have compile("com.querydsl:querydsl-apt:4.1.4") in your dependencies. According to the docs

Since implementation details matter for annotation processors, they must be declared separately on the annotation processor path. Gradle ignores annotation processors on the compile classpath.

So, put com.querydsl:querydsl-apt:4.1.4 in the annotationProcessor scope.

BTW, consider switching to api / implementation scopes over compile.

madhead
  • 31,729
  • 16
  • 153
  • 201
  • thank you for your prompt answer, I have added two lines `annotationProcessor "com.querydsl:querydsl-apt:4.1.4" annotationProcessor "org.springframework.data:spring-data-mongodb"` but it did not help and error is the same. Do you have any other ideas? – Michal Plewka Dec 25 '18 at 13:26