简体   繁体   English

是否可以通过 gradle (Kotlin-DSL) 为 Kotlin MongoDB 文档生成 Q 类?

[英]Is it possible to generate Q classes by gradle (Kotlin-DSL) for Kotlin MongoDB Documents?

I have a project with Maven, Kotlin, QueryDSL, Spring Boot and MongoDB.我有一个项目 Maven、Kotlin、QueryDSL、Spring 启动和 Z206E3718AF092FDACCCE.F8 It works quite well but I thought that migrating to Gradle could speed up building it.它工作得很好,但我认为迁移到 Gradle 可以加快构建它的速度。 Everything was good before I began moving module with QueryDSL.在我开始使用 QueryDSL 移动模块之前,一切都很好。 It turned up that I can not generate Q-classes for Kotlin classes annotated with @Document.事实证明,我无法为带有@Document 注释的 Kotlin 类生成 Q 类。 So is there a way to solve it?那么有没有办法解决呢? Document example (placed /src/main/kotlin/com/company, in kotlin directory):文档示例(放在 /src/main/kotlin/com/company,在 kotlin 目录下):

package ...
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document

@Document(collection = "myDocument")
data class MyDocument(
        val smth: String
) 

maven (piece that responsible for generating) maven(负责生成的部分)

<plugin>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <version>${kotlin.version}</version>

                    <configuration>
                        <args>
                            <arg>-Werror</arg>
                        </args>
                        <annotationProcessors>
                            org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor
                        </annotationProcessors>
                        <compilerPlugins>
                            <plugin>spring</plugin>
                        </compilerPlugins>
                    </configuration>

                    <dependencies>
                        <dependency>
                            <groupId>org.jetbrains.kotlin</groupId>
                            <artifactId>kotlin-maven-noarg</artifactId>
                            <version>${kotlin.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.jetbrains.kotlin</groupId>
                            <artifactId>kotlin-maven-allopen</artifactId>
                            <version>${kotlin.version}</version>
                        </dependency>
                    </dependencies>

                    <executions>
                        <execution>
                            <id>compile</id>
                            <phase>compile</phase>
                            <configuration>
                                <sourceDirs>
                                    <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                                    <sourceDir>${project.basedir}/src/main/java</sourceDir>
                                </sourceDirs>
                            </configuration>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>kapt</id>
                            <goals>
                                <goal>kapt</goal>
                            </goals>
                            <configuration>
                                <sourceDirs>
                                    <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                                    <sourceDir>${project.basedir}/src/main/java</sourceDir>
                                </sourceDirs>
                            </configuration>
                        </execution>
                        <execution>
                            <id>test-compile</id>
                            <phase>test-compile</phase>
                            <configuration>
                                <sourceDirs>
                                    <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                                </sourceDirs>
                            </configuration>
                            <goals>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

For gradle+kotlin AFAIU we have to use kapt to generate Q-classes in this way对于 gradle+kotlin AFAIU 我们必须使用 kapt 以这种方式生成 Q-classes

kapt("com.querydsl:querydsl-apt:4.2.1:jpa")

but it does not work for me, my new build.gradle.kts:但这对我不起作用,我的新 build.gradle.kts:

import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.2.0.RELEASE"
    id("io.spring.dependency-management") version "1.0.8.RELEASE"
    kotlin("jvm") version "1.3.50"
    kotlin("kapt") version "1.3.50"
    kotlin("plugin.jpa") version "1.3.50"
    id("org.jetbrains.kotlin.plugin.spring") version "1.3.21"
}

apply(plugin = "kotlin")
apply(plugin = "kotlin-kapt")
apply(plugin = "kotlin-jpa")
apply(plugin = "org.springframework.boot")
apply(plugin = "io.spring.dependency-management")


group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}


dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("com.querydsl:querydsl-jpa")
    implementation("com.querydsl:querydsl-apt")

    kapt("com.querydsl:querydsl-apt:4.2.1:jpa")
    kapt("org.springframework.boot:spring-boot-starter-data-jpa")
    kapt("org.springframework.boot:spring-boot-configuration-processor")
    kapt("org.springframework.data:spring-data-mongodb:2.2.0.RELEASE")


    implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
}

//sourceSets { main["kotlin"].srcDirs += [generated] }
//val querydslSrcDir = "src/main/generated"

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

In maven I can set precisely annotation processor (org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor) but in gradle I can not figure out how to achieve it.在 maven 中,我可以精确设置注释处理器(org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor),但在 gradle 中我无法弄清楚如何实现它。

You should add implementation("com.querydsl:querydsl-mongodb") and kapt("com.querydsl:querydsl-apt") in dependencies section.您应该在dependencies项部分添加implementation("com.querydsl:querydsl-mongodb")kapt("com.querydsl:querydsl-apt")

Then add the following after dependencies section.然后在dependencies项部分之后添加以下内容。

kapt {
    annotationProcessor("org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor")
}

Also, don't forget to remove those JPA dependencies as well.另外,不要忘记删除那些 JPA 依赖项。

This is a working example i created. 这是我创建的一个工作示例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM