2020-11-10 22:26:26 +01:00
#!/usr/bin/env bash
#
# Downloads openjdk from AdoptOpenJDK by accessing the API.
# The API is documented at https://api.adoptopenjdk.net/swagger-ui/
#
source $( dirname $0 ) /logger.inc
2020-11-11 21:15:04 +01:00
set -e
2020-11-10 22:26:26 +01:00
case " $( uname) " in
Linux*)
JDK_OS = linux
COMPONENTS_TO_STRIP = 1 # e.g. openjdk-11.0.3+7/bin/java
; ;
Darwin*)
JDK_OS = mac
COMPONENTS_TO_STRIP = 3 # e.g. jdk-11.0.3+7/Contents/Home/bin/java
; ;
CYGWIN*| MINGW*)
JDK_OS = windows
; ;
*)
2020-11-11 21:15:04 +01:00
log_error " Unknown OS: $( uname) "
exit 1
2020-11-10 22:26:26 +01:00
; ;
esac
2020-11-11 22:56:02 +01:00
OPENJDK_VERSION = $1
2020-11-11 21:15:04 +01:00
DOWNLOAD_URL = $( curl --silent -X GET " https://api.adoptopenjdk.net/v3/assets/feature_releases/ ${ OPENJDK_VERSION } /ga?architecture=x64&heap_size=normal&image_type=jdk&jvm_impl=hotspot&os= ${ JDK_OS } &page=0&page_size=1&project=jdk&sort_method=DEFAULT&sort_order=DESC&vendor=adoptopenjdk " \
-H "accept: application/json" \
| jq -r ".[0].binaries[0].package.link" )
OPENJDK_ARCHIVE = $( basename ${ DOWNLOAD_URL } )
log_debug " Archive name: ${ OPENJDK_ARCHIVE } "
2020-11-10 22:26:26 +01:00
CACHE_DIR = ${ HOME } /.cache/openjdk
TARGET_DIR = ${ HOME } /openjdk${ OPENJDK_VERSION }
mkdir -p ${ CACHE_DIR }
mkdir -p ${ TARGET_DIR }
if [ ! -e ${ CACHE_DIR } /${ OPENJDK_ARCHIVE } ] ; then
log_info " Downloading from ${ DOWNLOAD_URL } to ${ CACHE_DIR } "
2020-11-11 21:15:04 +01:00
curl --location --output ${ CACHE_DIR } /${ OPENJDK_ARCHIVE } " ${ DOWNLOAD_URL } "
2020-11-10 22:26:26 +01:00
else
log_info " Skipped download, file ${ CACHE_DIR } / ${ OPENJDK_ARCHIVE } already exists "
fi
log_info " Extracting to ${ TARGET_DIR } "
2020-11-11 21:15:04 +01:00
case " $OPENJDK_ARCHIVE " in
*.zip)
7z x ${ CACHE_DIR } /${ OPENJDK_ARCHIVE } -o${ TARGET_DIR }
mv ${ TARGET_DIR } /*/* ${ TARGET_DIR } /
; ;
*.tar.gz)
tar --extract --file ${ CACHE_DIR } /${ OPENJDK_ARCHIVE } -C ${ TARGET_DIR } --strip-components= ${ COMPONENTS_TO_STRIP }
; ;
*)
log_error " Unknown filetype: ${ OPENJDK_ARCHIVE } "
exit 1
; ;
esac
2020-11-10 22:26:26 +01:00
2020-11-11 22:56:02 +01:00
if [ ! -e ${ HOME } /java.env ] ; then
cat > ${ HOME } /java.env <<EOF
2020-11-10 22:26:26 +01:00
export JAVA_HOME = " ${ TARGET_DIR } "
export PATH = " ${ TARGET_DIR } /bin: ${ PATH } "
EOF
2020-11-11 22:56:02 +01:00
log_info " OpenJDK can be used via ${ HOME } /java.env "
cat ${ HOME } /java.env
source ${ HOME } /java.env
java -version
else
log_info " ${ HOME } /java.env already existed and has not been changed "
log_info " OpenJDK ${ OPENJDK_VERSION } can be used from ${ TARGET_DIR } "
fi