【问题标题】:Querydsl Code generation for groovy with gradle使用 gradle 为 groovy 生成 Querydsl 代码
【发布时间】:2014-10-22 15:56:48
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

    标签: jpa groovy gradle querydsl


    【解决方案1】:

    我今天想出了一个可行的解决方案。它结合了我在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”语句污染整个构建脚本。

    我的解决方案可能不是最好的,所以我对 cme​​ts 持开放态度。目前,它的工作原理如下:

    1. 您运行 ./gradle compileGroovy 来构建您手动编写的类(包括您在 Groovy 中编写的 @Entity 类`
    2. 您运行./gradle generateQueryDSL 应该将“Q”类生成到src/main/generated
    3. 您终于再次运行 ./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'
        }
    

    【讨论】:

      【解决方案2】:

      感谢分享。

      没有找到其他解决方案来查询 groovy 生成的 DSL。 我修改了一下,现在可以做:

      ./gradlew clean build
      

      从 .java 和 .groovy 文件生成“Q 文件”,当然还要编译它们。

      我将示例推送到 github: https://github.com/eprogramming/querydsl-groovy-java-gradle

      但我不知道为什么,这只有在我运行“./gradlew clean build”两次时才有效。

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-12
        • 2020-09-30
        • 2019-03-25
        • 2019-05-31
        • 2021-05-08
        • 2019-12-14
        • 2022-10-09
        • 2021-07-21
        相关资源
        最近更新 更多