|
4 | 4 | # google-java-format on JDK 16+. Without these, GJF fails on JDK 17+ with either |
5 | 5 | # IllegalAccessError or NoSuchMethodError when it reaches into com.sun.tools.javac.*. |
6 | 6 | # On JDK <= 15 the flags are unrecognized and would break Maven startup, so we only |
7 | | -# set them when detected JDK major version is >= 16. |
| 7 | +# set them when the detected JDK major version is >= 16. |
| 8 | +# |
| 9 | +# If that JDK can't run google-java-format (most often because it's older than JDK 17, |
| 10 | +# which the pinned GJF 1.27.0 requires), we retry under an explicit JDK 17 install — |
| 11 | +# mirroring the `make fmt` -> `make fmt-jdk17` fallback. Override the fallback location |
| 12 | +# with JDK17_HOME, or just point JAVA_HOME at a JDK 17+ and re-run. |
8 | 13 |
|
9 | 14 | set -euo pipefail |
10 | 15 |
|
11 | | -JDK_VERSION_OUTPUT=$(java -version 2>&1 | head -1) |
12 | | -# Matches `"1.8.0_xxx"` (legacy) and `"17.0.1"` / `"25"` (modern) forms. |
13 | | -JDK_MAJOR=$(echo "$JDK_VERSION_OUTPUT" | sed -E 's/.*version "([0-9]+)(\.[0-9]+)?.*/\1/') |
14 | | -if [ "$JDK_MAJOR" = "1" ]; then |
15 | | - JDK_MAJOR=$(echo "$JDK_VERSION_OUTPUT" | sed -E 's/.*version "1\.([0-9]+).*/\1/') |
16 | | -fi |
| 16 | +# Location used by the JDK 17 fallback. Override for non-Debian layouts, e.g. |
| 17 | +# `JDK17_HOME=$(/usr/libexec/java_home -v 17) bash scripts/mvn-spotless-apply.sh`. |
| 18 | +JDK17_HOME="${JDK17_HOME:-/usr/lib/jvm/java-17-openjdk-amd64}" |
17 | 19 |
|
18 | | -if [ "${JDK_MAJOR:-0}" -ge 16 ]; then |
19 | | - export MAVEN_OPTS="${MAVEN_OPTS:-} \ |
| 20 | +# The --add-exports flags google-java-format needs to reach into com.sun.tools.javac.* |
| 21 | +# on JDK 16+. |
| 22 | +GJF_ADD_EXPORTS="\ |
20 | 23 | --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \ |
21 | 24 | --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \ |
22 | 25 | --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \ |
23 | 26 | --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \ |
24 | 27 | --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \ |
25 | 28 | --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" |
| 29 | + |
| 30 | +# Detect the major version of the JDK Maven will actually use: $JAVA_HOME/bin/java when |
| 31 | +# JAVA_HOME is set, otherwise `java` from PATH. mvn resolves its JVM the same way, so the |
| 32 | +# add-exports gating below matches what the first attempt really runs (and re-running with |
| 33 | +# JAVA_HOME pointed at a JDK 17+ makes that attempt succeed without hitting the fallback). |
| 34 | +JAVA_BIN="java" |
| 35 | +if [ -n "${JAVA_HOME:-}" ] && [ -x "${JAVA_HOME}/bin/java" ]; then |
| 36 | + JAVA_BIN="${JAVA_HOME}/bin/java" |
| 37 | +fi |
| 38 | + |
| 39 | +JDK_VERSION_OUTPUT=$("$JAVA_BIN" -version 2>&1 | head -1) |
| 40 | +# Matches `"1.8.0_xxx"` (legacy) and `"17.0.1"` / `"25"` (modern) forms. |
| 41 | +JDK_MAJOR=$(echo "$JDK_VERSION_OUTPUT" | sed -E 's/.*version "([0-9]+)(\.[0-9]+)?.*/\1/') |
| 42 | +if [ "$JDK_MAJOR" = "1" ]; then |
| 43 | + JDK_MAJOR=$(echo "$JDK_VERSION_OUTPUT" | sed -E 's/.*version "1\.([0-9]+).*/\1/') |
| 44 | +fi |
| 45 | + |
| 46 | +DEFAULT_OPTS="${MAVEN_OPTS:-}" |
| 47 | +if [ "${JDK_MAJOR:-0}" -ge 16 ]; then |
| 48 | + DEFAULT_OPTS="$DEFAULT_OPTS $GJF_ADD_EXPORTS" |
| 49 | +fi |
| 50 | + |
| 51 | +# First attempt: the JDK selected above (JAVA_HOME or PATH). |
| 52 | +if MAVEN_OPTS="$DEFAULT_OPTS" mvn spotless:apply; then |
| 53 | + exit 0 |
| 54 | +fi |
| 55 | + |
| 56 | +echo "" |
| 57 | +echo "==> default JDK could not run google-java-format (likely < JDK 17). Retrying with JDK 17..." |
| 58 | +echo "" |
| 59 | + |
| 60 | +if [ ! -x "$JDK17_HOME/bin/java" ]; then |
| 61 | + echo "error: JDK 17 not found at $JDK17_HOME" >&2 |
| 62 | + echo " install it, set JDK17_HOME to a JDK 17 install, or point JAVA_HOME" >&2 |
| 63 | + echo " at a JDK 17+ and re-run." >&2 |
| 64 | + exit 1 |
26 | 65 | fi |
27 | 66 |
|
28 | | -exec mvn spotless:apply |
| 67 | +# JDK 17 is always >= 16, so the add-exports flags are always required here. |
| 68 | +exec env JAVA_HOME="$JDK17_HOME" \ |
| 69 | + MAVEN_OPTS="${MAVEN_OPTS:-} $GJF_ADD_EXPORTS" \ |
| 70 | + mvn spotless:apply |
0 commit comments