Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querydsl Code generation for groovy with gradle

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

like image 585
TheKojuEffect Avatar asked Oct 22 '14 15:10

TheKojuEffect


1 Answers

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'
    }
like image 67
Kostas Filios Avatar answered Nov 15 '22 08:11

Kostas Filios