Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
442 changes: 57 additions & 385 deletions .github/workflows/release.yml

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# Working in this repository

Building and testing: the README's "Building and testing locally" section. Gradle runs on the Temurin 21 that
`mise install` provides, `./gradlew check` is what CI runs, and `./gradlew formatDiff` formats the files you changed.
Building and testing: the README's "Building and testing locally" section. This is the 17 line: Gradle, the toolchain
and the tests run on the Temurin 17 that `mise install` provides, and `./gradlew check` is what CI runs. Until 17.1.0
is out nothing in the build formats the code: format the files you changed with the 21 line's runnable jar on JDK 21,
`java -jar open-java-format-21.1.2-all.jar --replace <files>`. Error Prone, NullAway and Checkstyle do not run here;
the code is checked on 21.X.X and comes here as backports.

## Conventions for new code

- **Java 17.** No syntax or API newer than Java 17: javac 17 rejects them. A `switch` over the types of a sealed
interface is an `instanceof` chain that throws in its last branch. A backport that needs such a change says so in
a paragraph of its commit message, under the original one (`git cherry-pick -x`).
- **Collections.** Plain `java.util` types. `List.of`, `List.copyOf`, `Map.of` and `Stream.toList()` give
unmodifiable collections; unlike Guava's, `Set.of` and `Map.of` do not keep insertion order, and `contains(null)`
on them throws. Guava's `ImmutableList`, `ImmutableMap` and `ImmutableSet` are banned by forbidden-apis
(`config/forbidden-apis`), which fails the build, in every module but the SPI: `FormatterService` keeps returning
`ImmutableList` for compatibility, and the methods that implement it carry their module's `@SuppressForbidden`.
- **Nullability.** One annotation, JSpecify's `org.jspecify.annotations.Nullable`; do not add another `Nullable`,
nor a `NotNull` or `NonNull`: what is not `@Nullable` is non-null, and NullAway checks it. It is a type-use
annotation, so on a nested type it goes before the simple name (`Input.@Nullable Token`), and on an array before
the brackets (`String @Nullable []`).
nor a `NotNull` or `NonNull`: what is not `@Nullable` is non-null, and NullAway checks it on the 21 line. It is a
type-use annotation, so on a nested type it goes before the simple name (`Input.@Nullable Token`), and on an array
before the brackets (`String @Nullable []`).
- **Local variables.** `var` wherever the initializer gives the type unchanged, in `for` loops too. Not where it
would box a primitive (`for (var n : List<Integer>)`), and not without an initializer that has a type of its own
(a lambda, `null`).
Expand Down
81 changes: 35 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ and published from this repository.

Why the project exists is in the [manifesto](https://openjavaformat.dev/manifesto/).

## The 17 line

This branch, `17.X.X`, is the formatter and the Gradle plugin built for Java 17, for builds that run on JDK 17 to
20: Maven builds such as those of apache/maven-parent and the Plexus and MojoHaus parents, and Gradle builds whose
daemon runs on JDK 17. On every file that javac 17 parses, a 17.x formats exactly as the 21.x of the same minor
version: 17.1.x as 21.1.x. On JDK 21 and later, use 21.x, from the `21.X.X` branch.

- **What it publishes.** `open-java-format`, `-spi` and `-jdk-bootstrap` on Maven Central, the Gradle plugin on the
Gradle Plugin Portal, and the runnable jar on the GitHub release. The native image, the IDE plugins, the .deb
packages and the WebAssembly module do not depend on the JDK a build runs on, and come with the 21 line.
- **The Gradle plugin's native formatter.** Set `openjavaformat.native.formatter.version` to the 21 release of the
same minor version, `21.1.2` for 17.1.x, and the plugin runs that native image. Without it the plugin asks for a
native image of its own version, which the 17 line does not publish.
- **What reaches it.** Backports from 21.X.X: changes to the output, and fixes for crashes and security issues.
Syntax newer than Java 17 stays on the 21 line.
- **How long it lives.** As long as large projects build on JDK 17. This README announces its last release.

## Get started

Each page is short and every command on it was run against the published artifacts.
Expand Down Expand Up @@ -114,8 +131,9 @@ private static void configureResolvedVersionsWithVersionMapping(Project project)

A version is three numbers, `<java>.<minor>.<build>`, and each one answers a question of its own.

- **The first is the Java version the formatter is built for**, 21 today. It moves when the minimum
Java version moves.
- **The first is the Java version the formatter is built for**: 17 on this line, 21 on the main one.
It moves when the minimum Java version moves. The 17 line follows the minor versions of the 21
line, 17.1.x formats as 21.1.x does, and its third number counts its own releases.
- **The second moves with every change to the output**: a fix in wrapping or alignment, a new rule,
a new feature. A newer minor version can reformat code that the previous one left alone.
- **The third moves with fixes that leave the output alone**: a crash, a regression, an internal
Expand All @@ -129,54 +147,34 @@ fixes made the output differ from upstream, that numbering lost its meaning.

## Building and testing locally

[mise](https://mise.jdx.dev/) pins the tooling: `mise.toml` installs Temurin 21 and
[act](https://github.com/nektos/act).
This is the 17 line: everything builds and runs on JDK 17. [mise](https://mise.jdx.dev/) pins the
tooling: `mise.toml` installs Temurin 17 and [act](https://github.com/nektos/act).

```bash
mise trust && mise install
./gradlew check # what the CI build job runs: the tests, Checkstyle and forbidden-apis
./gradlew check # what the CI build job runs: the tests and forbidden-apis
```

`-PjavaRuntime=25` runs the same tests on JDK 25, as CI's `jdk` jobs do for 25, 26 and 27, and for
the early-access builds of 28, where value classes are a preview feature and the `value` modifier
tests run. The code is compiled for Java 21 either way. Gradle has to find that JDK: installed with
mise, or named with `-Porg.gradle.java.installations.paths=/path/to/jdk`.
Error Prone, NullAway and Checkstyle do not run on this line: their releases are built for Java 21,
and the code they would check is checked on the 21 line, from which it comes here as backports.

The Gradle plugins' tests build their projects with Gradle 9.3.1, the oldest the plugins support.
`-PtestkitGradleVersion=9.8.0` builds them with another, as CI's JDK 27 leg does: 9.3.1 does not run
on Java 27, and 9.8.0 is the oldest release that does.

Nothing inside the build downloads a JDK. `gradle.properties` turns toolchain auto-download off and
reads the installations from `JDK21_HOME` and `GRAALVM_HOME`, so a missing JDK is an error you can
read rather than a silent download.
reads the installation from `JDK17_HOME`, so a missing JDK is an error you can read rather than a
silent download.

The native image needs a GraalVM 25 behind `GRAALVM_HOME`. CI installs it with
`graalvm/setup-graalvm` and builds the image in a job of its own:
The released Gradle plugin that formats this repository is built for Java 21, so the 17 line applies
none until 17.1.0 is out. Until then, format the files you change with the 21 line's runnable jar on
JDK 21:

```bash
./gradlew -PnativeImage=true :open-java-format-native:nativeCompile
java -jar open-java-format-21.1.2-all.jar --replace <files>
```

GraalVM has to come from outside Gradle: unpacking a JDK that Gradle downloaded itself does not
preserve the symlink GraalVM ships at `bin/native-image`, and `nativeCompile` then fails on an empty,
unexecutable file.

### WebAssembly module

The playground on openjavaformat.dev runs the formatter in the browser, compiled to WebAssembly by
GraalVM's Web Image backend. CI builds and tests it in its `wasm` job, the release workflow in its
own, and the zip goes on the GitHub release; `./gradlew build` leaves it out. Web Image ships in Oracle GraalVM only, so the
module needs Oracle GraalVM 25 behind `GRAALVM_HOME`, Binaryen 119 or later (`wasm-opt`) on the
`PATH`, and Node.js 24 or later for the tests:

```bash
./gradlew -Pwasm=true :open-java-format-wasm:wasmTest # build, then test in Node against the runnable jar
./gradlew -Pwasm=true :open-java-format-wasm:wasmDev # a bare page next to the module, to try it
jwebserver -d "$PWD/open-java-format-wasm/build/wasm-dev"
```

Without `-Pwasm=true` the subproject is not part of the build at all: it compiles against
`org.graalvm.webimage.api`, a module of Oracle GraalVM that no other JDK has.
The native image, the IntelliJ and Eclipse plugins, the WebAssembly module and the benchmarks are
built on the 21 line. Here they stay in the tree, commented out in `settings.gradle`, so that a
backport that touches them applies as it is.

### Running CI locally

Expand All @@ -186,18 +184,9 @@ is pushed:
```bash
mise run ci:list # what jobs exist
mise run ci:lint # dry-run every job: resolves images, actions and steps, executes nothing
mise run ci:build # actually run the linux jar+native build job
mise run ci:native # actually run the linux-aarch64 native job
mise run ci:build # actually run the build job
```

Two caveats:

- **act cannot containerise macOS**, so the `macos-26-intel` leg has to be checked by building the
native image on a Mac.
- **On an Apple Silicon host** the `ubuntu-24.04` container runs as arm64, so the job labelled
`ubuntu-latest` produces an aarch64 binary locally. Fine for exercising the workflow, not a
substitute for real CI.

## License and attribution

This project is a fork of [palantir-java-format](https://github.com/palantir/palantir-java-format),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@ plugins {
id 'org.jreleaser'
}

// Two deployments, one version. A published groupId:artifactId:version can never gain files, so every
// platform's native binary has to travel in a single deployment — and those binaries come from one CI
// job per platform, long after the jars are staged. `-PreleaseTarget=native` deploys the binaries those jobs
// collected, the default deploys the jars; on a tag both read the same version from it.
// One deployment, the jars: the 17 line publishes no native image, which comes with the 21 line.
//
// `-PreleaseTarget=github` deploys nothing. It is for `jreleaserRelease`, which opens a draft GitHub
// release on the tag with what Maven Central does not carry — the runnable formatter jar, the Gradle
// plugins' jar, the IntelliJ plugin zip, the Eclipse plugin jar, every platform's native binary as a
// plain download, the Linux binaries as .deb packages, and the WebAssembly module for openjavaformat.dev's
// playground — each file signed, plus a checksum file. Its text is release-notes/<version>.md.
// release on the tag with what Maven Central does not carry — the runnable formatter jar and the Gradle
// plugins' jar — each file signed, plus a checksum file. Its text is release-notes/<version>.md.
//
// Publishing is a run of its own: with JRELEASER_MAVENCENTRAL_DEPLOYMENT_ID set, JReleaser publishes that
// deployment, which an earlier run uploaded and the Portal validated, and nothing is built. The ID has to
// be an environment variable: JReleaser 1.26 drops a deploymentId set in the DSL. The release workflow
// uploads both deployments first and publishes them only once both are in, so a version whose native
// build fails publishes nothing.
// be an environment variable: JReleaser 1.26 drops a deploymentId set in the DSL.
//
// gradle-open-java-format is not deployed here: the release workflow publishes its plugins to the Gradle
// Plugin Portal once Maven Central serves the jars and native images they depend on. The GitHub release
// carries its jar too, built from the same tag.
// Plugin Portal once Maven Central serves the jars they depend on. The GitHub release carries its jar too,
// built from the same tag.
def releaseTarget = providers.gradleProperty('releaseTarget').getOrElse('jars')
def deploymentToPublish = providers.environmentVariable('JRELEASER_MAVENCENTRAL_DEPLOYMENT_ID').getOrNull()
def releasedProjects
Expand All @@ -35,60 +28,15 @@ if (releaseTarget == 'jars') {
':open-java-format',
':open-java-format-jdk-bootstrap',
]
} else if (releaseTarget == 'native') {
releasedProjects = [':open-java-format-native']
} else if (releaseTarget == 'github') {
releasedProjects = []
} else {
throw new GradleException("Unknown -PreleaseTarget=${releaseTarget}: use 'jars', 'native' or 'github'")
throw new GradleException("Unknown -PreleaseTarget=${releaseTarget}: use 'jars' or 'github'")
}
def stagingRepositories = releasedProjects.collect { path ->
relativePath(project(path).layout.buildDirectory.dir('staging-deploy'))
}

// The GitHub release takes the binaries the per-platform jobs collected from -PnativeImages=<dir>, exactly
// as the native deployment does, the .deb packages built from the Linux ones from -PdebPackages=<dir>, and the
// WebAssembly module's zip that the wasm job built from -PwasmModule=<dir>.
def nativeBinaries = []
def debPackages = []
def wasmModules = []
if (releaseTarget == 'github') {
def collectedNativeImages = providers.gradleProperty('nativeImages').getOrNull()
if (collectedNativeImages == null) {
throw new GradleException('-PreleaseTarget=github needs -PnativeImages=<dir> holding the native binaries')
}
nativeBinaries = fileTree(file(collectedNativeImages)) {
include 'open-java-format-*'
exclude '**/*.txt'
}.files.sort { it.name }
if (nativeBinaries.isEmpty()) {
throw new GradleException("No native images to release in ${collectedNativeImages}")
}

def collectedDebPackages = providers.gradleProperty('debPackages').getOrNull()
if (collectedDebPackages == null) {
throw new GradleException('-PreleaseTarget=github needs -PdebPackages=<dir> holding the .deb packages')
}
debPackages = fileTree(file(collectedDebPackages)) {
include 'open-java-format_*.deb'
}.files.sort { it.name }
// One per Linux binary: the apt repository serves both architectures or neither.
if (debPackages*.name.collect { it.replaceAll(/.*_/, '') } != ['amd64.deb', 'arm64.deb']) {
throw new GradleException("Expected an amd64 and an arm64 .deb package in ${collectedDebPackages}, found ${debPackages*.name}")
}

def collectedWasmModule = providers.gradleProperty('wasmModule').getOrNull()
if (collectedWasmModule == null) {
throw new GradleException('-PreleaseTarget=github needs -PwasmModule=<dir> holding open-java-format-wasm-<version>.zip')
}
wasmModules = fileTree(file(collectedWasmModule)) {
include 'open-java-format-wasm-*.zip'
}.files.sort { it.name }
if (wasmModules.size() != 1) {
throw new GradleException("Expected one open-java-format-wasm-<version>.zip in ${collectedWasmModule}, found ${wasmModules.size()}")
}
}

// A provider: the root project sets its version only after applying this plugin, and JReleaser reads the
// file when its task runs.
def releaseNotes = layout.projectDirectory.file(provider { "release-notes/${version}.md" })
Expand All @@ -107,7 +55,7 @@ jreleaser {
java {
groupId = 'dev.openjavaformat'
multiProject = true
version = '21'
version = '17'
}
}
}
Expand All @@ -119,11 +67,14 @@ jreleaser {
skipTag = true
tagName = '{{projectVersion}}'
releaseName = '{{projectVersion}}'
// The current release line; it moves when the next line starts. JReleaser assumes main otherwise,
// and when GitHub has no such branch it tries to push one, which fails from the tag's checkout.
branch = '21.X.X'
// The release line of the tag. JReleaser assumes main otherwise, and when GitHub has no such branch
// it tries to push one, which fails from the tag's checkout.
branch = '17.X.X'
// Published by hand after a look.
draft = true
// The latest release stays the 21 line's: GitHub ignores this for a draft, so the draft is published
// with "Set as the latest release" unchecked too.
makeLatest = 'FALSE'
// The text is release-notes/<version>.md as written; see release-notes/README.md. JReleaser fails
// when the file is missing.
if (releaseTarget == 'github') {
Expand All @@ -135,26 +86,15 @@ jreleaser {
}

// Name templates, not Gradle's version: the root project sets its version only after applying this
// plugin. The jars and the zip are built by the tasks jreleaserRelease depends on below.
// plugin. The jars are built by the tasks jreleaserRelease depends on below.
files {
(nativeBinaries + debPackages + wasmModules).each { file ->
artifact {
path = file.absolutePath
}
}
if (releaseTarget == 'github') {
artifact {
path = 'open-java-format/build/libs/open-java-format-{{projectVersion}}-all.jar'
}
artifact {
path = 'gradle-open-java-format/build/libs/gradle-open-java-format-{{projectVersion}}.jar'
}
artifact {
path = 'open-java-format-idea-plugin/build/distributions/open-java-format-idea-plugin-{{projectVersion}}.zip'
}
artifact {
path = 'open-java-format-eclipse-plugin/build/libs/open-java-format-eclipse-plugin-{{projectVersion}}.jar'
}
}
}

Expand Down Expand Up @@ -204,8 +144,7 @@ tasks.named('jreleaserDeploy') {

tasks.named('jreleaserRelease') {
if (releaseTarget == 'github') {
dependsOn ':open-java-format:allJar', ':gradle-open-java-format:jar',
':open-java-format-idea-plugin:buildPlugin', ':open-java-format-eclipse-plugin:jar'
dependsOn ':open-java-format:allJar', ':gradle-open-java-format:jar'
}
}

Expand Down
1 change: 1 addition & 0 deletions docs/decisions/ADR-002-tests-by-jdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ A test that needs a newer JDK says so on its class, and its case files live in a
- On JDK 23 and 24 the module-import tests no longer run.
- Before a push, the tests run on JDK 21 and 25 locally: `./gradlew test -PjavaRuntime=25 -PtestkitGradleVersion=9.3.1`; a change to the JDK 28 tests also on an early-access build of 28, with `-PjavaRuntime=28 -Porg.gradle.java.installations.paths=<its home>`.
- Comparing the native binary, which parses JDK 25 code whatever the test JVM, with an in-process surface on `test-cases-java25/` needs a test JVM of 25 or later.
- On the 17 line, `17.X.X`, there is no `Java21InputAstVisitor`: javac 17 cannot compile it, and `Formatter.visitorFor` takes `JavaInputAstVisitor` on every JDK (#298). The classes and folders for JDK 21 and later stay there as they are and skip themselves on JDK 17, so that backports apply to them as they are.
Loading
Loading