QueryDSL 注释处理器和 gradle 插件

QueryDSL annotation processor and gradle plugin

无法理解如何配置 build.gradle 以在没有任何 jpa/jdo/mongo 的情况下使用 querydsl 注释处理器。我想使用 @QueryEntity 注释生成 Q 类 这样我就可以使用 DSL 支持编写动态 SQL 查询,然后将查询转换为纯文本并将其提供给 Spring R2DBC DatabaseClient执行者.

有没有什么 gradle querydsl apt 插件和 querydsl 注释处理器用于在 build.gradle 文件中生成带有 @QueryEntity 注释的 Q 类 的方法?

我正在使用gradle5,Spring数据R2DBC,Spring启动,计划将queryDsl与注释处理器集成。

这是我的当前 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()
}

一般来说,您不应该使用 QueryDSL 插件。 为了配置 QueryDSL 生成,您只需要相关的 querydsl 模块、注释处理器和生成的源目录。例如,对于 lombok 集成,此配置应该有效(您可能需要使用您需要的确切 QueryDSL 模块):

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

}

附加信息:https://github.com/querydsl/querydsl/issues/2444#issuecomment-489538997

我想在这里留下这个答案,因为我花了几个小时寻找适用于 Kotlin 的解决方案(这个问题没有 Java 限制,因为据我所知,这方面的信息真的很少)。 Kotlin 支持注释处理 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 还有其他可用选项(可能这对 JPA、JDO、Hiberante 有一些额外的调整很有用)

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

现在,每当您 运行 gradle build 注释处理将触发 DSL 查询 class 生成 class 用 @QueryEntity 注释的 es。我希望它能有所帮助,以防有人需要这个用于 Kotlin。

这对我有用(请在依赖项中遵循完全相同的顺序)

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')

}

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