一、升级项目
1.1 POM文件
最近很多小伙伴私信我要我出一个springboot搭配jdk21的教程,应粉丝要求,我去spring官网找到了springboot和jdk版本的是配图,供大家参考!之前博主写的springboot 3.0.0 搭配jdk20不知道各位小伙伴们有没有看过呢?现在jdk21出来了,springboot 3.0.0的版本已经不够用了,需要升级到springboot 3.1.x系列的版本才可以与之匹配。
modelVersion>4.0.0modelVersion>
parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>3.1.0version>
relativePath/>
parent>
properties>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
maven.compiler.encoding>UTF-8maven.compiler.encoding>
maven.compiler.source>21maven.compiler.source>
maven.compiler.target>21maven.compiler.target>
maven.compiler.compilerVersion>21maven.compiler.compilerVersion>
java.version>21java.version>
skipTests>trueskipTests>
properties>
1.2 Lombok问题
如果你的项目中有使用Lombok,并且版本呢是1.18.28,可能会出现这个问题,这是因为此版本并不支持jdk21,需要升级到1.18.30。
java: java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field 'com.sun.tools.javac.tree.JCTree qualid'
dependency>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
version>1.18.30version>
scope>providedscope>
dependency>
二、制作docker镜像
2.1 dockerfile文件
FROM alpine:3.15
ARG version=21.0.0.34.1
# Please note that the THIRD-PARTY-LICENSE could be out of date if the base image has been updated recently.
# The Corretto team will update this file but you may see a few days' delay.
RUN wget -O /THIRD-PARTY-LICENSES-20200824.tar.gz https://corretto.aws/downloads/resources/licenses/alpine/THIRD-PARTY-LICENSES-20200824.tar.gz &&
echo "82f3e50e71b2aee21321b2b33de372feed5befad6ef2196ddec92311bc09becb /THIRD-PARTY-LICENSES-20200824.tar.gz" | sha256sum -c - &&
tar x -ovzf THIRD-PARTY-LICENSES-20200824.tar.gz &&
rm -rf THIRD-PARTY-LICENSES-20200824.tar.gz &&
wget -O /etc/apk/keys/amazoncorretto.rsa.pub https://apk.corretto.aws/amazoncorretto.rsa.pub &&
SHA_SUM="6cfdf08be09f32ca298e2d5bd4a359ee2b275765c09b56d514624bf831eafb91" &&
echo "${SHA_SUM} /etc/apk/keys/amazoncorretto.rsa.pub" | sha256sum -c - &&
echo "https://apk.corretto.aws" >> /etc/apk/repositories &&
apk add --no-cache amazon-corretto-21=$version-r0 &&
rm -rf /usr/lib/jvm/java-21-amazon-corretto/lib/src.zip
ENV LANG C.UTF-8
ENV JAVA_HOME=/usr/lib/jvm/default-jvm
ENV PATH=$PATH:/usr/lib/jvm/default-jvm/bin
2.2 构建镜像命令
docker build -t openjdk:21-jdk-alpine .