Skip to content

Commit

Permalink
[b/342096412] Detect Java version in the main script (#434)
Browse files Browse the repository at this point in the history
* [b/342096412] Detect Java version in the main script

The `dwh-migration-dumper` script worked for Java version 9 and above.
This change makes it compatible with Java 8 also in Linux environments.

The `dwh-migration-dumper-java8` script is a wrapper for the old script with the same name. It is added to preserve backwards compatibility.

The Java detection is not applied for Windows environments. The bat scripts are added to preserve backwards compatibility.

The new scripts in the bin directory behave the same in the source and binary releases.

* Use JDK_JAVA_OPTIONS to handle all Java versions

The JDK_JAVA_OPTIONS is passed to Java 9+ only, so by using this
environment variable, there is no need to use the Java version detection
script.
  • Loading branch information
dawidxc authored Aug 21, 2024
1 parent b7bc229 commit c1f5037
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 20 deletions.
25 changes: 20 additions & 5 deletions bin/dwh-migration-dumper
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#!/bin/sh -ex
#!/bin/bash
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

BASE_DIR=$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>&1 > /dev/null && cd .. && pwd)
. "${BASE_DIR}/dumper/app/src/main/sh/java_versioning.sh"
BASE_BIN_DIR="${BASE_DIR}/dumper/app/build/install/app/bin"
BIN=$(is_java_greater_than_8 && echo "${BASE_BIN_DIR}/dwh-migration-dumper" || echo "${BASE_BIN_DIR}/dwh-migration-dumper-java8")
BIN="${BASE_DIR}/dumper/app/build/install/app/bin/dwh-migration-dumper-starter"

if [ ! -x "$BIN" ] ; then
(cd "${BASE_DIR}" && ./gradlew --parallel :dumper:app:installDist)
fi

exec "$BIN" "$@"
export JDK_JAVA_OPTIONS="--add-opens=java.base/java.nio=ALL-UNNAMED"

exec "$BIN" "$@"
19 changes: 19 additions & 0 deletions bin/dwh-migration-dumper.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@rem Copyright 2024 Google LLC
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.

@if "%DEBUG%"=="" @echo off
set JDK_JAVA_OPTIONS="--add-opens=java.base/java.nio=ALL-UNNAMED"

set "DIRNAME=%~dp0..\dumper\app\build\install\app\bin"
"%DIRNAME%\dwh-migration-dumper-starter.bat" %*
16 changes: 7 additions & 9 deletions dumper/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,11 @@ jacocoTestReport {
}

startScripts {
applicationName = "dwh-migration-dumper-starter"
classpath = files('$APP_HOME/lib/*')
defaultJvmOpts = ["--add-opens=java.base/java.nio=ALL-UNNAMED"]
}

task createStartScriptForJava8(type: CreateStartScripts) {
mainClass = tasks.startScripts.mainClass
applicationName = 'dwh-migration-dumper-java8'
outputDir = tasks.startScripts.outputDir
classpath = tasks.startScripts.classpath
}

tasks.named('installDist') {
dependsOn 'createStartScriptForJava8'
dependsOn 'cloudExtractorStartScripts'
dependsOn 'cloudExtractorStartScriptsForJava8'
}
Expand Down Expand Up @@ -944,6 +936,12 @@ distributions {
from(cloudExtractorStartScripts) {
into "bin"
}
from("src/main/sh/dwh-migration-dumper") {
into "bin"
}
from("src/main/sh/dwh-migration-dumper.bat") {
into "bin"
}
from(generateLicenseReport) {
into "docs/licenses"
}
Expand Down
23 changes: 23 additions & 0 deletions dumper/app/src/main/sh/dwh-migration-dumper
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

BASE_DIR=$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>&1 > /dev/null && cd .. && pwd)
BIN="${BASE_DIR}/bin/dwh-migration-dumper-starter"

export JDK_JAVA_OPTIONS="--add-opens=java.base/java.nio=ALL-UNNAMED"

exec "$BIN" "$@"
19 changes: 19 additions & 0 deletions dumper/app/src/main/sh/dwh-migration-dumper.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@rem Copyright 2024 Google LLC
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.

@if "%DEBUG%"=="" @echo off
set JDK_JAVA_OPTIONS="--add-opens=java.base/java.nio=ALL-UNNAMED"

set "DIRNAME=%~dp0"
"%DIRNAME%\dwh-migration-dumper-starter.bat" %*
24 changes: 18 additions & 6 deletions dumper/app/src/main/sh/java_versioning.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
#!/bin/bash
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function is_java_greater_than_8() {
java_path=$(which java)
java_version_output=$($java_path -version 2>&1)
java_version_output="$(java -version 2>&1)"

# Will return 1 for all versions before Java 9, but we do not care.
java_major_version=$(echo "$java_version_output" | grep -Eoi 'version "?([0-9]+)' | head -1 | cut -d'"' -f2 | cut -d'.' -f1)
# Will return 1 for all versions before Java 9.
java_major_version="$(echo "$java_version_output" | grep -Eoi 'version "?([0-9]+)' | head -1 | cut -d'"' -f2 | cut -d'.' -f1)"

# Check if the major version is greater than 8.
if [[ $java_major_version -gt 8 ]]; then
if [[ "$java_major_version" -gt 8 ]]; then
return 0 # True
else
return 1 # False
fi
}
}

0 comments on commit c1f5037

Please sign in to comment.