AWS CodeBuild是否支持OpenJDK14?求配置实现支持的方法
Hey there! Great question—AWS CodeBuild doesn’t come with OpenJDK 14 pre-installed in its official standard images, but you absolutely can add support for it using one of two straightforward approaches:
方法一:使用自定义 Docker 镜像
If you want a reusable environment with OpenJDK 14 pre-configured, building a custom Docker image is the way to go. Here’s how:
- Create a
Dockerfilethat installs OpenJDK 14 (we’ll use Amazon Corretto 14, AWS’s distribution of OpenJDK, for reliability):
# 基于 Amazon Linux 2 基础镜像 FROM amazonlinux:2 # 安装 Amazon Corretto 14 RUN yum install -y https://corretto.aws/downloads/latest/amazon-corretto-14-x64-linux-jdk.rpm # 设置默认 Java 和 Javac 版本为 14 RUN alternatives --set java /usr/lib/jvm/java-14-amazon-corretto.x86_64/bin/java RUN alternatives --set javac /usr/lib/jvm/java-14-amazon-corretto.x86_64/bin/javac
- Build this image and push it to Amazon ECR (or another container registry of your choice).
- In your AWS CodeBuild project configuration, select "Custom image" as the environment and point it to your ECR image URI.
方法二:在 Buildspec.yml 中动态安装 OpenJDK 14
If you prefer not to manage a custom image, you can install OpenJDK 14 directly during the build’s install phase. The commands vary slightly based on your base image:
对于 Amazon Linux 2 环境
Update your buildspec.yml like this:
version: 0.2 phases: install: commands: # 安装 Amazon Corretto 14 - yum install -y https://corretto.aws/downloads/latest/amazon-corretto-14-x64-linux-jdk.rpm # 设置默认 Java 版本 - alternatives --set java /usr/lib/jvm/java-14-amazon-corretto.x86_64/bin/java - alternatives --set javac /usr/lib/jvm/java-14-amazon-corretto.x86_64/bin/javac # 验证安装成功 - java -version build: commands: # 在这里添加你的构建命令,比如 mvn clean package
对于 Ubuntu 环境
Use these commands instead:
version: 0.2 phases: install: commands: - apt-get update - apt-get install -y openjdk-14-jdk - update-alternatives --set java /usr/lib/jvm/java-14-openjdk-amd64/bin/java - update-alternatives --set javac /usr/lib/jvm/java-14-openjdk-amd64/bin/javac - java -version build: commands: # 你的构建命令
小提醒
Just a heads-up: OpenJDK 14 reached end-of-life in September 2021, so it no longer receives security updates. If your project allows, consider upgrading to a long-term support (LTS) version like OpenJDK 17 for better security and stability.
内容的提问来源于stack exchange,提问作者Eric Lou

