spring - 将 Spring Boot 应用程序迁移到最新的 Java 版本(Java 15)

标签 spring spring-boot kotlin gradle build.gradle

我有一个 spring boot 项目设置为使用 Java 8 和 Kotlin。我想将该项目迁移到 Java 15,但不幸的是我没有找到任何对此有用的文档。

到目前为止,我已经能够将我的 Spring Boot 版本从 2.3.3.RELEASE 升级到 2.4.0 版,并根据使用 spring initializr 创建的项目示例引用该项目以在 jvmTarget 中使用 Java 14。

但是,我没有找到升级到 Java 15 的方法。任何想法都将不胜感激。

这是我的 build.gradle 的样子:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.lang.System.getenv

buildscript {
repositories {
    maven {
        url = uri("https://plugins.gradle.org/m2/")
    }
 }
 dependencies {
    classpath("org.jmailen.gradle:kotlinter-gradle:3.0.2")
 }
}

plugins {
 id("org.springframework.boot") version "2.4.0"
 id("io.spring.dependency-management") version "1.0.10.RELEASE"
 kotlin("jvm") version "1.4.0"
 kotlin("plugin.spring") version "1.4.0"
 id ("org.owasp.dependencycheck") version "5.3.2"
 id ("com.github.spotbugs") version "4.5.0"
 id ("org.jmailen.kotlinter") version "3.0.2"
 `jacoco`
}

tasks.named<org.springframework.boot.gradle.tasks.run.BootRun>. 
("bootRun") {
 args("--spring.profiles.active=local")
}

jacoco {
toolVersion = "0.8.6"
}

group = "au.project"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
mavenCentral()
maven {
    url = uri("https://...")
    credentials {
        username = getenv("ARTIFACTORY_USER")
        password = getenv("ARTIFACTORY_PASSWORD")
     }
  }
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter- 
webflux")
implementation("com.fasterxml.jackson.module:jackson-module- 
 kotlin")
implementation("io.github.microutils:kotlin-logging:1.8.3")
implementation("io.projectreactor.kotlin:reactor-kotlin- 
extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
implementation("org.owasp:dependency-check-gradle:5.3.2")
implementation("org.springdoc:springdoc-openapi-kotlin:1.4.8")
implementation("org.springdoc:springdoc-openapi-webflux-ui:1.4.8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter- 
test") {
    exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
 }
  testImplementation("io.projectreactor:reactor-test")
  testImplementation("com.github.tomakehurst:wiremock-jre8:2.27.2")
  testImplementation("com.openpojo:openpojo:0.8.6")
  }

 tasks.withType<Test> {
 useJUnitPlatform()
 }

 tasks.withType<KotlinCompile> {
 kotlinOptions {
    freeCompilerArgs = listOf("-Xjsr305=strict")
    jvmTarget = "14"
 }
}
....

注意:为 build.gradle 中的 kotlinOptions 属性更改 jvmTarget = 15,我收到以下异常:“Unknown JVM target version: 15 Supported version: 1.6, 1.8, 9,10,11,12,12 ,14. Kotlin是否不支持Java 15?

最佳答案

JDK 15 需要 Gradle 6.7.x,如果您还没有更新 Gradle,请先更新。

我建议您使用新的 toolchain Gradle 的特点。如果无法在主机系统上检测到它,它将下载构建代码所需的 JDK,以确保使用所需的 JDK 版本执行构建。这也简化了混合团队在不同环境中的正确构建。

java {
    toolchain {
        // automatically download a jdk for the build if not available
        languageVersion.set(JavaLanguageVersion.of(11))
    }
}

这不能与 java.sourceCompatibilityjava.targetCompatibility 一起使用,您需要删除这些配置。

关于spring - 将 Spring Boot 应用程序迁移到最新的 Java 版本(Java 15),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64924862/

相关文章:

android - 当 LiveData<MVoice> 在 Android Studio 中的双向数据绑定(bind)中更改时,如何重置 LiveData<Boolean>?

android - RecyclerView 未绑定(bind)数据 notifyDataSetChanged 在过滤数据时不起作用

android - 将聊天气泡对准RecyclerView的左侧

java - Spring JdbcTemplate 在创建存储过程时抛出错误

java - 限制JSON数据请求

spring - org.hibernate.UnknownEntityTypeException : Unable to locate persister: entity. 设置

java - 没有使用 Spring Security 进行身份验证和授权

spring - 如何在 Spring AMQP 中使用拦截器

java - 覆盖 spring boot 包 jar 中的 messages.properties

java - Spring Boot rest API 编码错误