gpt4 book ai didi

java - Java QueryDSL和Gradle Kotlin DSL

转载 作者:行者123 更新时间:2023-12-03 03:12:11 27 4
gpt4 key购买 nike

我们正在将Groovy gradle构建文件迁移到KotlinDSL,而我一直坚持配置QueryDSL类的生成。

实体类使用Java编写,而QDSL文件也应使用Java。

工作的常规构建文件代码段:

sourceSets {
generated.java { srcDirs = ['src/main/generated']}
}

task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
source = sourceSets.main.java
classpath = configurations.compile + configurations.querydslapt
options.compilerArgs = ["-proc:only", "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"]
destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}

compileJava {
dependsOn generateQueryDSL
source generateQueryDSL.destinationDir
}

compileGeneratedJava {
dependsOn generateQueryDSL
options.warnings = false
classpath += sourceSets.main.runtimeClasspath
}

这是我对KotlinDSL中代码的重写:
val querydsl by configurations.creating

val generated by java.sourceSets.creating {
java.srcDirs("build/generated/java")
}

tasks {
val generateQueryDsl by creating(JavaCompile::class.java) {
group = "build"
description = "Generate QueryDSL classes"
classpath = configurations.compile + querydsl
options.compilerArgs = listOf("-proc:only", "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor")
destinationDir = generated.java.srcDirs.first()
}

getByName<JavaCompile>("compileJava") {
dependsOn(generateQueryDsl)
source(generateQueryDsl.destinationDir)
}

getByName<JavaCompile>("compileGeneratedJava") {
dependsOn(generateQueryDsl)
options.isWarnings = false
classpath += java.sourceSets["main"].runtimeClasspath
}
}

当我运行 generateQueryDsl任务时,我没有来源,也没有生成QDSL文件:
> Task :common:compileKotlin UP-TO-DATE
> Task :common:generateGrammarSource UP-TO-DATE
> Task :common:compileJava UP-TO-DATE
> Task :common:processResources UP-TO-DATE
> Task :common:classes UP-TO-DATE
> Task :common:inspectClassesForKotlinIC UP-TO-DATE
> Task :common:jar UP-TO-DATE
> Task :common-db:compileKotlin UP-TO-DATE
> Task :common-db:compileJava UP-TO-DATE
> Task :common-db:processResources UP-TO-DATE
> Task :common-db:classes UP-TO-DATE
> Task :common-db:inspectClassesForKotlinIC UP-TO-DATE
> Task :common-db:jar UP-TO-DATE
> Task :core:generateQueryDsl NO-SOURCE

我不确定,但是有种感觉,我可以简化这一代的工作,但是我没有想法。

最佳答案

还有另一种生成QueryDSL Q *类的方法,可用于最新的Gradle版本(我认为> = 4.9):下面是该脚本的Groovy版本,您应该尝试从中制作Kotlin版本:

sourceSets {
generated.java {
srcDir file('/build/generated/java')
}
}

dependencies {
compile("com.querydsl:querydsl-jpa:${queryDslVersion}")
compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:${hibernateJpaVersion}"

annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
annotationProcessor "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:${hibernateJpaVersion}"
}
compileJava {
options.annotationProcessorPath = configurations.annotationProcessor
options.setAnnotationProcessorGeneratedSourcesDirectory(file("$projectDir/build/generated/java"))
}

这些QueryDSL类将在Java编译任务执行(注释处理器)期间自动生成,您不再需要定义专用任务。

关于java - Java QueryDSL和Gradle Kotlin DSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52855193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com