带有QueryDSL和Gradle的IntelliJ 15

时间:2016-07-13 13:24:31

标签: gradle spring-boot querydsl

我使用gradle 2.3和QueryDSL 4.1.3在IntelliJ 15中有一个Spring-boot 1.4项目,因为我的实体没有被Querydsl构建到Q类中。我有以下内容:

 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/')
    }
}

当我运行单元测试时,我收到错误:

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

当我运行“gradle clean build”时,我得到: 找不到com.mysema.querydsl:querydsl-apt:。

  

在以下位置搜索:         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

我意识到QueryDsl在3&之间做了一些包重构。 4,但看起来我的依赖项无法找到,即使它们显然在这里:https://mvnrepository.com/artifact/com.querydsl/querydsl-jpa/4.1.3

至少,这是我目前关于为什么我的构建失败的理论。有没有人得到最新的春天,gradle和querydsl工作?

更新: 我删除了ewerk插件并简化了我的build.gradle文件,所以现在一切都正常构建。我正在更新以帮助其他可能需要此功能的人:

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 个答案:

答案 0 :(得分:12)

您是否看到您的QClasses在Gradle输出中生成?从你的错误来看,它似乎已经通过它应该生成它们的点。

我认为问题是你没有配置JPAAnnotationProcessor。这样做是为了方便gradle,方法是将jpa附加到你的querydsl-apt依赖项中。在Maven中,您应用插件manually

我的build.gradle中有以下与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

构思块只是在IDEA中自动配置生成的源目录,以便in-IDE构建正常工作。

编辑:

JPAAnnotationProcessor输出如下所示。

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]

编辑:

我对ewerk插件并不熟悉,所以我看了。它似乎正在尝试为您激活JPAAnnotationProcessor。您可能需要根据文档here设置JPA标记,因为它默认为false。

请参阅有关依赖性问题的评论主题。 编辑:对于Gradle 4.6+,您可以使用annotationProcessor语法。

api "com.querydsl:querydsl-root:$querydslVersion"
api "com.querydsl:querydsl-jpa:$querydslVersion"
annotationProcessor "com.querydsl:querydsl-apt:$querydslVersion:jpa"

答案 1 :(得分:0)

如果有人想通过Kotlin和Gradle kotlin dsl这样做,请按照以下方式进行操作:

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")
}

请注意,在Kotlin 1.2.20中修复Kapt错误之前,您可能需要使用Java 8 for Gradle。