Introducing jbang-fmt

4 minute read

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.

Tools like Spotless (which I love!) are still a bit too clunky to use on the command line, forcing my hand to move - all in hope this will be adopted by others and make itself irrelevant.

jbang fmt

Why jbang-fmt?

  • Simple: One command to format any Java file or directory

  • Fast: Built for command-line usage

  • Flexible: All the options you need (but probably shouldn’t use)

  • JBang-aware: Respects //DEPS and ///usr/bin/env directives

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 ci.yml for an example.

Maven and Gradle integration

You can use it from Maven and Gradle using their jbang support - no additional plugins needed. See Maven and Gradle in documentation for more details.

Shortcuts for common "debates"

  • Tabs vs spaces? Use --indent-with=tab or --indent-with=space

  • Big vs small monitor? Use --line-length=120 or --line-length=40

  • Java version nostalgia? Use --java-version=4 for the good old days or --java-version=25 for living on the edge

  • Compact docs? Use --indent-size=2 etc.

…​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!


1. I wanted to offer both Google Java Format and Eclipse but Google Java Format even on API level seems to have no consistent ability to ignore lines.

Tags

Comments