From 385fd9493e9aea954019dc31e2d4df1dd343b3c3 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Tue, 1 Dec 2026 08:00:16 +0300 Subject: [PATCH 1/2] Show builds on JDK 17 how to use the 17 line 17.1.0 is out: the formatter and the Gradle plugin built for Java 17, which format every file that javac 17 parses as the 21 release of the same minor version does. The Gradle page gets a section on it: the plugin version, the native binary through openjavaformat.native.formatter.version, and a Dependabot rule that keeps a build on 17.x. Spotless and Get started name 17.x for Java 17 to 20. zensical.toml holds the 17 line's version and its 21 release as variables of their own, which follow-releases.yml does not move. --- docs/get-started/gradle.md | 56 +++++++++++++++++++++++++++++++++++- docs/get-started/index.md | 3 +- docs/get-started/spotless.md | 8 +++++- zensical.toml | 5 ++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/docs/get-started/gradle.md b/docs/get-started/gradle.md index ea5c07d..a43c093 100644 --- a/docs/get-started/gradle.md +++ b/docs/get-started/gradle.md @@ -1,7 +1,8 @@ # Gradle plugin The plugin adds a task that formats the lines you changed, and it keeps IntelliJ IDEA on the same -formatter version as the build. It needs Gradle 9 and a Gradle daemon running on Java 21 or later. +formatter version as the build. It needs Gradle 9 and a Gradle daemon running on Java 21 or later; on +Java 17 to 20, see [Builds on JDK 17](#builds-on-jdk-17). ## Apply the plugin @@ -77,6 +78,59 @@ even when the native binary is switched on. See [Spotless](spotless.md). The task looks at `git diff HEAD` and formats only the changed lines of the `.java` files in it. With a clean working tree it has nothing to do. +## Builds on JDK 17 + +A Gradle daemon on Java 17 to 20 cannot load the plugin above, which is built for Java 21. Apply +17.x instead, the formatter and the plugin built for Java 17: + +=== "Groovy" + + ``` groovy title="build.gradle" + plugins { + id 'java' + id 'dev.openjavaformat.java-format' version '{{ ojf_17_version }}' + } + ``` + +=== "Kotlin" + + ``` kotlin title="build.gradle.kts" + plugins { + java + id("dev.openjavaformat.java-format") version "{{ ojf_17_version }}" + } + ``` + +On every file that javac 17 parses, a 17.x formats exactly as the 21.x of the same minor version: +{{ ojf_17_version }} as {{ ojf_17_native_version }}. Syntax newer than Java 17 needs 21.x. The rest of +this page holds for 17.x as well. + +17.x has no native binary of its own. With `openjavaformat.native.formatter`, name the 21 release +whose binary the plugin runs; the binary does not depend on the JDK the build runs on: + +``` properties title="gradle.properties" +openjavaformat.native.formatter=true +openjavaformat.native.formatter.version={{ ojf_17_native_version }} +``` + +Without the second line the plugin asks for `open-java-format-native` in its own version, which +does not exist, and the build fails to resolve it. + +Dependabot offers 21.x to a build on 17.x, as a major version up. To keep the build on 17.x, ignore +the major updates: + +``` yaml title=".github/dependabot.yml" +version: 2 +updates: + - package-ecosystem: gradle + directory: / + schedule: + interval: weekly + ignore: + - dependency-name: "dev.openjavaformat*" + update-types: ["version-update:semver-major"] +``` + ## Multi-project builds Declare the plugin once in the root project and apply it wherever there is Java code. Each project diff --git a/docs/get-started/index.md b/docs/get-started/index.md index 71cde58..5a200b2 100644 --- a/docs/get-started/index.md +++ b/docs/get-started/index.md @@ -8,7 +8,8 @@ the same formatter, so they all produce the same files; the one exception is the The current version is **{{ ojf_version }}**. Everything except the native binaries needs Java 21 or later, and the Gradle plugin needs Gradle 9. The Maven plugin has version numbers of its own; the - current one is **{{ maven_plugin_version }}**. + current one is **{{ maven_plugin_version }}**. On Java 17 to 20, the [Gradle plugin](gradle.md#builds-on-jdk-17) + and [Spotless](spotless.md#maven) take **{{ ojf_17_version }}**, built for Java 17. ## In the build diff --git a/docs/get-started/spotless.md b/docs/get-started/spotless.md index 29e6e22..be2b615 100644 --- a/docs/get-started/spotless.md +++ b/docs/get-started/spotless.md @@ -11,7 +11,8 @@ so the `spotless` block needs no formatter step. If it still calls `palantirJava that line. In an [Android module](gradle.md#android-projects) the step formats the Java files of the Android -source sets. A `target` in the `spotless` block replaces them. +source sets. A `target` in the `spotless` block replaces them. A Gradle daemon on Java 17 to 20 +applies {{ ojf_17_version }} instead, as in [Builds on JDK 17](gradle.md#builds-on-jdk-17). === "Groovy" @@ -79,6 +80,11 @@ option comes with Spotless Maven plugin 3.12.0 21.0.2 or later, because the step asks for the `PALANTIR` style, and Maven has to run on Java 21 or later. Unlike the Gradle step, it needs no `--add-exports` flags. +On Java 17 to 20, name {{ ojf_17_version }} as the version, the formatter built for Java 17. On every +file that javac 17 parses it formats exactly as {{ ojf_17_native_version }}, so a build that checks the +format on JDK 17 in one place and on JDK 21 in another sees one output. Syntax newer than Java 17 +needs 21.x. + ``` xml title="pom.xml" com.diffplug.spotless diff --git a/zensical.toml b/zensical.toml index ee8b2c4..8026855 100644 --- a/zensical.toml +++ b/zensical.toml @@ -119,6 +119,11 @@ toggle.name = "Switch to system preference" # extra.version: the theme reads that one as the switch for a version selector. [project.extra] ojf_version = "21.1.2" +# The 17 line, for builds on JDK 17 to 20, and the 21 release of the same minor version, whose native binary its +# Gradle plugin runs. A 17.x is never the latest release, so follow-releases.yml leaves these two alone: they move +# by hand with each release of the 17 line. +ojf_17_version = "17.1.0" +ojf_17_native_version = "21.1.2" # The Maven plugin is released from its own repository, openjavaformat/fmt-maven-plugin, with version # numbers of its own. follow-maven-plugin-releases.yml bumps it on each release of the plugin. maven_plugin_version = "2.27.0.2" From 2fa040126fb838c73dc462f43bcd0dffbb358f3d Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Tue, 1 Dec 2026 10:05:35 +0300 Subject: [PATCH 2/2] Follow the releases of the 17 line as well The hourly bump followed ojf_version alone, with the latest release, which a 17.x never is. It now also sets ojf_17_version to the newest published 17.x, and ojf_17_native_version to the newest 21 release of that minor version, whose native binary the 17 line's Gradle plugin runs. Each moves only to a higher version, and one commit names every version that moved. --- .github/workflows/follow-releases.yml | 66 ++++++++++++++++++--------- zensical.toml | 4 +- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/.github/workflows/follow-releases.yml b/.github/workflows/follow-releases.yml index 37117d3..3849683 100644 --- a/.github/workflows/follow-releases.yml +++ b/.github/workflows/follow-releases.yml @@ -1,11 +1,17 @@ name: Follow open-java-format releases -# The pages install the formatter version that zensical.toml names as ojf_version. Once an hour this -# compares it with the latest release of openjavaformat/open-java-format. When a new release is out, -# that line is changed, the result is pushed to main, and Documentation is run on it to deploy the -# site. That run has to be explicit: a push made with the workflow's own token starts no -# push-triggered workflow. Nothing but that line changes: the pages name earlier versions as -# history, on Migrate for instance, and those stay as written. +# The pages install the formatter versions that zensical.toml names. Once an hour this compares +# them with the releases of openjavaformat/open-java-format: +# +# - ojf_version with the latest release; +# - ojf_17_version with the newest release of the 17 line, which is never the latest release, and +# ojf_17_native_version with the newest 21 release of the same minor version, whose native binary +# the 17 line's Gradle plugin runs. +# +# When a new release is out, those lines are changed, the result is pushed to main, and Documentation +# is run on it to deploy the site. That run has to be explicit: a push made with the workflow's own +# token starts no push-triggered workflow. Nothing but those lines changes: the pages name earlier +# versions as history, on Migrate for instance, and those stay as written. on: schedule: @@ -31,29 +37,47 @@ jobs: env: GH_TOKEN: ${{ github.token }} run: | - current=$(sed -n 's/^ojf_version = "\(.*\)"$/\1/p' zensical.toml) - release=$(gh release view --repo openjavaformat/open-java-format --json tagName,publishedAt) - latest=$(jq -r .tagName <<< "$release") - published=$(jq -r '.publishedAt[:10]' <<< "$release") - # The latest release is the one published last, and a fix for an older line can come out - # after a newer line started: only a higher version number is a bump. - newer=$(printf '%s\n' "$current" "$latest" | sort -V | tail -n 1) - if [ "$latest" = "$current" ] || [ "$newer" != "$latest" ]; then - echo "Already on $current; the latest release is $latest." + # The newest published release whose tag starts with $1, drafts and prereleases left out. + newest() { + gh release list --repo openjavaformat/open-java-format --limit 1000 \ + --json tagName,isDraft,isPrerelease \ + --jq '.[] | select((.isDraft or .isPrerelease) | not) | .tagName' \ + | grep -E "^${1//./\\.}" | sort -V | tail -n 1 || true + } + bumped=() + # Sets the line `$1 = "..."` of zensical.toml to the version $2, when $2 is higher. A fix for an + # older line can come out after a newer line started: only a higher version number is a bump. + bump() { + local key=$1 version=$2 current higher + current=$(sed -n "s/^$key = \"\(.*\)\"$/\1/p" zensical.toml) + higher=$(printf '%s\n' "$current" "$version" | sort -V | tail -n 1) + if [ -z "$version" ] || [ "$version" = "$current" ] || [ "$higher" != "$version" ]; then + echo "$key stays $current; the release found is ${version:-none}." + return + fi + # The dots are escaped so that sed takes them literally. + sed -i "s/^$key = \"${current//./\\.}\"$/$key = \"$version\"/" zensical.toml + bumped+=("$key $current -> $version") + } + + bump ojf_version "$(gh release view --repo openjavaformat/open-java-format --json tagName --jq .tagName)" + bump ojf_17_version "$(newest 17.)" + minor=$(sed -n 's/^ojf_17_version = "17\.\([0-9]*\)\..*"$/\1/p' zensical.toml) + bump ojf_17_native_version "$(newest "21.${minor}.")" + + if [ "${#bumped[@]}" -eq 0 ]; then exit 0 fi - - # The dots are escaped so that sed takes them literally. - sed -i "s/^ojf_version = \"${current//./\\.}\"$/ojf_version = \"$latest\"/" zensical.toml git diff git config user.name 'github-actions[bot]' git config user.email '41898282+github-actions[bot]@users.noreply.github.com' git commit --all --quiet --file - <