Suresh Payankannur

Friday, September 5, 2014

Querydsl JPA Model Generation with Gradle

Querydsl is a very useful tool to write concise JPA queries. This requires a plugin to generate the meta model classes that will be referenced in the queries. There is a maven plugin to handle the code generation. But when using Gradle, this needs to be configured manually. Add this to the build.gradle file.
ext {
    generatedSourcesDir = file("${buildDir}/generated-sources")
    querydslVersion    = "3.4.2"
}
sourceSets {
    main {
        java {
            srcDir "src/main/java"
            srcDir generatedSourcesDir
        }
    }
}
configurations {
    querydslapt
}
task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
    source = sourceSets.main.java
    classpath = configurations.compile + configurations.querydslapt
    options.compilerArgs = [
            "-proc:only",
            "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"
    ]
    destinationDir = generatedSourcesDir
}
compileJava {
    doFirst {
        generatedSourcesDir.mkdirs();
    }
    options.compilerArgs += ['-s', generatedSourcesDir]

    dependsOn generateQueryDSL
}
dependencies {
      compile  "com.mysema.querydsl:querydsl-core:${querydslVersion}"
      compile  "com.mysema.querydsl:querydsl-jpa:${querydslVersion}"
      querydslapt  "com.mysema.querydsl:querydsl-apt:${querydslVersion}"
}

1 comment:

  1. Hello,

    I pretty much copy pasted the same entries as your article to my gradle.build. Having complie time errors. When I run my gradle build I do see that Q classes are getting generated but process fail with Q classes cannot resolve error in my Controller class where I am using these auto generated class. Any idea what could be causing this?

    Below are my gradle.build entries:

    ext{
    generatedSrcDir = file("src/main/generated")
    }
    sourceSets {
    generated {
    java {
    srcDir "src/main/java"
    srcDir generatedSrcDir
    }
    }
    }

    configurations {
    querydslapt
    }

    task generateQueryDSL (type: JavaCompile, group: 'build', description: 'Generates the QueryDSL') {
    source = sourceSets.main.java
    classpath = configurations.compile + configurations.querydslapt

    options.compilerArgs = [
    "-proc:only",
    "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"
    ]
    destinationDir = generatedSrcDir //sourceSets.generated.java.srcDirs.iterator().next()
    }

    compileJava {

    doFirst {
    generatedSrcDir.mkdirs();
    }
    options.compilerArgs += ['-s', generatedSrcDir]

    dependsOn generateQueryDSL
    }

    dependencies {
    def querydslVersion = "3.4.2"
    compile "com.mysema.querydsl:querydsl-core:${querydslVersion}"
    compile "com.mysema.querydsl:querydsl-jpa:${querydslVersion}"
    querydslapt "com.mysema.querydsl:querydsl-apt:${querydslVersion}"
    }

    Thanks,
    Annapurna

    ReplyDelete

Blog Archive

Scroll To Top