我今天想出了一个可行的解决方案。它结合了我在github上找到的"Classpath based code generation" chapter of the QueryDSL manual和the build script of some "tasca" project written提供的信息(非常感谢作者)。
我在 github 上找到的构建脚本已针对 Scala 进行了调整,但经过一些修改后,我让它与 groovy 一起使用。我在下面给你我的整个 build.gradle。这是一个相当新鲜的spring boot项目。
请注意,我可能会删除一些不是针对特定问题的内容(例如 jadira/joda 库)。我留下它们是为了说明您必须将它们作为项目的编译依赖项和 buildscript 依赖项都包含在内。
发生这种情况是因为 Querydsl 的 GenericExporter 需要与它扫描的已编译类相关的所有内容,否则它将失败(好吧,它对我造成了影响)。
最后,我特意保留了 GenericExporter 配置中使用的完全限定类名,这样我就不会用“import”语句污染整个构建脚本。
我的解决方案可能不是最好的,所以我对 cmets 持开放态度。目前,它的工作原理如下:
- 您运行
./gradle compileGroovy 来构建您手动编写的类(包括您在 Groovy 中编写的 @Entity 类`
- 您运行
./gradle generateQueryDSL 应该将“Q”类生成到src/main/generated
- 您终于再次运行
./gradle compileGroovy 以重新构建所有内容,这次包括之前由 generateQueryDSL 生成的源代码。
我使用 gradle 1.11 和 2.1 对其进行了测试。我希望它也对你有用。
//
// Configure the build script itself
//
buildscript {
ext {
querydslGeneratedSourcesDir = file("src/main/generated")
querydslInputEntityPackage = "my.project.domain"
querydslVersion = "3.6.0"
groovyVersion = "2.3.8"
jadiraVersion = "3.2.0.GA"
jodaTimeVersion = "2.6"
jodaMoneyVersion = "0.10.0"
hibernateJpaApiVersion = "1.0.0.Final"
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
// Load Spring Boot plugin
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.9.RELEASE")
// Required by QueryDSL GenericExporter
classpath("com.mysema.querydsl:querydsl-codegen:${querydslVersion}")
classpath("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:${hibernateJpaApiVersion}")
classpath files("${buildDir}/classes/main")
classpath("org.jadira.cdt:cdt:${jadiraVersion}")
classpath("joda-time:joda-time:${jodaTimeVersion}")
classpath("org.joda:joda-money:${jodaMoneyVersion}")
}
}
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
//
// Make both java and groovy files visible only to the Groovy Compiler plugin
//
sourceSets {
main {
java {
srcDirs = []
}
groovy {
srcDirs = ['src/main/java', querydslGeneratedSourcesDir]
}
}
test {
java {
srcDirs = []
}
groovy {
srcDirs = ['src/test/java']
}
}
}
task generateQueryDSL(group: 'build', description: 'Generate QueryDSL query types from compiled entity types') {
new com.mysema.query.codegen.GenericExporter().with {
setKeywords(com.mysema.query.codegen.Keywords.JPA)
setEntityAnnotation(javax.persistence.Entity.class)
setEmbeddableAnnotation(javax.persistence.Embeddable.class)
setEmbeddedAnnotation(javax.persistence.Embedded.class)
setSupertypeAnnotation(javax.persistence.MappedSuperclass.class)
setSkipAnnotation(javax.persistence.Transient.class)
setTargetFolder(querydslGeneratedSourcesDir)
export(querydslInputEntityPackage)
}
}
//
// Configure spring boot plugin
//
bootRepackage {
mainClass = 'my.project.Application'
}
jar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
// end::actuator[]
compile("org.springframework.boot:spring-boot-starter-data-jpa")
// compile("org.springframework.boot:spring-boot-starter-security")
compile("mysql:mysql-connector-java:5.1.34")
compile("org.codehaus.groovy:groovy-all:${groovyVersion}")
compile("cz.jirutka.spring:spring-rest-exception-handler:1.0.1")
compile("com.mysema.querydsl:querydsl-core:${querydslVersion}")
// Joda & Jadira types
compile("joda-time:joda-time:${jodaTimeVersion}")
compile("org.joda:joda-money:${jodaMoneyVersion}")
compile("org.jadira.usertype:usertype.extended:${jadiraVersion}")
compile("org.jadira.cdt:cdt:${jadiraVersion}")
compile("com.fasterxml.jackson.datatype:jackson-datatype-joda:2.4.4")
// Testing
testCompile("junit:junit")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
// Configure the gradle wrapper
task wrapper(type: Wrapper) {
gradleVersion = '2.1'
}