Skip to content

Commit d0f7be0

Browse files
committed
Add JDK 17 fallback to mvn-spotless-apply.sh
scripts/mvn-spotless-apply.sh previously exec'd `mvn spotless:apply` directly, so when the default JDK was older than 17 (the minimum required by the pinned google-java-format 1.27.0) it failed with no recovery. Mirror the `make fmt` -> `make fmt-jdk17` fallback: try the selected JDK first, and if that fails retry under an explicit JDK 17 install, always applying the javac --add-exports flags GJF needs on JDK 16+. To keep the fallback honest and portable: - detect the JDK version from the JVM Maven actually uses ($JAVA_HOME/bin/java if JAVA_HOME is set, else `java` on PATH), so the add-exports gating matches the running JDK and re-running with JAVA_HOME=<jdk17> succeeds on the first attempt; - allow the fallback location to be overridden via JDK17_HOME for non-Debian layouts, and error clearly (pointing at JDK17_HOME / JAVA_HOME) when no JDK 17 is found.
1 parent 27aebf6 commit d0f7be0

1 file changed

Lines changed: 52 additions & 10 deletions

File tree

scripts/mvn-spotless-apply.sh

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,67 @@
44
# google-java-format on JDK 16+. Without these, GJF fails on JDK 17+ with either
55
# IllegalAccessError or NoSuchMethodError when it reaches into com.sun.tools.javac.*.
66
# 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.
813

914
set -euo pipefail
1015

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}"
1719

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="\
2023
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
2124
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
2225
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
2326
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
2427
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
2528
--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
2665
fi
2766

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

Comments
 (0)