Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
How it works
A Prettier plugin must first parse the source code of the target language into a traversable data structure (Usually an Abstract Syntax Tree) and then print out that data structure in a "pretty" style.
What this means is that unlike many other Prettier plugins,
prettier-java has no additional runtime pre-requisites (e.g: Python executable).
It could even be used inside a browser.
This project contains 2 packages:
Install Prettier and Prettier-Java plugin
# Local installation npm install prettier-plugin-java --save-dev # Or globally npm install -g prettier prettier-plugin-java
or with yarn:
# Local installation yarn add prettier-plugin-java --dev # Or globally yarn global add prettier prettier-plugin-java
Note: If you want to install the prettier-plugin-java globally, you should also install the prettier package globally.
To reformat all your Java files, run:
# If you have installed the package locally npx prettier --write "**/*.java" # Or globally prettier --write "**/*.java"
To see an advanced usage: please go to the Advanced Usage section
A neat maven plugin for prettier-java was made by developers from HubSpot.
Add it to the
plugins section of your
<build> <plugins> <plugin> <groupId>com.hubspot.maven.plugins</groupId> <artifactId>prettier-maven-plugin</artifactId> <!-- Find the latest version at https://github.com/jhipster/prettier-java/releases --> <version>0.8</version> </plugin> </plugins> </build>
If you would like to use this plugin, we recommend you to check their project as is it well documented.
Prettier-java is currently sorting imports according to the Google Java Style guide.
If you are using an IDE such as IntelliJ, you might want to configure it to match with Prettier-java.
You can also import the Checkstyle configuration provided in the next section.
You can use Prettier in combination with other linter, like Checkstyle.
Here is one Checkstyle Prettier compatible configuration you can use to start with !
You can directly import the config into IntelliJ Idea for instance:
Special thanks to @thorbenvh8 for creating the original