Skip to content

Add sbt-java-formatter-add-opens plugin#261

Merged
mkurz merged 1 commit intosbt:mainfrom
mkurz:sbt-java-formatter-add-opens
Apr 14, 2026
Merged

Add sbt-java-formatter-add-opens plugin#261
mkurz merged 1 commit intosbt:mainfrom
mkurz:sbt-java-formatter-add-opens

Conversation

@mkurz
Copy link
Copy Markdown
Member

@mkurz mkurz commented Apr 14, 2026

Just take a look at the change README. Call me crazy, but I like that idea.

  • Adds alias for the javafmt[Check][All] commands
  • Launches a new sbt <javafmt_command> session and adds the required -add-opens flags

From a user perspective, the commands now just work again on Java 17+. No need to set flags.

- Adds alias for the `javafmt[Check][All]` commands
- Launches a new `sbt <javafmt_command>` session and adds the required `-add-opens` flags

From a user perspective, the commands now just work again on Java 17+. No need to set flags.
@mkurz
Copy link
Copy Markdown
Member Author

mkurz commented Apr 14, 2026

So to make the sbt java formatter plugin work out of the box on Java 17+, in your project you just have to change

-addSbtPlugin("com.github.sbt" % "sbt-java-formatter"           % "0.11.1")
+addSbtPlugin("com.github.sbt" % "sbt-java-formatter-add-opens" % "0.11.1")

Magic 🪄

@mkurz
Copy link
Copy Markdown
Member Author

mkurz commented Apr 14, 2026

Tested locally with sbt 1 and sbt2. --server is required for sbt2 otherwise client mode is default and no new server/jvm will be started which of course would not add any flags.

@mkurz
Copy link
Copy Markdown
Member Author

mkurz commented Apr 14, 2026

I will merge that and cut a milestone.
In worst case we can revert it anyway (which I doubt).

@mkurz mkurz merged commit 1135ef8 into sbt:main Apr 14, 2026
2 checks passed
@mkurz mkurz deleted the sbt-java-formatter-add-opens branch April 14, 2026 19:21
@mkurz
Copy link
Copy Markdown
Member Author

mkurz commented Apr 15, 2026

This solution is not really great.
It will not handle dependsOn(Test / javafmt), like in sbt.

  Compile / unmanagedSources / inputFileStamps :=
    (Compile / unmanagedSources / inputFileStamps).dependsOn(Compile / javafmt).value,
  Test / unmanagedSources / inputFileStamps :=
    (Test / unmanagedSources / inputFileStamps).dependsOn(Test / javafmt).value,

I found a better approach and reverted this again in

The new implementation fixes the underlying problem in the main plugin by launching google-java-format in its own JVM with the flags it needs, instead of wrapping javafmt commands in a separate sbt session.

That means the workaround is now transparent no matter how javafmt gets invoked, and it also fixes the javafmtOnCompile case that this approach could not solve reliably.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant