Skip to content

Commit 6544a35

Browse files
committed
Add the Maven plugin to Get started
dev.openjavaformat:fmt-maven-plugin 2.27.0.1 is on Maven Central: our fork of spotify/fmt-maven-plugin, released from openjavaformat/fmt-maven-plugin. The new page adds it to a build with the format goal, checks formatting in CI with the check goal and runs either goal from the command line. Every snippet was run against the published 2.27.0.1 with open-java-format 2.98.0.4: the plugin's output is byte-identical to the command line's, check fails on an unformatted file and passes after format, and a build without the open-java-format dependency stops with the message that names it. The plugin has version numbers of its own, so zensical.toml gets maven_plugin_version next to ojf_version, to bump on each plugin release. The overview gets a card and names that version. Spotless and Migrate now point Maven builds at the plugin instead of saying Maven has no way, and About lists the plugin among the Maven Central artifacts, with its own repository and release workflow.
1 parent 1627ba4 commit 6544a35

6 files changed

Lines changed: 86 additions & 5 deletions

File tree

‎docs/about.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ project exists is in the [manifesto](manifesto.md).
99

1010
| Where | What |
1111
| --- | --- |
12-
| [Maven Central](https://central.sonatype.com/namespace/dev.openjavaformat) | `dev.openjavaformat:open-java-format`, with `-spi`, `-native` and `-jdk-bootstrap` |
12+
| [Maven Central](https://central.sonatype.com/namespace/dev.openjavaformat) | `dev.openjavaformat:open-java-format`, with `-spi`, `-native` and `-jdk-bootstrap`, and the Maven plugin `dev.openjavaformat:fmt-maven-plugin` |
1313
| [Gradle Plugin Portal](https://plugins.gradle.org/plugin/dev.openjavaformat.java-format) | `dev.openjavaformat.java-format` |
1414
| [JetBrains Marketplace](https://plugins.jetbrains.com/plugin/34359-open-java-format) | the IntelliJ IDEA plugin |
1515
| [GitHub Releases](https://github.com/openjavaformat/open-java-format/releases/latest) | native binaries, the runnable jar, the Gradle, IntelliJ IDEA and Eclipse plugins |
1616

1717
The Maven Central artifacts, the Gradle plugins and the files of a GitHub release are built by the
1818
[release workflow](https://github.com/openjavaformat/open-java-format/blob/main/.github/workflows/release.yml)
19-
from the tag of the version.
19+
from the tag of the version. The Maven plugin has a repository and a release workflow of its own,
20+
[openjavaformat/fmt-maven-plugin](https://github.com/openjavaformat/fmt-maven-plugin).
2021

2122
## Verify a download
2223

‎docs/get-started/index.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ the same formatter, so they all produce the same files. Set up the ones your pro
66
!!! info "Requirements"
77

88
The current version is **{{ ojf_version }}**. Everything except the native binaries needs Java 21 or
9-
later, and the Gradle plugin needs Gradle 9.
9+
later, and the Gradle plugin needs Gradle 9. The Maven plugin has version numbers of its own; the
10+
current one is **{{ maven_plugin_version }}**.
1011

1112
<div class="grid cards" markdown>
1213

@@ -23,6 +24,12 @@ the same formatter, so they all produce the same files. Set up the ones your pro
2324

2425
Works with Gradle through our plugin. Maven waits for a pull request in Spotless.
2526

27+
- :simple-apachemaven:{ .lg .middle } __[Maven plugin](maven.md)__
28+
29+
---
30+
31+
Formats the sources in every build, or fails the build in CI when they are not formatted.
32+
2633
- :lucide-terminal:{ .lg .middle } __[Command line](command-line.md)__
2734

2835
---

‎docs/get-started/maven.md‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Maven plugin
2+
3+
The plugin formats the Java sources of a Maven build, or fails the build when they are not
4+
formatted. Maven has to run on Java 21 or later.
5+
6+
## Add the plugin
7+
8+
``` xml title="pom.xml"
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>dev.openjavaformat</groupId>
13+
<artifactId>fmt-maven-plugin</artifactId>
14+
<version>{{ maven_plugin_version }}</version>
15+
<executions>
16+
<execution>
17+
<goals>
18+
<goal>format</goal>
19+
</goals>
20+
</execution>
21+
</executions>
22+
<dependencies>
23+
<dependency>
24+
<groupId>dev.openjavaformat</groupId>
25+
<artifactId>open-java-format</artifactId>
26+
<version>{{ ojf_version }}</version>
27+
</dependency>
28+
</dependencies>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
```
33+
34+
The `format` goal runs in the `process-sources` phase, so `mvn compile`, `mvn test` and every later
35+
phase format `src/main/java` and `src/test/java` first.
36+
37+
The dependency is not optional. The plugin does not bring a formatter of its own: it runs the
38+
open-java-format version named there, and without it the build stops with a message that says what
39+
to add. Name the same version as in your IDE and your other builds.
40+
41+
## Check in CI
42+
43+
Use the `check` goal instead of `format`. It changes no file, and in the `verify` phase it fails the
44+
build when a file is not formatted:
45+
46+
``` text title="mvn verify"
47+
[ERROR] Found 1 non-complying files, failing build
48+
[ERROR] To fix formatting errors, run "mvn dev.openjavaformat:fmt-maven-plugin:format"
49+
```
50+
51+
## Run a goal from the command line
52+
53+
``` sh
54+
mvn dev.openjavaformat:fmt-maven-plugin:format
55+
mvn dev.openjavaformat:fmt-maven-plugin:check
56+
```
57+
58+
The plugin still has to be in the `pom.xml` with its dependency: Maven reads the dependencies of a
59+
plugin from the POM, never from the command line.
60+
61+
## Options
62+
63+
The plugin is a fork of [spotify/fmt-maven-plugin](https://github.com/spotify/fmt-maven-plugin) with
64+
open-java-format in place of google-java-format. Its options, such as extra source directories, file
65+
name patterns and skipping a directory, are listed in
66+
[its README](https://github.com/openjavaformat/fmt-maven-plugin#options). There is nothing to set for
67+
the formatting itself: imports are always sorted and cleaned up, and long strings are always
68+
reflowed, as on every other route.

‎docs/get-started/spotless.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME
6565

6666
The Maven plugin of Spotless has no step for open-java-format. Its `palantirJavaFormat` step always
6767
downloads `com.palantir.javaformat:palantir-java-format`, and it cannot be pointed at another
68-
artifact.
68+
artifact. Until that changes, a Maven build formats with our [Maven plugin](maven.md).
6969

7070
The pull request [diffplug/spotless#3084](https://github.com/diffplug/spotless/pull/3084) adds an
7171
`openJavaFormat` step to both Spotless plugins, for Gradle and for Maven:

‎docs/migrate.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ In a `pom.xml` it is the same change of `groupId`, `artifactId` and `version`.
9696
### Spotless
9797

9898
With Gradle, remove `palantirJavaFormat()` from the `spotless` block and apply our plugin, which adds
99-
its own step. Maven has no way yet. [Spotless](get-started/spotless.md) has both.
99+
its own step. With Maven, Spotless has no step for open-java-format yet: remove `palantirJavaFormat`
100+
and add our [Maven plugin](get-started/maven.md). [Spotless](get-started/spotless.md) has both.
100101

101102
### IntelliJ IDEA
102103

‎zensical.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ nav = [
2020
"get-started/index.md",
2121
{ "Gradle plugin" = "get-started/gradle.md" },
2222
{ "Spotless" = "get-started/spotless.md" },
23+
{ "Maven plugin" = "get-started/maven.md" },
2324
{ "Command line" = "get-started/command-line.md" },
2425
{ "IntelliJ IDEA" = "get-started/intellij-idea.md" },
2526
{ "Eclipse" = "get-started/eclipse.md" },
@@ -106,6 +107,9 @@ toggle.name = "Switch to system preference"
106107
# extra.version: the theme reads that one as the switch for a version selector.
107108
[project.extra]
108109
ojf_version = "2.98.0.4"
110+
# The Maven plugin is released from its own repository, openjavaformat/fmt-maven-plugin, with version
111+
# numbers of its own. Bump it on each release of the plugin.
112+
maven_plugin_version = "2.27.0.1"
109113

110114
# Renders every page as a Jinja template, for {{ ojf_version }}. A misspelt variable or a stray {{
111115
# fails the build instead of leaving a gap or the raw text on the site.

0 commit comments

Comments
 (0)