【问题标题】:Upgrade Spring Boot app (using javax.persistence) to JDK 11将 Spring Boot 应用程序(使用 javax.persistence)升级到 JDK 11
【发布时间】:2019-01-21 01:14:21
【问题描述】:

我正在将 JDK 8 应用程序转换为 JDK 11。它使用 Gradle 构建并在后台使用 JPA/Hibernate。我已经升级了必要的 Gradle 模块,Spring Boot 现在是 2.1,它使用的是 Gradle 5.1 包装器。它使用 QueryDSL JPA 注释处理从 PostgreSQL 数据库构建 Q* 类。

构建脚本中的以下任务生成上述类:

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

//    dependsOn generateQueryDSL
}

而被注释掉的QueryDSL生成任务是:

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.querydsl.apt.jpa.JPAAnnotationProcessor"
    ]
    destinationDir = generatedSourcesDir
}

我在运行 Gradle compileJava 任务时收到的主要错误如下:

* What went wrong:
Execution failed for task ':app-base:compileJava'.
> java.lang.NoClassDefFoundError: javax/persistence/Entity

我正在使用以下构建依赖项:

compile group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.2.1'

自去年夏天以来一直没有更新。有什么方法可以让 JavaCompile Gradle 任务与 JDK 11 和 JPA 2.2 一起工作?

2019 年 2 月 1 日更新 - 用户请求的 API 项目的 build.gradle(适用于 JDK 11 和 Spring Boot 2.1 和 Gradle 4.8,但对于 Gradle 5 需要进行大量清理)

buildscript {
    repositories {
        jcenter()
        mavenLocal()
        mavenCentral()
        maven { url "http://repo.spring.io/libs-release" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "jacoco"

jar {
    enabled = true
}

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

ext {
    generatedSourcesDir = file("${buildDir}/generated-sources")
    pojogenGeneratedDir = file("src/pojogen")
}

sourceSets {
    main {
        java {
            srcDir "src/main/java"
            srcDir pojogenGeneratedDir
            srcDir generatedSourcesDir
        }
    }
}

configurations {
    provided
    compile.extendsFrom provided
}

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

jar {
    enabled = true
}

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

dependencies {

    /*
        SPRING
     */

    testCompile("org.springframework.boot:spring-boot-starter-test:2.1.2.RELEASE")

    // tag::tomcat[]
    compile("org.springframework.boot:spring-boot-starter-web:2.1.2.RELEASE") {
        exclude module: "spring-boot-starter-jetty:2.1.2.RELEASE"
    }
    compile("org.springframework.boot:spring-boot-starter-tomcat:2.1.2.RELEASE")
    // end::tomcat[]
    // tag::actuator[]
    compile("org.springframework.boot:spring-boot-starter-actuator:2.1.2.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-security:2.1.2.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:2.1.2.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-batch:2.1.2.RELEASE")
    compile("org.springframework.boot:spring-boot-devtools:2.1.2.RELEASE")
    // end::actuator[]

    /*
        APACHE COMMONS
     */

    // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'

    // https://mvnrepository.com/artifact/commons-io/commons-io
    compile group: 'commons-io', name: 'commons-io', version: '2.6'

    /*
        GOOGLE GUAVA
     */

    // https://mvnrepository.com/artifact/com.google.guava/guava
    compile group: 'com.google.guava', name: 'guava', version: '27.0.1-jre'

    /*
        LOGBACK
     */

    // https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
    testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

    /*
        MOCKITO
     */

    // https://mvnrepository.com/artifact/org.mockito/mockito-core
    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.23.4'

    /*
        JWT
     */

    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'

    /*
        MAIL
     */

    // https://mvnrepository.com/artifact/com.sun.mail/javax.mail
    compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'

    // https://mvnrepository.com/artifact/javax.mail/javax.mail-api
    compile group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2'

    /*
        SWAGGER
     */

    // https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'

    // https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'

    /*
        PASSWORD VALIDATOR
    */

    // https://mvnrepository.com/artifact/org.passay/passay
    compile group: 'org.passay', name: 'passay', version: '1.3.1'

    /*
        LOMBOK
     */

    // https://mvnrepository.com/artifact/org.projectlombok/lombok
    provided group: 'org.projectlombok', name: 'lombok', version: '1.18.4'

    /*
        Thymeleaf
     */

//    // https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf
//    compile group: 'org.thymeleaf', name: 'thymeleaf', version: '3.0.9.RELEASE'
//
//    // https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4
//    compile group: 'org.thymeleaf', name: 'thymeleaf-spring4', version: '3.0.9.RELEASE'

    /*
        H2 (unit testing)
     */

    // https://mvnrepository.com/artifact/com.h2database/h2
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'

    /*
        Thumbnail tools
     */

    // https://mvnrepository.com/artifact/net.coobird/thumbnailator
    compile group: 'net.coobird', name: 'thumbnailator', version: '0.4.8'

    /*
        JUNIT
     */

    testCompile("junit:junit")
}

/*
    BUILD EXCLUSIONS
 */

test {
    systemProperties 'property': 'value'
    testLogging {
        events "passed", "skipped", "failed", "standardOut", "standardError"
    }
}

/*
    JaCoCo
 */
jacoco {
    toolVersion = "0.7.6.201602180812"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}

【问题讨论】:

  • 看起来您需要添加缺少依赖项的 JAR。
  • 您不需要自定义任务,因为 Gradle 会自动检测并添加注释处理器。您应该为所需的依赖项使用 annotationProcessor 配置。此外,在使用 Spring Boot 时,您已经需要 hibernate 和 JPA 的依赖项,并且您不需要额外的依赖项。

标签: java spring jpa gradle java-11


【解决方案1】:

我通过将 Gradle 回滚到 4.8 并回滚 Hibernate 依赖项,让它与 JDK 11 一起工作。如果对我正在使用的确切依赖项感兴趣,请进一步询问。

【讨论】:

  • 您能否编辑答案并附上 build.gradle。 :) @mike-melusky
  • 这是我的 API 项目的 build.gradle(spring boot 2.1,jdk 11,gradle 4.8。)我需要清理一些东西,因为其他用户已经提到过让 gradle 5 更好,但是对于暂时有效。
猜你喜欢
  • 2020-06-17
  • 2021-09-10
  • 2022-10-01
  • 1970-01-01
  • 2019-06-06
  • 2016-09-01
  • 2023-02-26
  • 2020-04-14
  • 2021-01-18
相关资源
最近更新 更多