Skip to content

Commit

Permalink
feat(java): allow dynamic java install (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Aug 16, 2021
1 parent bce5c3b commit 2f3d203
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Dockerfile.bionic
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ RUN install-tool golang 1.16.7
# renovate: datasource=github-releases lookupName=helm/helm
RUN install-tool helm v3.6.3

# renovate: datasource=docker lookupName=openjdk versioning=docker
RUN install-tool java 11.0.12
# renovate: datasource=adoptium-java
RUN install-tool java 11.0.12+7
# renovate: datasource=gradle-version lookupName=gradle versioning=gradle
RUN install-tool gradle 7.1.1

Expand Down
1 change: 1 addition & 0 deletions src/usr/local/bin/install-buildpack
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ apt_install \
curl \
dumb-init \
gnupg \
jq \
openssh-client \
unzip \
xz-utils \
Expand Down
45 changes: 38 additions & 7 deletions src/usr/local/buildpack/tools/java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,47 @@

set -e

require_root
check_semver $TOOL_VERSION

VERSION_CODENAME=$(. /etc/os-release && echo ${VERSION_CODENAME})
if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then
echo Invalid version: ${TOOL_VERSION}
exit 1
fi

echo "deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu ${VERSION_CODENAME} main" | tee /etc/apt/sources.list.d/java.list
curl -sSL \
'http://keyserver.ubuntu.com/pks/lookup?op=get&search=0xDA1A4A13543B466853BAF164EB9B1D8886F44E2A' \
| apt-key add -
tool_path=$(find_tool_path)

apt_install openjdk-${MAJOR}-jdk-headless
function update_env () {
reset_tool_env
export_tool_env JAVA_HOME "${1}"
export_tool_path "${1}/bin"
}

if [[ -z "${tool_path}" ]]; then
INSTALL_DIR=$(get_install_dir)
base_path=${INSTALL_DIR}/${TOOL_NAME}
tool_path=${base_path}/${TOOL_VERSION}

mkdir -p ${tool_path}

file=/tmp/java.tgz

ARCH=x64
URL=https://api.adoptium.net/v3/assets/version
API_ARGS='heap_size=normal&image_type=jdk&os=linux&page=0&page_size=1&project=jdk&vendor=adoptium'

BIN_URL=$(curl -sSLf -H 'accept: application/json' "${URL}/${TOOL_VERSION}?architecture=${ARCH}&${API_ARGS}" \
| jq --raw-output '.[0].binaries[0].package.link')

curl -sSfLo ${file} ${BIN_URL}
tar --strip 1 -C ${tool_path} -xf ${file}
rm ${file}

update_env ${tool_path}

shell_wrapper java
else
echo "Already installed, resetting env"
update_env ${tool_path}
fi

java -version
2 changes: 1 addition & 1 deletion src/usr/local/buildpack/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function check_command () {
}


SEMVER_REGEX="^(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))?(\.(0|[1-9][0-9]*))?([a-z-].*)?$"
SEMVER_REGEX="^(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))?(\.(0|[1-9][0-9]*))?(\+[0-9]+)?([a-z-].*)?$"

function check_semver () {
if [[ ! "${1}" =~ ${SEMVER_REGEX} ]]; then
Expand Down
41 changes: 29 additions & 12 deletions test/java/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
ARG IMAGE=containerbase/buildpack
FROM ${IMAGE} as build

ARG APT_HTTP_PROXY

# TODO: only lts
# renovate: datasource=docker lookupName=openjdk versioning=docker
RUN install-tool java 11.0.12
FROM ${IMAGE} as base

RUN touch /.dummy

WORKDIR /tmp

COPY --chown=1000:0 test test

#--------------------------------------
# build: Java LTS base
#--------------------------------------
FROM base as build

ARG APT_HTTP_PROXY

# TODO: only lts
# renovate: datasource=adoptium-java
RUN install-tool java 11.0.12+7

#--------------------------------------
# test: gradle 6
#--------------------------------------
Expand Down Expand Up @@ -66,17 +71,29 @@ FROM build as testc
ARG APT_HTTP_PROXY

# need to stay old
RUN install-tool java 8
# renovate: datasource=adoptium-java
RUN install-tool java 8.0.302+8

#--------------------------------------
# test: java 16
# test: java 16 (non-root)
#--------------------------------------
FROM build as testd
FROM base as testd

ARG APT_HTTP_PROXY

# renovate: datasource=docker lookupName=openjdk versioning=docker
RUN install-tool java 16.0.2
USER 1000

# renovate: datasource=adoptium-java
ARG JAVA_VERSION=16.0.2+7
RUN install-tool java

# renovate: datasource=gradle-version lookupName=gradle versioning=gradle
RUN install-tool gradle 7.1.1

RUN gradle --version

# fail if java version mismatch
RUN gradle --version | grep "${JAVA_VERSION}"

#--------------------------------------
# test: maven
Expand Down

0 comments on commit 2f3d203

Please sign in to comment.