简体   繁体   中英

Java QueryDSL and Gradle Kotlin DSL

We are migrating our Groovy gradle build files to KotlinDSL and I am stuck with configuring the generation of QueryDSL classes.

The entity classes are written in java and the QDSL files should be in java too.

The working groovy build file code snippet:

sourceSets {
    generated.java { srcDirs = ['src/main/generated']}
}

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 = sourceSets.generated.java.srcDirs.iterator().next()
}

compileJava {
    dependsOn generateQueryDSL
    source generateQueryDSL.destinationDir
}

compileGeneratedJava {
    dependsOn generateQueryDSL
    options.warnings = false
    classpath += sourceSets.main.runtimeClasspath
}

This is my rewrite of the code in KotlinDSL:

val querydsl by configurations.creating

val generated by java.sourceSets.creating {
    java.srcDirs("build/generated/java")
}

tasks {
    val generateQueryDsl by creating(JavaCompile::class.java) {
        group = "build"
        description = "Generate QueryDSL classes"
        classpath = configurations.compile + querydsl
        options.compilerArgs = listOf("-proc:only", "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor")
        destinationDir = generated.java.srcDirs.first()
    }

    getByName<JavaCompile>("compileJava") {
        dependsOn(generateQueryDsl)
        source(generateQueryDsl.destinationDir)
    }

    getByName<JavaCompile>("compileGeneratedJava") {
        dependsOn(generateQueryDsl)
        options.isWarnings = false
        classpath += java.sourceSets["main"].runtimeClasspath
    }
}

When I run the generateQueryDsl task, I got NO-SOURCE and no QDSL files generated:

> Task :common:compileKotlin UP-TO-DATE
> Task :common:generateGrammarSource UP-TO-DATE
> Task :common:compileJava UP-TO-DATE
> Task :common:processResources UP-TO-DATE
> Task :common:classes UP-TO-DATE
> Task :common:inspectClassesForKotlinIC UP-TO-DATE
> Task :common:jar UP-TO-DATE
> Task :common-db:compileKotlin UP-TO-DATE
> Task :common-db:compileJava UP-TO-DATE
> Task :common-db:processResources UP-TO-DATE
> Task :common-db:classes UP-TO-DATE
> Task :common-db:inspectClassesForKotlinIC UP-TO-DATE
> Task :common-db:jar UP-TO-DATE
> Task :core:generateQueryDsl NO-SOURCE

I am not sure, but have a feeling that I could do this generation much simpler, but I ran out of the ideas.

There is another way to generate the QueryDSL Q* classes, available with latest Gradle versions ( I think >= 4.9) : below is the Groovy version of the script, you should try making the Kotlin version from it:

sourceSets {
    generated.java {
        srcDir file('/build/generated/java')
    }
}

dependencies {
    compile("com.querydsl:querydsl-jpa:${queryDslVersion}")
    compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:${hibernateJpaVersion}"

    annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
    annotationProcessor "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:${hibernateJpaVersion}"
}
compileJava {
    options.annotationProcessorPath = configurations.annotationProcessor
    options.setAnnotationProcessorGeneratedSourcesDirectory(file("$projectDir/build/generated/java"))
}

These QueryDSL classes will be automatically generated during Java compilation task execution (annotation processor), you don't need to define a dedicated task anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM