jpa - 使用 gradle 为 groovy 生成 Querydsl 代码

标签 jpa groovy gradle querydsl

我在 groovy 中定义了实体 bean。我无法为 groovy 中的实体生成 querydsl 代码。

Gradle Querydsl Configuration适用于 Java 中的实体 bean,但不适用于 groovy。

引用 Alternative code generation 上的 Querydsl 文档,它指出 GenericExporter必须用于为 groovy 实体生成代码。但我不知道如何配置 GenericExporter在毕业。

如果有人能够使用 gradle 为 groovy 中的实体生成 querydsl 代码,请分享。

谢谢

最佳答案

我今天想出了一个可行的解决方案。它结合了 "Classpath based code generation" chapter of the QueryDSL manual 提供的信息和 the build script of some "tasca" project written我在github上找到的(非常感谢作者)。

我在 github 上找到的构建脚本针对 Scala 进行了调整,但经过一些修改后,我让它与 groovy 一起工作。我在下面给你我的整个 build.gradle。这是一个相当新鲜的 Spring 启动项目。

请注意,我可能会删除一些与问题无关的内容(例如 jadira/joda 库)。我留下它们是为了明确你必须将它们作为项目的编译依赖项和 buildscript 依赖项包含在内。

发生这种情况是因为 Querydsl 的 GenericExporter 需要与它扫描的编译类相关的所有内容,否则它将失败(好吧,它对我来说)。

最后,我特意留下了 GenericExporter 配置中使用的完全限定类名,这样我就不会用“import”语句污染整个构建脚本。

我的解决方案可能不是最好的,所以我愿意发表评论。目前,它的工作原理如下:

  • 你跑 ./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'
        }
    

    关于jpa - 使用 gradle 为 groovy 生成 Querydsl 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26511801/

    相关文章:

    java - android中的productFlavors和包

    java - 从 Artifactory 远程存储库下载大型 Artifact

    java - 来自一个测试类的日志显示在 Gradle 测试报告中另一类的标准输出部分中

    java - 审计数据库回滚

    java - 如何在Java中自动加载具有@ManyToMany关系的实体

    groovy - 如何在groovy脚本中将数组排序为3个元素的组

    Grails/Groovy 域类继承转换

    java - 通过 id 而不是对象添加与实体的关系

    javax.persistence.PersistenceException : org. hibernate.exception.GenericJDBCException:无法执行查询

    groovy - 局部变量的名称查找