Skip to content

Commit 8a18b7d

Browse files
authored
Merge pull request #13 from openjavaformat/gradle-plugin-bundled-formatter
Say the Gradle plugin carries the formatter and needs no root repository
2 parents 732562c + 020560c commit 8a18b7d

2 files changed

Lines changed: 58 additions & 19 deletions

File tree

‎docs/get-started/gradle.md‎

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ formatter version as the build. It needs Gradle 9 and a Gradle daemon running on
3131
}
3232
```
3333

34-
The formatter itself is downloaded from Maven Central, in the same version as the plugin.
34+
The formatter comes inside the plugin, in the plugin's own version: the build declares nothing for it
35+
and needs no repository for it. The repository above is for the build's own dependencies, and for the
36+
native binary below.
3537

3638
## Choose how the formatter runs
3739

@@ -45,7 +47,10 @@ the two settings below; without either, `formatDiff` stops before the first file
4547
```
4648

4749
Gradle then runs the formatter as a native binary, outside its own JVM. This works on Linux
48-
with glibc, on macOS and on Windows on x86-64.
50+
with glibc, on macOS and on Windows on x86-64. The binary, some 60 MB, is
51+
`dev.openjavaformat:open-java-format-native` from Maven Central: each project that formats
52+
resolves it through its own repositories, `mavenCentral()` or a mirror of it, and runs a copy
53+
kept in `~/.gradle/open-java-format-caches`.
4954

5055
=== "On the Gradle JVM"
5156

@@ -74,8 +79,9 @@ With a clean working tree it has nothing to do.
7479

7580
## Multi-project builds
7681

77-
Declare the plugin once in the root project and apply it wherever there is Java code. The formatter
78-
is resolved by the root project, so the root project needs the repository too.
82+
Declare the plugin once in the root project and apply it wherever there is Java code. Each project
83+
resolves what it needs through its own repositories, so the root project needs none; a parallel
84+
build (`--parallel`) works too.
7985

8086
=== "Groovy"
8187

@@ -84,15 +90,13 @@ is resolved by the root project, so the root project needs the repository too.
8490
id 'dev.openjavaformat.java-format' version '{{ ojf_version }}' apply false
8591
}
8692

87-
allprojects {
88-
repositories {
89-
mavenCentral()
90-
}
91-
}
92-
9393
subprojects {
9494
apply plugin: 'java'
9595
apply plugin: 'dev.openjavaformat.java-format'
96+
97+
repositories {
98+
mavenCentral()
99+
}
96100
}
97101
```
98102

@@ -103,15 +107,13 @@ is resolved by the root project, so the root project needs the repository too.
103107
id("dev.openjavaformat.java-format") version "{{ ojf_version }}" apply false
104108
}
105109

106-
allprojects {
107-
repositories {
108-
mavenCentral()
109-
}
110-
}
111-
112110
subprojects {
113111
apply(plugin = "java")
114112
apply(plugin = "dev.openjavaformat.java-format")
113+
114+
repositories {
115+
mavenCentral()
116+
}
115117
}
116118
```
117119

@@ -131,6 +133,40 @@ Up to 21.0.1, `formatDiff` and the Spotless step fail with the cache on
131133
Gradle's own `idea` task does not support the cache, and Gradle turns it off for a build that runs
132134
the task.
133135

136+
## Build cache
137+
138+
The [Spotless](spotless.md) task `spotlessJava` is cacheable, and the formatter is part of its cache
139+
key: another formatter version runs the task again, while a copy of the project in another directory,
140+
or on another machine, takes the result from the cache. A build that cannot run the formatter, for
141+
want of the `--add-exports` flags above, fails `spotlessJava` and leaves nothing in the cache.
142+
143+
Up to 21.0.1, the key did not change with the formatter version, and a failure of the formatter went
144+
into the cache as the task's result.
145+
146+
## Another formatter version
147+
148+
The plugin formats with the version that comes inside it. To try another one, declare it in the root
149+
project. Each project that formats resolves it through its own repositories, and so does the root
150+
project, for the IntelliJ IDEA settings below:
151+
152+
=== "Groovy"
153+
154+
``` groovy title="build.gradle"
155+
dependencies {
156+
palantirJavaFormat 'dev.openjavaformat:open-java-format:<version>'
157+
}
158+
```
159+
160+
=== "Kotlin"
161+
162+
``` kotlin title="build.gradle.kts"
163+
dependencies {
164+
"palantirJavaFormat"("dev.openjavaformat:open-java-format:<version>")
165+
}
166+
```
167+
168+
The IntelliJ IDEA settings below then name that version too.
169+
134170
## IntelliJ IDEA
135171

136172
When the project is opened in IntelliJ IDEA, the plugin writes the formatter settings into `.idea`,

‎docs/get-started/spotless.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ that line.
3939
```
4040

4141
The step runs the formatter inside the Gradle JVM, so it needs the `--add-exports` flags. The native
42-
formatter property from the [Gradle plugin](gradle.md) page does not help here: on Java 21 and
43-
later the Spotless step does not use it.
42+
formatter property from the [Gradle plugin](gradle.md) page does not help here: the Spotless step
43+
always runs on the Gradle JVM.
4444

4545
``` properties title="gradle.properties"
4646
org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
@@ -55,7 +55,10 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME
5555
./gradlew spotlessCheck # fail when a file is not formatted
5656
```
5757

58-
!!! warning "An error that stays after the fix"
58+
Without the flags `spotlessJava` fails and names them. The step works with Gradle's
59+
[configuration cache and build cache](gradle.md#build-cache), and in parallel builds.
60+
61+
!!! warning "Up to 21.0.1: an error that stays after the fix"
5962

6063
Without the flags the step fails with `NoClassDefFoundError: Could not initialize class
6164
com.palantir.javaformat.java.ImportOrderer`. Spotless remembers that result, so after adding

0 commit comments

Comments
 (0)