diff --git a/src/java/devcontainer-feature.json b/src/java/devcontainer-feature.json index 51e214a25..7a9caa924 100644 --- a/src/java/devcontainer-feature.json +++ b/src/java/devcontainer-feature.json @@ -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.", diff --git a/src/java/install.sh b/src/java/install.sh index 277681fec..47f0e8559 100644 --- a/src/java/install.sh +++ b/src/java/install.sh @@ -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 @@ -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 @@ -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 @@ -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")" } @@ -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 @@ -301,8 +305,7 @@ 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 @@ -310,8 +313,7 @@ if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then 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}" diff --git a/test/java/install_latest_version.sh b/test/java/install_latest_version.sh new file mode 100644 index 000000000..a45d5e0b0 --- /dev/null +++ b/test/java/install_latest_version.sh @@ -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 diff --git a/test/java/scenarios.json b/test/java/scenarios.json index 538d77972..f28fceb41 100644 --- a/test/java/scenarios.json +++ b/test/java/scenarios.json @@ -8,6 +8,14 @@ } } }, + "install_latest_version": { + "image": "ubuntu:focal", + "features": { + "java": { + "version": "latest" + } + } + }, "install_lts_version": { "image": "ubuntu:focal", "features": {