12

I was hoping to bring querydsl into my spring-boot project via gradle. Despite finding a couple of examples online, none of them actually work for me because of issues with dependencies (I think). According to the QueryDSL support forum, gradle is not supported yet. But I was wondering with all the gradle & spring-boot being created if someone has managed to make it work yet?

Here is my build.gradle:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'

buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
    }
}

repositories {
    mavenCentral()
    maven { url: "http://repo.spring.io/libs-snapshot" }
//    maven { url: "http://repo.spring.io/milestone" }

}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.0.0.RC5")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.0.RC5")
    compile("org.springframework:spring-orm:4.0.0.RC1")
    compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
    compile("com.h2database:h2:1.3.172")
    compile("joda-time:joda-time:2.3")
    compile("org.thymeleaf:thymeleaf-spring4")
    compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
    compile('org.codehaus.groovy:groovy-all:2.2.1')
    compile('org.jadira.usertype:usertype.jodatime:2.0.1')

    // this line fails
    querydslapt "com.mysema.querydsl:querydsl-apt:3.3.2"

    testCompile('org.spockframework:spock-core:0.7-groovy-2.0') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }
    testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
    testCompile("junit:junit")
}

jacocoTestReport {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}


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

        java {
            srcDirs = []
        }
        groovy {
            srcDirs = ['src/main/groovy', 'src/main/java']
        }
        resources {
            srcDirs = ['src/main/resources']
        }

        output.resourcesDir = "build/classes/main"
    }

    test {
        java {
            srcDirs = []
        }
        groovy {
            srcDirs = ['src/test/groovy', 'src/test/java']
        }
        resources {
            srcDirs = ['src/test/resources']
        }

        output.resourcesDir = "build/classes/test"
    }
}


configurations {
    // not really sure what this is, I see it in examples but not in documentation
    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 = sourceSets.generated.java.srcDirs.iterator().next()
}

compileJava {
    dependsOn generateQueryDSL
    source generateQueryDSL.destinationDir
}

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

clean {
    delete sourceSets.generated.java.srcDirs
}

idea {
    module {
        sourceDirs += file('src/main/generated')
    }
}

But gradle fails with:

Could not find method querydslapt() for arguments [com.mysema.querydsl:querydsl-apt:3.3.2] 

I have tried changing the querydsl-apt version to earlier ones but I get the same error.

sonoerin
  • 5,015
  • 23
  • 75
  • 132
  • Can I ask you where you copied the compile configuration from? There are several errors in it and it would be good to locate the source and fix it there. – Dave Syer Apr 01 '14 at 06:43
  • I found examples here (https://gist.github.com/EdwardBeckett/5377401), here (http://stackoverflow.com/questions/6431026/generating-jpa2-metamodel-from-a-gradle-build-script), and the querydsl support forum. – sonoerin Apr 03 '14 at 02:13

2 Answers2

5

Working configuration for Spring Boot 1.3.5 and supported QueryDSL, tested with gradle 2.14.

ext {
    queryDslVersion = '3.6.3'
    javaGeneratedSources = file("$buildDir/generated-sources/java")
}

compileJava {
    doFirst {
        javaGeneratedSources.mkdirs()
    }
    options.compilerArgs += [
            '-parameters', '-s', javaGeneratedSources
    ]
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile "com.mysema.querydsl:querydsl-jpa:$queryDslVersion"

    compileOnly "com.mysema.querydsl:querydsl-apt:$queryDslVersion:jpa"
}

Complete project source code: spring-boot-querydsl

MariuszS
  • 30,646
  • 12
  • 114
  • 155
4

You probably need to do at least 2 things:

  1. Declare the "querydslapt" configuration before you use it

  2. Add querydsl-jpa (or whatever flavours you need) to your "compile" configuration.

Then you will have the classpath set up, but the apt bit will not do anything without some more configuration (as you found no doubt from the querydsl support forum). The apt but is used to generate some code that you then need to compile and use in your application code (the "Q*" classes corresponding to your domain objects). You could drive that from a build task in gradle I imagine (it only has to run once for every change in the domain objects).

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • Thank you Dave for your constant help. I am still struggling with this because I still don't really understand gradle. I will step aside and see if I can educate myself better. In the meantime, I'll just pass on querydsl in my project. – sonoerin Apr 03 '14 at 02:08