Querydsl Code generation for groovy with gradle

柔情痞子 提交于 2019-12-07 04:17:55

问题


I have entity beans defined in groovy. I'm not being able to generate querydsl code for entities in groovy.

This Gradle Querydsl Configuration works fine for entity beans in Java but not for groovy.

Referring to Querydsl documentation on Alternative code generation, it states that GenericExporter have to used to generate code for groovy entities. But I could not figure out how to configure GenericExporter in gradle.

Please share if anyone have been able to generate querydsl code for entities in groovy with gradle.

Thanks


回答1:


There seems to be a working solution I came up with just today. It combines the information provided by the "Classpath based code generation" chapter of the QueryDSL manual and the build script of some "tasca" project written I found on github (thanks a lot to the author).

The build script I found on github was adjusted for Scala, but with some modifications I got it working with groovy. I'm giving you my whole build.gradle below. It's a quite fresh spring boot project.

Notice that I could be removing some stuff that are not problem-specific (like the jadira/joda libraries). I left them in order to make clear that you have to include them both as compile dependencies of the project and as buildscript dependencies.

This happens because Querydsl's GenericExporter needs everything associated with the compiled classes it scans, otherwise it will fail (well, it did to me).

Finally, I purposely left the fully qualified class names used in the GenericExporter configuration, so that I don't pollute the whole build script with "import" statements.

My solution is probably not the best, so I'm open to comments. Currently, it works as follows:

  1. You run ./gradle compileGroovy to build your manually written classes (included the @Entity classes you have written in Groovy`
  2. You run ./gradle generateQueryDSL which should produce the "Q" classes into src/main/generated
  3. You finally run ./gradle compileGroovy again to re-build everything, this time including the sources that were previously generated by generateQueryDSL.

I tested it with gradle 1.11 and 2.1. I hope it works for you, too.



    //
    // 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:


thanks for share.

Found no other solution to queryDSL generation to groovy. I modified a bit and now is possible to do:

./gradlew clean build

To generate 'Q files' from .java and .groovy files, and of course compile them.

I pushed the sample to github: https://github.com/eprogramming/querydsl-groovy-java-gradle

But i don't know why, this is working only when i run "./gradlew clean build" twice.

Thanks.



来源:https://stackoverflow.com/questions/26511801/querydsl-code-generation-for-groovy-with-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!