Introducing jbang-fmt
I really did not want to do this, but after I and others tried to convince google-java-format one too many times to not mess with JBang directives like //DEPS or the ///usr/bin/env lines, I finally gave up and made my own.
While I’m a fan of tools like Spotless, their command-line usage is still too cumbersome. This prompted me to create an improved solution called jbang-fmt, in the hope that its key features will be adopted by competing programs and ultimately render itself obsolete
So here it is: https://github.com/jbangdev/jbang-fmt
I would have called it java-fmt as it is not tied to JBang - you can use it as-is with any Java code; but I don’t like visits from lawyers.

Why jbang-fmt?
-
Simple: One command to format any Java file or directory
-
Fast: Built for command-line usage and using virtual threads
-
Flexible: All the options you need (but probably shouldn’t use)
-
JBang-aware: Respects
//DEPSand///usr/bin/envdirectives
It is a simple tool that formats Java code using Eclipse Formatter![1] but by default does not mess with JBang directives like //DEPS or the ///usr/bin/env lines.
In its simplest form you just do:
jbang-fmt .
And it will format all the Java files recursively in the current directory.
If you want to format specific set of files and directories you can do:
jbang-fmt myscript.java src/main
That’s it - you don’t need to read more, just install it and use it.
Installation
jbang app install jbang-fmt@jbangdev
One more thing…
Since I made this in like 30 minutes as I was being nerd sniped I couldn’t resist adding a few more bell and whistles.
Before I show them - please know - that I really highly recommend you use one format in your project and in many ways I fully agree with google-java-format faq on locking down the formatting rules.
That said - the world is a messy place and we all have different needs - so I like to make formatting easy to do and enable easy experimentation - so I added all the ways I can think of a simple formatting tool could be useful.
--check
This is a really simple flag that will check if the code is formatted without making any changes.
jbang-fmt --check .
If the code is not formatted it will exit with a non-zero code. Useful in CI pipelines to fail the build if the code is not formatted and as git pre-commit hooks - see Git Integration for an example of the latter.
Maven and Gradle integration
Shortcuts for common "debates"
-
Tabs vs spaces? Use
--indent-with=tabor--indent-with=space -
Big vs small monitor? Use
--line-length=120or--line-length=40 -
Java version nostalgia? Use
--java-version=4for the good old days or--java-version=25for living on the edge -
Compact docs? Use
--indent-size=2etc.
…and if you want full control you can use -Skey=val to override any Eclipse formatter property.
But again, remember I told you to just use one format - above is not meant to be used unless you are ready for war.
In addition it also supports using @-files in case you ignored my advice and want to go crazy on shared formatting.
Conclusion
So - what are you waiting for? Install it and start formatting your code today!
And if you have any feedback, suggestions, or want to contribute, please open an issue on GitHub!
Comments