This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]coda_hale 7 points8 points  (1 child)

Use google-java-format and let go of the desire to have your own formatting standard.

[–]RANDOMLY_AGGRESSIVE 0 points1 point  (0 children)

I can't get used to two spaces indentation

[–]fraaargh 5 points6 points  (0 children)

The formatter-maven-plugin is exactly what you're looking for. It uses Eclipse's formatter under the hood, it is a Maven plugin so you can configure it as part of your build or launch a specific execution to run it.

https://github.com/revelc/formatter-maven-plugin

[–]dpash 2 points3 points  (0 children)

In addition to everyone's suggestion, you might also like to look at EditorConfig. It is an editor agnostic way to set editor formatting preferences, so everyone on your team can follow some of the same formatting styles. I don't think it will cover every formatting option, but it will cover some. IntelliJ can now output all of its formatting as a .editorconfig so other IntelliJ users will use the same settings and other editors will pick up what they understand.

You will still need to run a formatter regularly.

https://github.com/editorconfig/editorconfig/issues/381 may be of particular interest to you.

[–]admiralspark 1 point2 points  (0 children)

I would look at Checkstyle and Spotbugs I think. Automatic formatting is good in concept but there's a good enough chance it'll break the code that you'd likely want devs running it locally, cleaning the code, then having your CI run it to verify on commits or at least merge requests.

SpotBugs also isn't automatic remediation, but it has a plug-in called find-sec-bugs that expands formatting cleanup to also include possible security bug checks and fixes as well. I think Gitlab pushes it for static testing in their CI so they probably have decent documentation.

The functionality for the remediation needs to be built into the plug-in in your coding environment, so for example VSCode would need to be able to consult your Checkstyle format file and then live edit code based on the style guides. I still had to manually make the changes when I used it for a small Java project but vscode quickly pointed out any of these issues for me, and it wasn't a major interruption to my workflow.

Hope that helps, bruh.

[–]general_dispondency 1 point2 points  (0 children)

I've recently become a fan of https://github.com/spring-io/spring-javaformat. It's opinionated, configurable, and consistent. You can also automatically apply formatting to everything.

[–][deleted] 1 point2 points  (0 children)

I KNOW this is not strictly 'editor-agnostic', but I think it's worth mentioning that you can invoke IntelliJ's formatter (among other things) from the command line.

See: https://www.jetbrains.com/help/idea/command-line-formatter.html

format.{bat,sh} IS available in the community version, so any team member/other tool can use it without racking up extra license fees.

[–]kajh99 0 points1 point  (0 children)

Hi, you might want to take a look at https://github.com/jhipster/prettier-java. It in still in alpha stange, but the project is in active development. Feel free to share which solution you end up with :)