Make SpringBoot1.5 + Gradle4.4 + Java8 + Docker environment compatible with Java11

This article is the 15th day article of Java Advent Calendar 2019.

Introduction

I left it for about a year after the free support for Java 8 expired in January 2019, Since I have time this time, I updated to Java 11 which is the first LTS since 8 It is a memo at that time

I tried to start writing, but it seems that OpenJDK 8 will be supported until June 2023, so Maybe I should have just changed from Oracle JDK to OpenJDK ... But I've done it so I'll record it

JDK update

Install AdoptOpenJDK 11

Enter the following command to install the latest JDK

brew cask install adoptopenjdk

However, at present, JDK13 is included, so it is necessary to specify the version to include JDK11 which is the LTS version. Search for the published version

$ brew search adoptopenjdk
==> Casks
adoptopenjdk                       adoptopenjdk11-openj9-jre          adoptopenjdk12-openj9              adoptopenjdk13-jre                 adoptopenjdk8                      adoptopenjdk8-openj9-large
adoptopenjdk10                     adoptopenjdk11-openj9-jre-large    adoptopenjdk12-openj9-jre          adoptopenjdk13-openj9              adoptopenjdk8-jre                  adoptopenjdk9
adoptopenjdk11                     adoptopenjdk11-openj9-large        adoptopenjdk12-openj9-jre-large    adoptopenjdk13-openj9-jre          adoptopenjdk8-openj9
adoptopenjdk11-jre                 adoptopenjdk12                     adoptopenjdk12-openj9-large        adoptopenjdk13-openj9-jre-large    adoptopenjdk8-openj9-jre
adoptopenjdk11-openj9              adoptopenjdk12-jre                 adoptopenjdk13                     adoptopenjdk13-openj9-large        adoptopenjdk8-openj9-jre-large

Since I want to install JDK11 this time, specify the version with the following command and install it

brew cask install adoptopenjdk11

Change the existing PATH to JDK11

export JAVA_HOME=`/usr/libexec/java_home -v 11`

Check version

$ java -version
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.5+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.5+10, mixed mode)

Specify JDK11 as Project SDK of IntelliJ

Open Project Structure with Command +; and specify JDK 11 for Project SDK and Project Language Lebel.

Update Gradle

Gradle 4.4 does not support JDK 11, so it needs to be updated It seems that 5.0 and later versions are supported, but since it is a good idea, I will try to make it the current latest 6.0.1

Update gradle-wrapper.properties

If you update the version described in gradle / wrapper / gradle-wrapper.properties, the version of Gradle applied to the project will be changed. This time, just change the following 4.4 part to 6.0.1.

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip

Update gradlew

That alone will not propagate the changes to gradlew, so regenerate gradlew

./gradlew wrapper

Updated gradle.build

Change various framework libraries to those compatible with JDK11 Basically everything is described in gradle.build, so you can modify it

Change Java version specification

The meaning of each specification is described in Official, so specify what you need.

sourceCompatibility = 11
targetCompatibility = 11

Changed to the description method of Gradle 6 series

Updated because the jar specification method has changed See Official for details.

jar {
    baseName = 'example'
    version = '1.0.0-SNAPSHOT'
}

Changed what was above to below

bootJar {
    archiveBaseName = "example"
    archiveVersion = "1.0.0-SNAPSHOT"
}

Change the version of Spiring Boot

Spring Boot 2.1.x or higher is required to use Java 11 Since it is a good idea, I will try to make it the latest 2.2.2 Basically, you can follow the Official Migration Guide.

buildscript {
    ext {
        springBootVersion = '1.5.13.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

SpringBoot no longer automatically applies the dependency management plugin, so add the plugin as well

apply plugin: 'io.spring.dependency-management'

Since the method of specifying the main class has changed, that has also changed

springBoot {
    mainClassName = "jp.co.example.Application"
}

Update library version

Update the library version while looking at MavenRepository

Library around Spring Boot

It is easier to specify the version of the library around Spring Boot with a variable However, please note that some versions may differ from the Spring Boot version.

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")

    testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
    testCompile("org.springframework.security:spring-security-test:5.2.1.RELEASE")
}

lombok

Since the method of specifying the dependency has changed, if you normally specify only the version and update it, an error will occur if the method automatically generated by lombok at build time is not found Refer to Official and add the following specifications

compileOnly('org.projectlombok:lombok:1.18.10')
annotationProcessor('org.projectlombok:lombok:1.18.10')

Also, when using @Value, I get an error that deserialization in Jackson doesn't work. It can be avoided by creating a file called lombok.config directly under the project and describing the following. Other settings can be found in the Official Migration Guide (https://projectlombok.org/changelog).

lombok.addJavaxGeneratedAnnotation = false
lombok.addLombokGeneratedAnnotation = true
lombok.noArgsConstructor.extraPrivate = false
lombok.anyConstructor.addConstructorProperties = true

config.stopBubbling = true

jjwt

Because it relies on APIs that are obsolete in Java 11 when using some algorithms If you are using Java 8 and have migrated to Java 11, you need to add a dependency Add the following referring to Official

compile('javax.xml.bind:jaxb-api:2.3.0')
compile('com.sun.xml.bind:jaxb-core:2.3.0')
compile('com.sun.xml.bind:jaxb-impl:2.3.0')

Other

Update to good Don't forget to update the libraries specified in other than dependencies. I forgot to update jacoco and wasted my time

Supports APIs that are obsolete in Java 11

This article will be used as a reference to fix the obsolete API.

Fix classes obsolete in SpringBoot 2.0

When I try to build it, there are multiple obsolete classes around SpringBoot, so I will fix it Refer to Official Migration Guide

This time, only the classes around org.springframework.boot.autoconfigure.web that controlled the error message were targeted. The following method

@RequestMapping(value = "/error")
public ErrorResponse error(HttpServletRequest request, HttpServletResponse response) {
  ServletRequestAttributes attributes = new ServletRequestAttributes(request);
  Throwable error = errorAttributes.getError(attributes);
  return convertException(error);
}

do this

@RequestMapping(value = "/error")
public ErrorResponse error(WebRequest request) {
  Throwable error = errorAttributes.getError(request);
  return convertException(error);
}

Try to test

If you do so far, the build will pass for the time being, so run the test and check if degreasing has occurred There seems to be no problem this time We will postpone the resolution of Warn and aim to deploy the JDK11 compatible application to Docker for the time being.

Modify the Dockerfile

Dockerfile only needs to change the base image This article will help you decide which image to choose. As recommended this time, select adoptopenjdk / openjdk11: alpine-slim

FROM adoptopenjdk/openjdk11:alpine-slim

Other

Change CircleCI settings

If you are using a CI / CD tool, don't forget to change that setting as well. It is easy to forget to specify the base image when building the test environment.

SpringBootActuator

Note that the / actuator prefix has been added to all endpoints automatically generated by SpringBootActuator. Deploying doesn't work, especially when using / health For other changes, refer to this article.

at the end

With the above modifications, you should be ready to deploy for the time being. If any problem occurs, I will add it as appropriate Please let me know if there is something like "this way of writing is correct" or "this may cause a bug".

reference

Install AdoptOpenJDK11 with homebrew on mac

Note on updating the version of Gradle wrapper

Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0

Spring Boot 2.0 Migration Guide

Lombok Project

Think about recommended Docker images after Java 11 release

Spring Boot 2.0 Actuator, 3 changes you need to know to get it working

What Java engineers should prepare for the Java 11 release

ObjectMapper can't deserialize without default constructor after upgrade to Spring Boot 2

Lombok Changelog

Recommended Posts

Make SpringBoot1.5 + Gradle4.4 + Java8 + Docker environment compatible with Java11
Prepare a scraping environment with Docker and Java
How to build docker environment with Gradle for intelliJ
Make Calendar gadgets made with JavaFX compatible with Java SE 9
[Note] Create a java environment from scratch with docker
Build Java development environment with WSL2 Docker VS Code
Pytorch execution environment with Docker
Java multi-project creation with Gradle
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
Using Docker from Java Gradle
React environment construction with Docker
Hello World with SpringBoot / Gradle
Let's write how to make API with SpringBoot + Docker from 0
Create Rails5 and postgresql environment with Docker and make pgadmin available
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Rails + MySQL environment construction with Docker
Create a Vue3 environment with Docker!
Build Couchbase local environment with Docker
Build a Java project with Gradle
Install java with Ubuntu 16.04 based Docker
Build a Node.js environment with Docker
Environment construction with Docker for beginners
Prepare Java development environment with Atom
Easily Docker Java applications with Jib
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Create SolrCloud verification environment with Docker
Create Laravel environment with Docker (docker-compose)
[Environment construction with Docker] Rails 6 & MySQL 8
Make JupyterLab run anywhere with docker
[Java & SpringBoot] Environment Construction for Mac
Build docker + laravel environment with laradock
Spring Boot gradle build with Docker
Switch environment by boot argument with SpringBoot
Alert slack with alert manager in Docker environment
Prepare Java development environment with VS Code
[Java] Create an executable module with Gradle
GPU environment construction with Docker [October 2020 version]
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Laravel development environment construction with Docker (Mac)
Build a PureScript development environment with Docker
Create Rails 6 + MySQL environment with Docker compose
Make something like Java Enum with Typescript
Achieve OpenSSL compatible encryption with Java / PHP
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Edit Mysql with commands in Docker environment
CICS-Run Java applications-(3) Build management with Gradle
Create a MySQL environment with Docker from 0-> 1
Creating a java web application development environment with docker for mac part1
Create Spring Boot-gradle-mysql development environment with Docker
[Java] Connection with local DB (IntelliJ + SpringBoot)
[Docker] Create Node.js + express + webpack environment with Docker
Spring Boot + Docker Java development environment construction
[docker] [nginx] Make a simple ALB with nginx
Laravel + MySQL + phpMyadmin environment construction with Docker
Build a Wordpress development environment with Docker
[Docker] Build Jupyter Lab execution environment with Docker
Java automated test implementation with JUnit 5 + Gradle