10

I have a Spring-boot 1.4 project in IntelliJ 15 using gradle 2.3 and QueryDSL 4.1.3 that won't build because my entities are not being built into Q classes by Querydsl. I have the following:

 buildscript {
    ext {
        springBootVersion = '1.4.0.BUILD-SNAPSHOT'
        querydslVersion = '4.1.3'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
        maven {url "https://plugins.gradle.org/m2/"}
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "gradle.plugin.com.ewerk.gradle.plugins:querydsl-plugin:1.0.7"
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'groovy'
apply plugin: "com.ewerk.gradle.plugins.querydsl"

jar {
    baseName = 'billing'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
    maven { url "https://repo.spring.io/libs-snapshot" }
    maven { url "https://repo.spring.io/libs-snapshot"}
    maven {url "https://plugins.gradle.org/m2/"}

}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile "com.querydsl:querydsl-root:$querydslVersion"
    compile "com.querydsl:querydsl-jpa:$querydslVersion"
    compile "com.querydsl:querydsl-apt:$querydslVersion:jpa"

    compile 'org.codehaus.groovy:groovy-all:2.4.1'
    compile fileTree(dir: "libs/qbo-sdk-2.5.0", include: "*.jar")

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }

    testCompile('org.spockframework:spock-spring:1.0-groovy-2.4') {
        exclude group: 'org.spockframework', module: 'spock-core'
    }
    testCompile("junit:junit")

}

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

querydsl {
    library = 'com.mysema.querydsl:querydsl-apt'
    querydslDefault = true
}


test {
    reports.junitXml.destination = file('build/test-results/folder')
}

jacoco {
    toolVersion = "0.7.0.201403182114"
}

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

configurations {
    querydslapt
}

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

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

when I run my unit tests I get the error:

Caused by: java.lang.ClassNotFoundException: com.company.billing.customer.QCustomer

And when I run "gradle clean build" I get: Could not find com.mysema.querydsl:querydsl-apt:.

Searched in the following locations: https://repo1.maven.org/maven2/com/mysema/querydsl/querydsl-apt//querydsl-apt-.pom https://repo1.maven.org/maven2/com/mysema/querydsl/querydsl-apt//querydsl-apt-.jar https://jcenter.bintray.com/com/mysema/querydsl/querydsl-apt//querydsl-apt-.pom https://jcenter.bintray.com/com/mysema/querydsl/querydsl-apt//querydsl-apt-.jar https://repo.spring.io/snapshot/com/mysema/querydsl/querydsl-apt//querydsl-apt-.pom https://repo.spring.io/snapshot/com/mysema/querydsl/querydsl-apt//querydsl-apt-.jar https://repo.spring.io/milestone/com/mysema/querydsl/querydsl-apt//querydsl-apt-.pom https://repo.spring.io/milestone/com/mysema/querydsl/querydsl-apt//querydsl-apt-.jar https://repo.spring.io/libs-snapshot/com/mysema/querydsl/querydsl-apt//querydsl-apt-.pom https://repo.spring.io/libs-snapshot/com/mysema/querydsl/querydsl-apt//querydsl-apt-.jar

I realize that QueryDsl did some package refactoring between 3 & 4, but it looks like my dependencies are unable to be found even though they are clearly here: https://mvnrepository.com/artifact/com.querydsl/querydsl-jpa/4.1.3

At least, that is my current theory on why my build fails. Has anyone gotten the latest spring, gradle, and querydsl to work?

Update: I removed the ewerk plugin and simplified my build.gradle file so everything builds properly now. I am updating to help anyone else who might need this:

buildscript {
    ext {
        springBootVersion = '1.4.0.BUILD-SNAPSHOT'
        querydslVersion = '4.1.3'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

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

jar {
    baseName = 'billing'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
    maven { url "https://repo.spring.io/libs-snapshot" }
    maven { url "https://repo.spring.io/libs-snapshot"}
    maven {url "https://plugins.gradle.org/m2/"}
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile "com.querydsl:querydsl-root:$querydslVersion"
    compile "com.querydsl:querydsl-jpa:$querydslVersion"
    compile "com.querydsl:querydsl-apt:$querydslVersion:jpa"

    compile 'org.codehaus.groovy:groovy-all:2.4.1'
    compile fileTree(dir: "libs/qbo-sdk-2.5.0", include: "*.jar")

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }

    testCompile('org.spockframework:spock-spring:1.0-groovy-2.4') {
        exclude group: 'org.spockframework', module: 'spock-core'
    }
    testCompile("junit:junit")
}

test {
    reports.junitXml.destination = file('build/test-results/folder')
}

jacoco {
    toolVersion = "0.7.0.201403182114"
}

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

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

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

2 Answers 2

12

Do you see your QClasses being generated in the Gradle output? From your error it looks like it's already passed the point where it should generate them.

I think the problem is you are not configuring the JPAAnnotationProcessor. This is done as a convenience in gradle by appending :jpa to your querydsl-apt dependency. In Maven, you apply the plugin manually.

I have the below in my build.gradle related to querydsl.

idea {
    module {
        sourceDirs += file('generated/')
        generatedSourceDirs += file('generated/')
    }
}
[...]
compile "com.querydsl:querydsl-root:$querydslVersion"
compile "com.querydsl:querydsl-jpa:$querydslVersion"
compile "com.querydsl:querydsl-apt:$querydslVersion:jpa

The idea block just auto-configures the generated source dir in IDEA, so that in-IDE builds work correctly.

EDIT:

The JPAAnnotationProcessor output looks like the below.

Note: Running JPAAnnotationProcessor
Note: Serializing Supertypes
Note: Generating com.myclass.example.QMappedSuperClass for [com.myclass.example.MappedSuperClass]
Note: Serializing Entity types
Note: Generating com.myclass.example.QMyClass for [com.myclass.example.MyClass]

EDIT:

I wasn't familiar with the ewerk plugin, so I looked. It appears that it is trying to activate the JPAAnnotationProcessor for you. You may need to set the JPA flag per the documentation here as it defaults to false.

See comment thread regarding dependency issues. EDIT: For Gradle 4.6+, you can use the annotationProcessor syntax.

api "com.querydsl:querydsl-root:$querydslVersion"
api "com.querydsl:querydsl-jpa:$querydslVersion"
annotationProcessor "com.querydsl:querydsl-apt:$querydslVersion:jpa"
8
  • I do not see any generated classes before or after I added your changes to my gradle file but I still get the same error that Could not find com.mysema.querydsl:querydsl-apt:. Searched in the following locations:....
    – sonoerin
    Jul 13, 2016 at 15:08
  • Sorry. I missed your dependency error. I'm not sure why you're trying to resolve the older com.mysema version. Can you run gradlew dependencies and find it in the dependency tree? Jul 13, 2016 at 15:40
  • I thought I was using the latest QueryDSL? As I am just setting up my project the only explicit use of QueryDSL is com.mysema.query.types.Predicate When I run gradlew dependency I still get the build failure ould not resolve all dependencies for configuration 'detachedConfiguration5'. > Could not find com.mysema.querydsl:querydsl-apt
    – sonoerin
    Jul 13, 2016 at 16:17
  • Why are you using com.mysema.query.types.Predicate instead of com.querydsl.core.types.Predicate? Your dependency configuration is for the com.querydsl group (version 4+), not the com.mysema group (version <3.7.4). Run gradlew dependencies not gradlew dependency. Even if your dependency is not found, it should tell you who is dependent on that jar, then you try to alter it. Is there more to your build file? You're importing the spring-boot-starter-test, but I don't see any other spring-boot dependencies. Jul 13, 2016 at 16:30
  • I updated my question with the entire build.gradle file - hopefully it makes more sense. I can't com.querydsl.core.types.Predicate because that dependency isn't found - which I guess highlights that my querydsl isn't found. I ran gradlew dependencies but it still failed with Could not resolve all dependencies for configuration 'detachedConfiguration5' I can view querydsl 4.1.3 on mvnrepository but gradle doesn't seem to find it?
    – sonoerin
    Jul 13, 2016 at 16:50
2

If anyone wants to do this with Kotlin and the Gradle kotlin dsl here's how you do it with that setup:

build.gradle.kts

plugins {
    [...]
    id("org.jetbrains.kotlin.kapt") version kotlinVersion
}

dependencies {
    [...]
    compile("com.querydsl:querydsl-core:$queryDslVersion")
    compile("com.querydsl:querydsl-jpa:$queryDslVersion")
    kapt("com.querydsl:querydsl-apt:$queryDslVersion:jpa")
}

Note you may need to use Java 8 for Gradle until a Kapt bug is fixed in Kotlin 1.2.20.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.