简体   繁体   English

升级到 Gradle 5 后的 Querydsl 注释处理器问题

[英]Querydsl Annotation Processor issue after upgrade to Gradle 5

I have a gradle script which generates querydsl classes from Mongo annotated entities.我有一个 gradle 脚本,它从 Mongo 注释的实体生成 querydsl 类。 It was working so far, but after upgrade to Gradle 5 I have a problem with:到目前为止它一直在工作,但是在升级到 Gradle 5 后,我遇到了一个问题:

* 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.请在下面找到我的 gradle.build 脚本。 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.我读到 Gradle 5 中有变化,编译期间默认不使用注释处理器,应该添加 annotationProcessor 声明,但是当我将它添加到依赖项时,会发生同样的错误。

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

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 Querydsl 缺乏与 Gradle 5 的兼容性在此处报告为错误: https : //github.com/ewerk/gradle-plugins/issues/108

Workaround is to add to gradle script:解决方法是添加到 gradle 脚本:

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

This is my working configuration for JPA without using additional plugins.这是我在不使用其他插件的情况下对 JPA 的工作配置。 Gradle 5.3, openjdk 11.0.2. 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".它有后缀“:jpa”。 Probably this is what you missed.可能这就是你错过的。 To activate the same one for mongodb you should add ":morphia" suffix.要为 mongodb 激活相同的功能,您应该添加“:morphia”后缀。

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.这是此处描述的问题的解决方法: https : //discuss.gradle.org/t/annotationprocessor-querydsl-java-lang-noclassdeffounderror/27107它们应该是注释处理器的传递依赖项,但还不是。 Probably you will have to include some mongo dependencies to annotationProcessor too.可能您也必须将一些 mongo 依赖项包含到 annotationProcessor 中。 Generated sources are located in \\build\\generated\\sources\\annotationProcessor\\java\\main生成的源位于\\build\\generated\\sources\\annotationProcessor\\java\\main

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.第二个依赖是一个隐藏的原因,为什么它对我不起作用。

I faced the same issue for Spring Boot Data JPA and Query DSL with Gradle 6.6.1.我在使用 Gradle 6.6.1 的 Spring Boot Data JPA 和 Query DSL 时遇到了同样的问题。 I tried so many things in my build.gradle file, including some of the suggestions in other responses to this question.我在build.gradle文件中尝试了很多东西,包括对这个问题的其他回答中的一些建议。 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 ).我能够想出一个build.gradle文件,其中包含对标准构建文件(从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:关键的添加是 Query DSL 插件:

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
}

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

I see you have compile("com.querydsl:querydsl-apt:4.1.4") in your dependencies.我看到您的依赖项中有compile("com.querydsl:querydsl-apt:4.1.4") 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. Gradle 会忽略编译类路径上的注解处理器。

So, put com.querydsl:querydsl-apt:4.1.4 in the annotationProcessor scope.因此,将com.querydsl:querydsl-apt:4.1.4放在annotationProcessor范围内。

BTW, consider switching to api / implementation scopes over compile .顺便说一句,考虑通过compile切换到api / implementation范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gradle 的 QueryDSL 问题 - QueryDSL issue with gradle 使用 Gradle 激活 Lombok 注释处理器 - Activating Lombok annotation processor with Gradle Querydsl mongodb gradle springboot 问题 - Querydsl mongodb gradle springboot issue 在 Kotlin(和 Gradle)中使用 Hibernate Validator Annotation Processor - Using Hibernate Validator Annotation Processor with Kotlin (and Gradle) Gradle弃用了lombok的注释处理器警告 - Gradle deprecated annotation processor warnings for lombok 将本地注释处理器项目集成到Gradle构建中 - Integrating local annotation processor project into gradle build 在Android的Gradle中,Storm注释处理器可以像AndroidAnnotations处理器一样运行吗? - In Gradle, on Android, can the Storm annotation processor be run like the AndroidAnnotations processor? Gradle通过不同的注释处理器两次生成Querydsl元数据 - Gradle generates Querydsl metadata twice via different annotation processors 当处理器依赖于注释值中的类时,选择 Gradle 增量注释处理器类别 - Choosing Gradle incremental annotation processor category when processor depends on a class in annotation value 如何使用注释处理器配置Gradle的增量构建 - How to configure Gradle's incremental build with annotation processor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM