Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ 15 with QueryDSL and Gradle

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/')
    }
}
like image 531
sonoerin Avatar asked Jul 13 '16 13:07

sonoerin


2 Answers

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.

like image 92
CorayThan Avatar answered Oct 02 '22 11:10

CorayThan


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"
like image 28
JudgingNotJudging Avatar answered Oct 02 '22 11:10

JudgingNotJudging