1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#!/usr/bin/env bash
# Installs JDK into environment
# Can only install OpenJDK versions
# --expire flag will skip installation if same JDK was installed less than 24 hours ago
set -uexo pipefail
script_home="$(cd "$(dirname "$0")" && pwd)"
cmdname="$(basename "$0")"
default=openjdk11
usage() {
echo "Usage: $cmdname [--(no-)expire] <openjdk11|default|...> INSTALLDIR_IF_NEEDED"
}
misuse() { usage 1>&2; exit 2; }
jdk-info () { "$script_home"/jdk-info "$@"; }
epochsecs() { printf '%(%s)T\n' -1; }
expire=''
# Validate command arguments
while true; do
case "$1" in
--expire) expire='true'; shift ;;
--no-expire) expire=''; shift ;;
--) shift; break ;;
*) break ;;
esac
done
test $# -eq 2 || misuse
ver="$1"
install="$2"
# Path to the file this script uses to cache install time
install_time="$install/jdk/jdk-install-time"
# Version must be an OpenJDK version
case "$ver" in
openjdk*) ;;
default) ver="$default" ;;
*)
echo "$cmdname does not know how to install $ver" 2>&1
exit 2
;;
esac
# If --expire flag is present, last JDK installation was within 24hrs, and the
# current JDK is the same as the one being requested, skip installation
if test "$expire"; then
if test -e "$install_time"; then
if test $(($(epochsecs) - $(< "$install_time"))) -lt 86400; then
jdk_spec=$(JDK_INFO_JAVA="$install/jdk/bin/java" jdk-info --print spec)
if test "$jdk_spec" = "$ver"; then
# Don't update more than once per day
exit 0
fi
fi
fi
else # not expiring
if test openjdk"$(jdk-info --print spec)" = "$ver"; then
exit 0
fi
fi
# Create temporary directory
tmpdir="$(mktemp -d "$cmdname-XXXXXX")"
tmpdir="$(cd "$tmpdir" && pwd)"
trap "$(printf 'rm -rf %q' "$tmpdir")" EXIT
# Unsure OS is either Linux of MacOS
case "$OSTYPE" in
linux*)
adoptos=linux
;;
darwin*)
adoptos=mac
;;
*)
echo "Unexpected platform: $OSTYPE" 1>&2
exit 2
;;
esac
# Get OpenJDK tarball from official source
curl -sSLo "$tmpdir/jdk.tar.gz" \
-f --retry 3 \
"https://api.adoptium.net/v3/binary/latest/${ver#openjdk}/ga/$adoptos/x64/jdk/hotspot/normal/eclipse?project=jdk"
# Extract OpenJDK from tarball
(cd "$tmpdir"
mkdir tmp-unpack
cd tmp-unpack
tar xpSf ../jdk.tar.gz)
# Rename OpenJDK directory to "jdk"
case "$OSTYPE" in
darwin*)
(cd "$tmpdir/tmp-unpack"
case "$ver" in
openjdk10) mv * jdk ;;
*) mv */Contents/Home jdk ;; # At least 8 and 11
esac)
;;
*)
(cd "$tmpdir/tmp-unpack" && mv * jdk)
;;
esac
# Remove current JDK if exists
rm -rf "$install/jdk"
mkdir -p "$install"
# Move new JDK to installation directory
mv "$tmpdir/tmp-unpack/jdk" "$install/jdk"
# Cache new JDK version
echo "$ver" > "$install/jdk/jdk-flavor"
# Cache current time as installation time
epochsecs > "$install_time"
|