--- /dev/null
+---
+prelude: >
+ OpenDaylight jenkins maven jobs with jdk17 and CentOS7 currently fails
+ with a confusing message stating that the JAVA_HOME variable is not
+ correctly set.
+ This can happen in various cases, usually when there is a mismatch
+ between the jdk used by maven and the folder pointed by JAVA_HOME.
+ It appears that openjdk17 is not available with CentOS7 and that
+ the folder indeed does not exist
+issues:
+ - |
+ Current message (JAVA_HOME variable is not set) is confusing and can lead
+ to erroneous interpretations.
+fixes:
+ - |
+ Add a folder existence check in related script before propagating
+ JAVA_HOME variable to other scripts.
+ If no folder was found, try to find an approaching solution and exit in
+ case of failure with a more relevant error message.
+other:
+ - |
+ Adapt and refactor code consequently to be more agnostic to distribution
+ and jdk installation specificities
+
-#!/bin/bash
+#!/bin/sh
# SPDX-License-Identifier: EPL-1.0
##############################################################################
# Copyright (c) 2018 The Linux Foundation and others.
JAVA_ENV_FILE="/tmp/java.env"
-update-java-redhat() {
- JAVA_RELEASE=${SET_JDK_VERSION//[a-zA-Z]/}
- if [[ ${JAVA_RELEASE} -ge 9 ]]; then
+JAVA_RELEASE=$(echo $SET_JDK_VERSION | sed 's/[a-zA-Z]//g')
+JAVA_RELEASE_NBR=$(echo $SET_JDK_VERSION | sed 's/[a-zA-Z:-]//g')
+#TODO check whether is it worth keeping there 2 distinct variables
+update_java_redhat() {
+ if [ ${JAVA_RELEASE} -ge 9 ]; then
# Java 9 or newer: new version format
export JAVA_HOME="/usr/lib/jvm/java-${JAVA_RELEASE}-openjdk"
else
# Java 8 or older: old version format
- export JAVA_HOME="/usr/lib/jvm/java-1.${SET_JDK_VERSION//[a-zA-Z:-]/}.0-openjdk"
+ export JAVA_HOME="/usr/lib/jvm/java-1.${JAVA_RELEASE_NBR}.0-openjdk"
fi
- sudo /usr/sbin/alternatives --install /usr/bin/java java "${JAVA_HOME}/bin/java" 1
- sudo /usr/sbin/alternatives --install /usr/bin/javac javac "${JAVA_HOME}/bin/javac" 1
- sudo /usr/sbin/alternatives --install /usr/lib/jvm/java-openjdk java_sdk_openjdk "${JAVA_HOME}" 1
- sudo /usr/sbin/alternatives --set java "${JAVA_HOME}/bin/java"
- sudo /usr/sbin/alternatives --set javac "${JAVA_HOME}/bin/javac"
- sudo /usr/sbin/alternatives --set java_sdk_openjdk "${JAVA_HOME}"
- echo JAVA_HOME="$JAVA_HOME" > "$JAVA_ENV_FILE"
}
-update-java-ubuntu() {
+update_java_ubuntu() {
HOST_ARCH=$(dpkg --print-architecture)
- export JAVA_HOME="/usr/lib/jvm/java-${SET_JDK_VERSION//[a-zA-Z:-]/}-openjdk-${HOST_ARCH}"
- sudo /usr/bin/update-alternatives --install /usr/bin/java java "${JAVA_HOME}/bin/java" 1
- sudo /usr/bin/update-alternatives --install /usr/bin/javac javac "${JAVA_HOME}/bin/javac" 1
- sudo /usr/bin/update-alternatives --install /usr/lib/jvm/java-openjdk java_sdk_openjdk "${JAVA_HOME}" 1
- sudo /usr/bin/update-alternatives --set java "${JAVA_HOME}/bin/java"
- sudo /usr/bin/update-alternatives --set javac "${JAVA_HOME}/bin/javac"
- sudo /usr/bin/update-alternatives --set java_sdk_openjdk "${JAVA_HOME}"
- echo JAVA_HOME="$JAVA_HOME" > "$JAVA_ENV_FILE"
+ export JAVA_HOME="/usr/lib/jvm/java-${JAVA_RELEASE_NBR}-openjdk-${HOST_ARCH}"
}
echo "---> Updating Java version"
case "${OS}" in
fedora|centos|redhat)
echo "---> RedHat type system detected"
- update-java-redhat
+ update_java_redhat
+ alternatives="/usr/sbin/alternatives"
;;
ubuntu|debian)
echo "---> Ubuntu/Debian system detected"
- update-java-ubuntu
+ update_java_ubuntu
+ alternatives="/usr/sbin/update-alternatives"
;;
esac
+
+if ! [ -d "$JAVA_HOME" ]; then
+ echo "$JAVA_HOME directory not found - trying to find an approaching one"
+ if ls -d "$JAVA_HOME"*; then
+ export JAVA_HOME=$(ls -d "$JAVA_HOME"* | head -1)
+ else
+ echo "no $JAVA_HOME directory nor candidate found -exiting " >&2
+ exit 17
+ fi
+fi
+
+sudo $alternatives --install /usr/bin/java java "${JAVA_HOME}/bin/java" 1
+sudo $alternatives --install /usr/bin/javac javac "${JAVA_HOME}/bin/javac" 1
+sudo $alternatives --install /usr/lib/jvm/java-openjdk java_sdk_openjdk "${JAVA_HOME}" 1
+sudo $alternatives --set java "${JAVA_HOME}/bin/java"
+sudo $alternatives --set javac "${JAVA_HOME}/bin/javac"
+sudo $alternatives --set java_sdk_openjdk "${JAVA_HOME}"
+echo JAVA_HOME="$JAVA_HOME" > "$JAVA_ENV_FILE"
+
java -version
echo JAVA_HOME="${JAVA_HOME}"