Skip to content

Commit

Permalink
Warn when default OpenJDK version is being used (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax authored May 29, 2024
1 parent 2c80fe9 commit db65551
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

* The buildpack now warns when no OpenJDK version is explicitly specified. Users are encouraged to specify a version to ensure future builds use the same OpenJDK version. ([#301](https://github.com/heroku/heroku-buildpack-jvm-common/pull/301))

### Changed

* Default JDK version for the `heroku-24` stack is now always the latest long-term support version, currently `21`. ([#300](https://github.com/heroku/heroku-buildpack-jvm-common/pull/300))
Expand Down
35 changes: 35 additions & 0 deletions bin/java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,41 @@ install_java_with_overlay() {
local buildDir="${1}"
local cacheDir="${2:-$(mktemp -d)}"
if [ ! -f "${buildDir}/.jdk/bin/java" ]; then
if [ -z "$(_get_system_property "${buildDir}/system.properties" "java.runtime.version")" ]; then
if [ "${STACK}" == "heroku-24" ]; then
warning "No OpenJDK version specified
Your application does not explicitly specify an OpenJDK
version. The latest long-term support (LTS) version will be
installed. This currently is OpenJDK ${DEFAULT_JDK_VERSION}.
This default version will change when a new LTS version is
released. Your application might fail to build with the new
version. We recommend explicitly setting the required OpenJDK
version for your application.
To set the OpenJDK version, add or edit the system.properties
file in the root directory of your application to contain:
java.runtime.version = ${DEFAULT_JDK_VERSION}
"
else
warning "No OpenJDK version specified
Your application does not explicitly specify an OpenJDK
version. OpenJDK ${DEFAULT_JDK_VERSION} will be installed.
This default version will change at some point. Your
application might fail to build with the new version. We
recommend explicitly setting the required OpenJDK version for
your application.
To set the OpenJDK version, add or edit the system.properties
file in the root directory of your application to contain:
java.runtime.version = ${DEFAULT_JDK_VERSION}
"
fi
fi

local jdkVersion
jdkVersion=$(get_jdk_version "${buildDir}")

Expand Down
22 changes: 11 additions & 11 deletions lib/jvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ fi

get_jdk_version() {
local appDir="${1:?}"
if [ -f "${appDir}/system.properties" ]; then
detectedVersion="$(_get_system_property "${appDir}/system.properties" "java.runtime.version")"
if [ -n "$detectedVersion" ]; then
echo "$detectedVersion"
else
echo "$DEFAULT_JDK_VERSION"
fi

configuredVersion="$(_get_system_property "${appDir}/system.properties" "java.runtime.version")"
if [ -n "${configuredVersion}" ]; then
echo "${configuredVersion}"
else
echo "$DEFAULT_JDK_VERSION"
echo "${DEFAULT_JDK_VERSION}"
fi
}

Expand Down Expand Up @@ -208,7 +205,10 @@ _get_system_property() {
local escaped_key
escaped_key="${key//\./\\.}"

[ -f "$file" ] &&
grep -E "^${escaped_key}[[:space:]=]+" "$file" |
sed -E -e "s/$escaped_key([\ \t]*=[\ \t]*|[\ \t]+)([_A-Za-z0-9\.-]*).*/\2/g"
if [ -f "${file}" ]; then
grep -E "^${escaped_key}[[:space:]=]+" "${file}" |
sed -E -e "s/${escaped_key}([\ \t]*=[\ \t]*|[\ \t]+)([_A-Za-z0-9\.-]*).*/\2/g"
else
echo ""
fi
}
1 change: 1 addition & 0 deletions test/compile_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ testCompileWithoutSystemProperties() {

assertCapturedSuccess

assertCaptured "WARNING: No OpenJDK version specified"
assertCaptured "Installing OpenJDK 21"
assertTrue "Java should be present in runtime." "[ -d ${BUILD_DIR}/.jdk ]"
assertTrue "Java version file should be present." "[ -f ${BUILD_DIR}/.jdk/version ]"
Expand Down
6 changes: 6 additions & 0 deletions test/spec/java_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
else
expect(app.output).to include("Installing OpenJDK #{jdk_version}")
end

expect(app.output).not_to include("WARNING: No OpenJDK version specified")
expect(app.output).to include("BUILD SUCCESS")
expect(successful_body(app)).to eq("Hello from Java!")
end
Expand All @@ -34,6 +36,8 @@
write_sys_props(Dir.pwd, "maven.version=3.3.9")
end
app.deploy do
expect(app.output).to include("WARNING: No OpenJDK version specified")

if app.stack == "heroku-24" then
expect(app.output).to include("Installing OpenJDK 21")
else
Expand Down Expand Up @@ -64,6 +68,8 @@
else
expect(app.output).to include("Installing OpenJDK #{jdk_version}")
end

expect(app.output).not_to include("WARNING: No OpenJDK version specified")
expect(app.output).to include("BUILD SUCCESS")

# Workaround (August 2020):
Expand Down

0 comments on commit db65551

Please sign in to comment.