Migrating Spring Boot Applications to the Latest Java Version (Java 15) - spring

I have a spring boot project set up to use Java 8 and Kotlin. I want to migrate the project to Java 15, but unfortunately I haven't found any documentation useful for that purpose.
So far i was able to upgrade my Spring boot version from 2.3.3.RELEASE to version 2.4.0 and reference the project to use Java 14 in jvmTarget based on a project example created using the spring initializr.
However, I didn't the find a way to upgrade to Java 15. Any ideas would be very appreciate it.
This is how my build.gradle looks like:
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"
}
}
....
Note: Chaning the jvmTarget = 15 for the kotlinOptions property within the build.gradle, I am getting the following exception: "Unknown JVM target version: 15 Supported version: 1.6, 1.8, 9,10,11,12,12,14. Does it means Kotlin does not support Java 15?

JDK 15 requires Gradle 6.7.x, please update Gradle first, if you haven't done so, yet.
I recommend you use the new toolchain feature of Gradle. It will download the required JDK to build your code, if it cannot detect it on the host system, to guarantee that the build is executed with the required JDK version. This also simplifies the right build in different environments with a mixed team.
java {
toolchain {
// automatically download a jdk for the build if not available
languageVersion.set(JavaLanguageVersion.of(11))
}
}
This does not work together with java.sourceCompatibility and java.targetCompatibility, you would need to remove these configurations.

First of all, you need to upgrade to latest version of Gradle, i.e (6.7.x) to support JDK 15.
Add or update the following lines in build.gradle :
java.sourceCompatibility = JavaVersion.VERSION_15
java.targetCompatibility = JavaVersion.VERSION_15

Related

Unresolved symbol:mainClassName

I have a SpringBoot project with the following build.gradle:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.7.0"
id("io.spring.dependency-management") version "1.0.9.RELEASE"
kotlin("jvm") version "1.7.0"
kotlin("plugin.spring") version "1.7.0"
kotlin("plugin.jpa") version "1.6.21"
id("org.flywaydb.flyway") version "6.4.4"
}
group = "com.planes"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
dependencies {
implementation("org.apache.tomcat.embed:tomcat-embed-jasper")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("io.jsonwebtoken:jjwt:0.9.0")
implementation("org.postgresql:postgresql")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
testImplementation("org.springframework.security:spring-security-test")
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
springBoot {
mainClassName = "com.planes.planesserver.DemoApplication"
}
When I run the Gradle Wrapper Task I get the following error: "Unresolved reference: mainClassName"
This happens after I updated SpringBoot from Version 2.2 to 2.7 and gradle from version 5 to version 6.9.3
In the previous configuration the project could compile just fine. Are there any adjustments I need to still make for this new configuration ?
Can anyone please give a suggestion ?
In Springboot 2.4 they changed the name and type of the main classname property, see in Springboot 2.4 Release notes:
You need to update your build script as follows :
springBoot {
// before springboot 2.4
// mainClassName = "com.planes.planesserver.DemoApplication"
// since springboot 2.4 :
mainClass.set( "com.planes.planesserver.DemoApplication")
}

Error creating bean with name 'configurationPropertiesBeans' defined in class path resource /ConfigurationPropertiesRebinderAutoConfiguration.class]

I can build the application (Kotlin+Spring+Spring Cloud) but I can't start it.
Based on what I searched around it is related to incompability among Spring dependencies. I found someone facing similar issue as mine but after applying its solution I keep getting same issue other question
I tried also the the trick suggested with Spring Initializr but I got nothing when I type spring-cloud-starter
I guess the issue will fix when I set correct versions for:
id("org.springframework.boot") version "2.4.7"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
implementation("org.springframework.boot:spring-boot-dependencies:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter-web:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.cloud:spring-cloud-starter-openfeign:2.2.9.RELEASE")
implementation("io.github.openfeign:feign-okhttp:10.2.0")
Here is my gradle.build.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.jetbrains.kotlin.jvm") version "1.4.10"
id("org.jetbrains.kotlin.kapt") version "1.4.10"
kotlin("plugin.spring") version "1.5.20"
id("org.springframework.boot") version "2.4.7"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
}
val kotlinVersion: String by project
val springVersion: String by project
val projectGroupId: String by project
val projectVersion: String by project
val jacocoVersion: String by project
group = projectGroupId
version = projectVersion
repositories {
mavenLocal()
...
mavenCentral()
}
// add dependencies
dependencies {
kapt(kotlin("stdlib", kotlinVersion))
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect", kotlinVersion))
compile("br.com.mycomp:lib-log:3.2.0-74")
implementation("org.springframework.boot:spring-boot-dependencies:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter-web:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.cloud:spring-cloud-starter-openfeign:2.2.9.RELEASE")
implementation("io.github.openfeign:feign-okhttp:10.2.0")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.2")
}
java {
sourceCompatibility = JavaVersion.toVersion("11")
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "11"
javaParameters = true
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "11"
javaParameters = true
}
}
}
springBoot {
mainClass.set("com.examplo.demo.DemoApplication.kt")
}
sourceSets {
main {
java {
srcDirs("build/generated/source/avro/main/java")
}
}
}
apply {
tasks.test {
useJUnitPlatform()
}
configurations {
all {
exclude(group = "junit", module = "junit")
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "org.slf4j", module = "slf4j-log4j12")
}
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "11"
javaParameters = true
}
}
}
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
and its gradle.properties
springVersion=2.3.4.RELEASE
kotlinVersion=1.4.10
projectGroupId=com.mycomp
projectVersion=0.0.1
jacocoVersion=0.8.7
artifactoryContextUrl=xxx
The version you have declared for your spring cloud dependency (2.2.9.RELEASE) is not compatible with spring boot 2.4. You need at least 3.0.0 See the table on https://spring.io/projects/spring-cloud -- specifically follow the link to the spring cloud 2020 releases since that's what corresponds to spring boot 2.4. I think this is likely the cause of your issue, but I have a few other suggestions to help keep things in sync.
I see kotlin version 1.4.10 but spring.kotlin version 1.5.20 and they usually keep lock step with one another, so it's best to at least use the same major version of both (in this case, 1.5.x) i.e. in your gradle.properties update the kotlin version to 1.5.x, and then in the build.gradle use
plugins {
id("io.spring.dependency-management") version "1.0.11.RELEASE"
id("org.springframework.boot") version "2.5.4"
kotlin("jvm") version "1.5.30"
kotlin("kapt") version "1.5.30"
kotlin("plugin.spring") version "1.5.30"
}
Also note the updated spring dependency management version. Since you've declared the spring boot version, and you're using the dependency management plugin, you should let it pull in the versions that it wants -- you shouldn't declare the $springVersion on your dependencies, and you don't need the springVersion variable in your gradle.properties. The dependency management will pull in the proper version, which is already being done for your webflux dependency. This part is the second most likely cause of your problem.
If you're absolutely sure that the versions you have declared are compatible with one another, make sure an older version of cloud isn't being unexpectedly pulled in.
./gradlew dependencies --configuration compileClasspath
or
./gradlew -q dependencyInsight --dependency spring-cloud-starter-openfeign --configuration compileClasspath
If you're able to tease out the culprit that's bringing in the older version, you can prevent the older version from being pulled in transitively. In the snippet below, I'm omitting logger because I prefer log4j2, but you can substitute the old package, and if you're declaring the same package with a newer version, it'll be preferred
configurations {
all {
exclude(group = "org.springframework.boot", module = "spring-boot-starter-logging") // Prefer log4j2
}
}

How to merge additional-spring-configuration-metadata.json with kapt when using Kotlin and Gradle?

I have read these documents but I don’t know how to merge additional-spring-configuration-metadata.json with kapt when using Kotlin and Gradle.
https://docs.spring.io/spring-boot/docs/2.2.x/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-kotlin-configuration-properties
I managed to merge it this way, but I want to know the official way.
plugins {
id("org.springframework.boot") version "2.2.2.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
kotlin("jvm") version "1.3.61"
kotlin("plugin.spring") version "1.3.61"
kotlin("kapt") version "1.3.61"
}
dependencies {
kapt("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-reflect")
…
}
kapt {
arguments {
arg(
"org.springframework.boot.configurationprocessor.additionalMetadataLocations",
"$projectDir/src/main/resources"
)
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
Adding an answer here for better visibility. This is a known issue in the Kapt maven plugin that does not make sure the compilation classpath is available when the annotation processor runs. The implementation can't see the file and therefore won't merge it.
Your workaround works and there is an issue you can vote for in the JetBrains issue tracker.

Not able download dependencies in build.gradle.kts file using intellij

I am doing a Spring Boot Kotlin Project. Here is my build.gradle.kts file.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.1.8.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
kotlin("jvm") version "1.2.71"
kotlin("plugin.spring") version "1.2.71"
}
group = "com.axis"
version = "0.0.1-SNAPSHOT"
//java.sourceCompatibility = JavaVersion.VERSION_1_8
val compileKotlin: KotlinCompile by tasks
val compileTestKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.kafka:spring-kafka")
implementation("com.google.code.gson:gson")
implementation("com.itextpdf:itextpdf:5.0.6")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("de.flapdoodle.embed:de.flapdoodle.embed.mongo")
testImplementation("io.projectreactor:reactor-test")
testImplementation("io.mockk:mockk:1.9.3")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
I am adding the mockk dependency but it is not getting downloaded. Neither of the dependencies are getting downloaded. The build is successful but the dependency which I add don't get downloaded. I have disabled my proxy and tried.
I have checked the below link and tried out. But it doesn't work
Gradle build doesn't download dependencies
Any help will be appreciated.
I was able to resolve this. When i switched to a different network i was able to download. Not sure as to why i was being blocked to download dependencies in some network.

Kotlin: Cannot inline bytecode built with JVM target 1.8 into byte code with JVM target 1.6

I have an issue when trying to add a few dependencies to my kotlin spring project. I used the spring boot initializer to get a basic project running.
My problem: If I uncomment jackson or either Koin dependency then my build fails with the mentioned in the titile
Here is the build.gradle.kts file :
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("plugin.jpa") version "1.3.31"
id("org.springframework.boot") version "2.2.0.M4"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
kotlin("jvm") version "1.3.31"
kotlin("plugin.spring") version "1.3.31"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
maven { url = uri("https://repo.spring.io/snapshot") }
maven { url = uri("https://repo.spring.io/milestone") }
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "junit", module = "junit")
}
// jackson
//implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+")
// Koin
//implementation("org.koin:koin-core:2.0.1")
// Koin Test
//implementation("org.koin:koin-test:2.0.1")
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
I Have already tried all the solutions from this previous question someone else asked: Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6
I already had the KotlinCompile jvmTarget option in there and it has been set in my build settings as well. Any help is appreciated thanks!
A lot of the answers to this are android specific - using kotlin to write a spring boot app isn't that well documented or supported but still awesome. This setting in intelij cleared this error for me - hope it helps someone, my kotlin compiler had a target set to 1.6:

Resources