view content/Eclipse/formatting-sources.md @ 110:be0331916375

README
author Dirk Olmes <dirk.olmes@codedo.de>
date Fri, 18 Jun 2021 07:24:40 +0200
parents e19bc3042ec9
children
line wrap: on
line source

Title: Formatting Java sources with Eclipse
Date: 2017-08-04
Lang: en
Status: draft

All books on good code style suggest formatting your sources consistently. I found that it does not matter too much if you're in the *curly braces at the end of the line* or in the *curly braces on a new line* camp. Clean code is readable in any formatting.

At [exentra](http://www.exentra.de) we follow this rule and since we all use Eclipse it's fairly easy: configure Eclipse's formatter, have a save action format all code on save and the basic source formatting is covered.

But what if you don't want or cannot use Eclipse? There are stand alone code formatters out there but why learn another tool when Eclipse already does the job fairly well? It would be nice if Eclipse's code formatter could be run from the command line. As [it turns out](http://www.peterfriese.de/formatting-your-code-using-the-eclipse-code-formatter/) it's possible. And it's even [documented in the Eclipse help](https://help.eclipse.org/oxygen/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-231.htm).

The approaches I have cited above only work on a desktop machine, though as they require a full Eclipse installation. But what if you want your code formatted on e.g. a CI machine? There must be a way to strip down the dependencies to run the source formatter to a bare minimum. It turns out that [someone took up the task already](http://franke.ms/#/eclipse-formatter-commandline.wiki). Stefan's latest update is based on Eclipse Neon but I want one based on Eclipse Oxygen of course.