Skip to content

Commit

Permalink
[Java] Add JDK 22 support (#1102)
Browse files Browse the repository at this point in the history
* Add JDK 22 support

* bump patch version

* removed exit added for debugging

* added one condition before changing the JDK_DISTRO

* version set to 21 and jdk_distro open fixed

* failed test cases fixed

* fixing install_type issue if not java

* yum update with epel-release install

* explicit test case writen to validate jdk 22 works fine

* moved check_packages to the start

* INSTALL_CMD moved to start

* variable changed
  • Loading branch information
prathameshzarkar9 authored Sep 25, 2024
1 parent 0be7941 commit 5077a2c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/java/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "java",
"version": "1.6.0",
"version": "1.6.1",
"name": "Java (via SDKMAN!)",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/java",
"description": "Installs Java, SDKMAN! (if not installed), and needed dependencies.",
Expand Down
166 changes: 84 additions & 82 deletions src/java/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ else
exit 1
fi

if [ "${ADJUSTED_ID}" = "rhel" ] && [ "${VERSION_CODENAME-}" = "centos7" ]; then
# As of 1 July 2024, mirrorlist.centos.org no longer exists.
# Update the repo files to reference vault.centos.org.
sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo
sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo
sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo
fi

# Setup INSTALL_CMD & PKG_MGR_CMD
if type apt-get > /dev/null 2>&1; then
PKG_MGR_CMD=apt-get
Expand All @@ -79,6 +71,67 @@ else
exit 1
fi

pkg_manager_update() {
case $ADJUSTED_ID in
debian)
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
${PKG_MGR_CMD} update -y
fi
;;
rhel)
if [ ${PKG_MGR_CMD} = "microdnf" ]; then
if [ "$(ls /var/cache/yum/* 2>/dev/null | wc -l)" = 0 ]; then
echo "Running ${PKG_MGR_CMD} makecache ..."
${PKG_MGR_CMD} makecache
fi
else
if [ "$(ls /var/cache/${PKG_MGR_CMD}/* 2>/dev/null | wc -l)" = 0 ]; then
echo "Running ${PKG_MGR_CMD} check-update ..."
set +e
stderr_messages=$(${PKG_MGR_CMD} -q check-update 2>&1)
rc=$?
# centos 7 sometimes returns a status of 100 when it apears to work.
if [ $rc != 0 ] && [ $rc != 100 ]; then
echo "(Error) ${PKG_MGR_CMD} check-update produced the following error message(s):"
echo "${stderr_messages}"
exit 1
fi
set -e
fi
fi
;;
esac
}

# Checks if packages are installed and installs them if not
check_packages() {
case ${ADJUSTED_ID} in
debian)
if ! dpkg -s "$@" > /dev/null 2>&1; then
pkg_manager_update
${INSTALL_CMD} "$@"
fi
;;
rhel)
if ! rpm -q "$@" > /dev/null 2>&1; then
pkg_manager_update
${INSTALL_CMD} "$@"
fi
;;
esac
}

if [ "${ADJUSTED_ID}" = "rhel" ] && [ "${VERSION_CODENAME-}" = "centos7" ]; then
# As of 1 July 2024, mirrorlist.centos.org no longer exists.
# Update the repo files to reference vault.centos.org.
sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo
sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo
sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo
yum update -y
check_packages epel-release
fi

# Clean up
clean_up() {
local pkg
Expand All @@ -95,7 +148,6 @@ clean_up() {
;;
esac
}
clean_up

# Ensure that login shells get the correct path if the user updated the PATH using ENV.
rm -f /etc/profile.d/00-restore-env.sh
Expand Down Expand Up @@ -143,80 +195,33 @@ updaterc() {
fi
}


pkg_manager_update() {
case $ADJUSTED_ID in
debian)
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
${PKG_MGR_CMD} update -y
fi
;;
rhel)
if [ ${PKG_MGR_CMD} = "microdnf" ]; then
if [ "$(ls /var/cache/yum/* 2>/dev/null | wc -l)" = 0 ]; then
echo "Running ${PKG_MGR_CMD} makecache ..."
${PKG_MGR_CMD} makecache
fi
else
if [ "$(ls /var/cache/${PKG_MGR_CMD}/* 2>/dev/null | wc -l)" = 0 ]; then
echo "Running ${PKG_MGR_CMD} check-update ..."
set +e
stderr_messages=$(${PKG_MGR_CMD} -q check-update 2>&1)
rc=$?
# centos 7 sometimes returns a status of 100 when it apears to work.
if [ $rc != 0 ] && [ $rc != 100 ]; then
echo "(Error) ${PKG_MGR_CMD} check-update produced the following error message(s):"
echo "${stderr_messages}"
exit 1
fi
set -e
fi
fi
;;
esac
}

# Checks if packages are installed and installs them if not
check_packages() {
case ${ADJUSTED_ID} in
debian)
if ! dpkg -s "$@" > /dev/null 2>&1; then
pkg_manager_update
${INSTALL_CMD} "$@"
fi
;;
rhel)
if ! rpm -q "$@" > /dev/null 2>&1; then
pkg_manager_update
${INSTALL_CMD} "$@"
fi
;;
esac
}

# Use Microsoft JDK for everything but JDK 8 and 18 (unless specified differently with jdkDistro option)
get_jdk_distro() {
VERSION="$1"
if [ "${JDK_DISTRO}" = "ms" ]; then
if echo "${VERSION}" | grep -E '^8([\s\.]|$)' > /dev/null 2>&1 || echo "${VERSION}" | grep -E '^18([\s\.]|$)' > /dev/null 2>&1; then
JDK_DISTRO="tem"
fi
fi
}

find_version_list() {
prefix="$1"
suffix="$2"
install_type=$3
ifLts="$4"
version_list=$5
java_ver=$6

check_packages jq
all_versions=$(curl -s https://api.adoptium.net/v3/info/available_releases)
if [ "${ifLts}" = "true" ]; then
all_lts_versions=$(curl -s https://api.adoptium.net/v3/info/available_releases)
major_version=$(echo "$all_lts_versions" | jq -r '.most_recent_lts')
regex="${prefix}\\K${major_version}\\.?[0-9]*\\.?[0-9]*${suffix}"
else
major_version=$(echo "$all_versions" | jq -r '.most_recent_lts')
elif [ "${java_ver}" = "latest" ]; then
major_version=$(echo "$all_versions" | jq -r '.most_recent_feature_release')
else
major_version=$(echo "$java_ver" | cut -d '.' -f 1)
fi

if [ "${JDK_DISTRO}" = "ms" ]; then
if [ "${major_version}" = "8" ] || [ "${major_version}" = "18" ] || [ "${major_version}" = "22" ]; then
JDK_DISTRO="tem"
fi
fi
if [ "${install_type}" != "java" ]; then
regex="${prefix}\\K[0-9]+\\.?[0-9]*\\.?[0-9]*${suffix}"
else
regex="${prefix}\\K${major_version}\\.?[0-9]*\\.?[0-9]*${suffix}${JDK_DISTRO}\\s*"
fi
declare -g ${version_list}="$(su ${USERNAME} -c ". \${SDKMAN_DIR}/bin/sdkman-init.sh && sdk list ${install_type} 2>&1 | grep -oP \"${regex}\" | tr -d ' ' | sort -rV")"
}
Expand All @@ -237,13 +242,12 @@ sdk_install() {
elif [[ "${pkg_vals}" =~ "${install_type}" ]] && [ "${requested_version}" = "latest" ]; then
requested_version=""
elif [ "${requested_version}" = "lts" ]; then
check_packages jq
find_version_list "$prefix" "$suffix" "$install_type" "true" version_list
find_version_list "$prefix" "$suffix" "$install_type" "true" version_list "${requested_version}"
requested_version="$(echo "${version_list}" | head -n 1)"
elif echo "${requested_version}" | grep -oE "${full_version_check}" > /dev/null 2>&1; then
echo "${requested_version}"
else
find_version_list "$prefix" "$suffix" "$install_type" "false" version_list
find_version_list "$prefix" "$suffix" "$install_type" "false" version_list "${requested_version}"
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ]; then
requested_version="$(echo "${version_list}" | head -n 1)"
else
Expand Down Expand Up @@ -301,17 +305,15 @@ if [ ! -d "${SDKMAN_DIR}" ]; then
updaterc "export SDKMAN_DIR=${SDKMAN_DIR}\n. \${SDKMAN_DIR}/bin/sdkman-init.sh"
fi

get_jdk_distro ${JAVA_VERSION}
sdk_install java ${JAVA_VERSION} "\\s*" "(\\.[a-z0-9]+)*-${JDK_DISTRO}\\s*" ".*-[a-z]+$" "true"
sdk_install java ${JAVA_VERSION} "\\s*" "(\\.[a-z0-9]+)*-" ".*-[a-z]+$" "true"

# Additional java versions to be installed but not be set as default.
if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
OLDIFS=$IFS
IFS=","
read -a additional_versions <<< "$ADDITIONAL_VERSIONS"
for version in "${additional_versions[@]}"; do
get_jdk_distro ${version}
sdk_install java ${version} "\\s*" "(\\.[a-z0-9]+)*-${JDK_DISTRO}\\s*" ".*-[a-z]+$" "false"
sdk_install java ${version} "\\s*" "(\\.[a-z0-9]+)*-" ".*-[a-z]+$" "false"
done
IFS=$OLDIFS
su ${USERNAME} -c ". ${SDKMAN_DIR}/bin/sdkman-init.sh && sdk default java ${JAVA_VERSION}"
Expand Down
15 changes: 15 additions & 0 deletions test/java/install_latest_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

echo 'public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }' > HelloWorld.java
javac HelloWorld.java

check "hello world" /bin/bash -c "java HelloWorld | grep "Hello, World!""
check "java version latest installed" grep "22.0.2" <(java --version)

# Report result
reportResults
8 changes: 8 additions & 0 deletions test/java/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
}
}
},
"install_latest_version": {
"image": "ubuntu:focal",
"features": {
"java": {
"version": "latest"
}
}
},
"install_lts_version": {
"image": "ubuntu:focal",
"features": {
Expand Down

1 comment on commit 5077a2c

@partridgeworks
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prathameshzarkar9 This commit breaks .devcontainer build on certain machines, see the reported issue and log in #1134 and also I have repro'd on my machine.

Please sign in to comment.