The reason is that aliyun YunXiao does not have the option to select JDK 21, the latest version only supports up to JDK 18. So this article was created.
- Step 1
First, add the JDK directory that needs to be installed to the pipeline cache.
Here we are using java-21-amazon-corretto, after installation, the /usr/lib/jvm/java-21-amazon-corretto directory is used as the default installation directory, so the YunXiao cache adds the /usr/lib/jvm/java-21-amazon-corretto directory.
- Step 2
Use the latest version of Java for building as much as possible.
- Step 3
Write the build script
#install jdk21
if [ ! -d "/usr/lib/jvm/java-21-amazon-corretto/bin" ]; then
apt-get update
apt-get install software-properties-common ca-certificates curl gnupg apt-utils -y
wget -O- https://apt.corretto.aws/corretto.key | apt-key add -
add-apt-repository 'deb https://apt.corretto.aws stable main'
apt-get update
apt-get install -y java-21-amazon-corretto-jdk
fi
export JAVA_HOME=/usr/lib/jvm/java-21-amazon-corretto
# maven build your module here
mvn -B clean package -pl admin -am -DskipTests -T 2C
Here we checked if the cached JDK directory exists. If it does not exist, we install JDK 21. After installation, we switch the default JDK to 21. Maven packaging will automatically detect the JAVA_HOME environment variable and build our admin module. The project is a normal Spring Boot multi-module project. After packaging is complete, the JDK directory will be cached in the pipeline for 30 days, greatly reducing build time for subsequent builds within this period.
You can see that the build without JDK directory cache took over two minutes. Most of the time was spent on installing JDK dependencies. When the shared build cluster of Alibaba Cloud YunXiao pipeline is heavily loaded, the build time can even reach 4-5 minutes. After optimization, the build stabilized at around one minute, which is a significant improvement.