Skip to content

Latest commit

 

History

History
265 lines (213 loc) · 10.4 KB

File metadata and controls

265 lines (213 loc) · 10.4 KB
 
Feb 15, 2022
Feb 15, 2022
1
[![Build Status](https://github.com/spotify/fmt-maven-plugin/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/spotify/fmt-maven-plugin/actions/workflows/ci.yml?query=branch%3Amain)
2
[![license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/spotify/fmt-maven-plugin/blob/main/LICENSE)
Feb 21, 2022
Feb 21, 2022
3
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.spotify.fmt/fmt-maven-plugin/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.spotify.fmt/fmt-maven-plugin)
Nov 17, 2016
Nov 17, 2016
4
5
## fmt-maven-plugin
Mar 17, 2016
Mar 17, 2016
6
Feb 17, 2022
Feb 17, 2022
7
**UPDATE 2022-02-14:** This plugin has moved from [coveooss](https://github.com/coveooss/) to the [spotify](https://github.com/spotify/) Github org. The new groupId will be `com.spotify.fmt`, and the `master` branch has been renamed to `main`.
Feb 14, 2022
Feb 14, 2022
8
Mar 17, 2016
Mar 17, 2016
9
Formats your code using [google-java-format](https://github.com/google/google-java-format) which follows [Google's code styleguide](https://google.github.io/styleguide/javaguide.html).
Mar 17, 2016
Mar 17, 2016
10
11
The format cannot be configured by design.
12
Aug 6, 2020
Aug 6, 2020
13
If you want your IDE to stick to the same format, google-java-format also includes integrations for IntelliJ and Eclipse IDE's, following the installation instructions on the [README](https://github.com/google/google-java-format/blob/master/README.md#using-the-formatter).
Mar 17, 2016
Mar 17, 2016
14
Mar 17, 2016
Mar 17, 2016
15
## Usage
16
Mar 17, 2016
Mar 17, 2016
17
### Standard pom.xml
Mar 17, 2016
Mar 17, 2016
18
Jul 18, 2017
Jul 18, 2017
19
To have your sources automatically formatted on each build, add to your pom.xml:
Mar 17, 2016
Mar 17, 2016
20
Mar 21, 2016
Mar 21, 2016
21
```xml
Mar 17, 2016
Mar 17, 2016
22
<build>
23
<plugins>
24
<plugin>
Feb 15, 2022
Feb 15, 2022
25
<groupId>com.spotify.fmt</groupId>
Mar 17, 2016
Mar 17, 2016
26
<artifactId>fmt-maven-plugin</artifactId>
Feb 25, 2022
Feb 25, 2022
27
<version>VERSION</version>
Mar 17, 2016
Mar 17, 2016
28
<executions>
29
<execution>
30
<goals>
31
<goal>format</goal>
32
</goals>
33
</execution>
34
</executions>
Sep 11, 2025
Sep 11, 2025
35
<dependencies>
36
<dependency>
37
<groupId>com.google.googlejavaformat</groupId>
38
<artifactId>google-java-format</artifactId>
39
<version>1.27.0</version>
40
</dependency>
41
</dependencies>
Mar 17, 2016
Mar 17, 2016
42
</plugin>
43
</plugins>
44
</build>
Mar 17, 2016
Mar 17, 2016
45
```
46
Sep 11, 2025
Sep 11, 2025
47
The inclusion of the `google-java-format` as a dependency allows you to control that version,
48
which is generally recommended, so that you can align it with the exact version used in your
49
IDE, pre-commit Git hook, etc. (If you are fine getting "whatever version `fmt-maven-plugin`
50
uses" then you could omit it.)
51
Jul 18, 2017
Jul 18, 2017
52
If you prefer, you can only check formatting at build time using the `check` goal:
53
54
```xml
55
<build>
56
<plugins>
57
<plugin>
Feb 15, 2022
Feb 15, 2022
58
<groupId>com.spotify.fmt</groupId>
Jul 18, 2017
Jul 18, 2017
59
<artifactId>fmt-maven-plugin</artifactId>
Feb 25, 2022
Feb 25, 2022
60
<version>VERSION</version>
Jul 18, 2017
Jul 18, 2017
61
<executions>
62
<execution>
63
<goals>
64
<goal>check</goal>
65
</goals>
66
</execution>
67
</executions>
68
</plugin>
69
</plugins>
70
</build>
71
```
72
Feb 21, 2022
Feb 21, 2022
73
#### Overriding the Default Lifecycle Phase
74
75
Both goals have a [Maven lifecycle phase](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#lifecycle-reference) configured by default.
76
77
| Goal | Default Phase |
78
|-----------|-------------------|
79
| `format` | `process-sources` |
80
| `check` | `verify` |
81
82
You may prefer to run these goals in a different phase instead.
83
Maven allows you to override the default phase by specifying a `<phase>` for the `<execution>`.
84
85
For example, you may prefer that the `check` goal is performed in an earlier phase such as `validate`:
86
87
```xml
88
<execution>
89
<phase>validate</phase>
90
<goals>
91
<goal>check</goal>
92
</goals>
93
</execution>
94
```
95
Mar 17, 2016
Mar 17, 2016
96
### Options
Mar 17, 2016
Mar 17, 2016
97
Mar 17, 2016
Mar 17, 2016
98
`sourceDirectory` represents the directory where your Java sources that need to be formatted are contained. It defaults to `${project.build.sourceDirectory}`
99
100
`testSourceDirectory` represents the directory where your test's Java sources that need to be formatted are contained. It defaults to `${project.build.testSourceDirectory}`
101
102
`additionalSourceDirectories` represents a list of additional directories that contains Java sources that need to be formatted. It defaults to an empty list.
103
104
`verbose` is whether the plugin should print a line for every file that is being formatted. It defaults to `false`.
105
Nov 23, 2016
Nov 23, 2016
106
`filesNamePattern` represents the pattern that filters files to format. The defaults value is set to `.*\.java`.
107
Feb 22, 2018
Feb 22, 2018
108
`skip` is whether the plugin should skip the operation.
109
Sep 22, 2025
Sep 22, 2025
110
`skipReflowingLongStrings` is whether the plugin should skip reflowing long strings. It defaults to `true`.
Sep 17, 2025
Sep 17, 2025
111
Sep 22, 2025
Sep 22, 2025
112
`skipRemovingUnusedImports` is whether the plugin should skip removing unused imports. It defaults to `false`.
Sep 17, 2025
Sep 17, 2025
113
Oct 18, 2018
Oct 18, 2018
114
`skipSortingImports` is whether the plugin should skip sorting imports.
115
Sep 30, 2022
Sep 30, 2022
116
`skipSourceDirectory` is whether the plugin should skip formatting/checking the `sourceDirectory`. It defaults to `false`.
117
118
`skipTestSourceDirectory` is whether the plugin should skip formatting/checking the `testSourceDirectory`. It defaults to `false`.
119
Mar 14, 2022
Mar 14, 2022
120
`style` sets the formatter style to be `google` or `aosp`. By default this is `google`. Projects using Android conventions may prefer `aosp`.
121
Mar 29, 2022
Mar 29, 2022
122
`forkMode` lets you specify whether to run google-java-format in a fork or in-process. Also adds JVM arguments to expose JDK internal javac APIs. Value `default` (which is the default) will fork (to avoid warnings for JDK9+ and to be able to run at all for JDK16+), `never` runs in-process, regardless of JDK version and `always` will always fork.
Feb 20, 2018
Feb 20, 2018
123
Mar 17, 2016
Mar 17, 2016
124
example:
125
```xml
126
<build>
127
<plugins>
128
<plugin>
Feb 15, 2022
Feb 15, 2022
129
<groupId>com.spotify.fmt</groupId>
Mar 17, 2016
Mar 17, 2016
130
<artifactId>fmt-maven-plugin</artifactId>
Feb 25, 2022
Feb 25, 2022
131
<version>VERSION</version>
Mar 17, 2016
Mar 17, 2016
132
<configuration>
133
<sourceDirectory>some/source/directory</sourceDirectory>
134
<testSourceDirectory>some/test/directory</testSourceDirectory>
135
<verbose>true</verbose>
Nov 23, 2016
Nov 23, 2016
136
<filesNamePattern>.*\.java</filesNamePattern>
Mar 17, 2016
Mar 17, 2016
137
<additionalSourceDirectories>
138
<param>some/dir</param>
139
<param>some/other/dir</param>
140
</additionalSourceDirectories>
Feb 22, 2018
Feb 22, 2018
141
<skip>false</skip>
Sep 30, 2022
Sep 30, 2022
142
<skipSourceDirectory>false</skipSourceDirectory>
143
<skipTestSourceDirectory>false</skipTestSourceDirectory>
Oct 18, 2018
Oct 18, 2018
144
<skipSortingImports>false</skipSortingImports>
Sep 22, 2025
Sep 22, 2025
145
<skipRemovingUnusedImports>false</skipRemovingUnusedImports>
146
<skipReflowingLongStrings>true</skipReflowingLongStrings>
Feb 20, 2018
Feb 20, 2018
147
<style>google</style>
Mar 17, 2016
Mar 17, 2016
148
</configuration>
149
<executions>
150
<execution>
151
<goals>
152
<goal>format</goal>
153
</goals>
154
</execution>
155
</executions>
156
</plugin>
157
</plugins>
158
</build>
Mar 17, 2016
Mar 17, 2016
159
```
160
Sep 15, 2017
Sep 15, 2017
161
162
163
### check Options
164
Jun 14, 2022
Jun 14, 2022
165
`displayFiles` default = true. Display the list of the files that are not compliant.
Sep 15, 2017
Sep 15, 2017
166
Jun 14, 2022
Jun 14, 2022
167
`displayLimit` default = 100. Number of files to display that are not compliant.
168
169
`failOnError` default = true. Fail the build if non-compliant files are found.
Sep 15, 2017
Sep 15, 2017
170
Feb 20, 2018
Feb 20, 2018
171
Sep 15, 2017
Sep 15, 2017
172
example to not display the non-compliant files:
173
```xml
174
<build>
175
<plugins>
176
<plugin>
Feb 15, 2022
Feb 15, 2022
177
<groupId>com.spotify.fmt</groupId>
Sep 15, 2017
Sep 15, 2017
178
<artifactId>fmt-maven-plugin</artifactId>
Feb 25, 2022
Feb 25, 2022
179
<version>VERSION</version>
Sep 15, 2017
Sep 15, 2017
180
<configuration>
181
<displayFiles>false</displayFiles>
182
</configuration>
183
<executions>
184
<execution>
185
<goals>
186
<goal>check</goal>
187
</goals>
188
</execution>
189
</executions>
190
</plugin>
191
</plugins>
192
</build>
193
```
194
195
example to limit the display up to 10 files
196
```xml
197
<build>
198
<plugins>
199
<plugin>
Feb 15, 2022
Feb 15, 2022
200
<groupId>com.spotify.fmt</groupId>
Sep 15, 2017
Sep 15, 2017
201
<artifactId>fmt-maven-plugin</artifactId>
Feb 25, 2022
Feb 25, 2022
202
<version>VERSION</version>
Sep 15, 2017
Sep 15, 2017
203
<configuration>
204
<displayLimit>10</displayLimit>
205
</configuration>
206
<executions>
207
<execution>
208
<goals>
209
<goal>check</goal>
210
</goals>
211
</execution>
212
</executions>
213
</plugin>
214
</plugins>
215
</build>
216
```
217
Jun 14, 2022
Jun 14, 2022
218
example to only warn about non-compliant files instead of failing the build
219
```xml
220
<build>
221
<plugins>
222
<plugin>
223
<groupId>com.spotify.fmt</groupId>
224
<artifactId>fmt-maven-plugin</artifactId>
225
<version>VERSION</version>
226
<configuration>
227
<failOnError>false</failOnError>
228
</configuration>
229
<executions>
230
<execution>
231
<goals>
232
<goal>check</goal>
233
</goals>
234
</execution>
235
</executions>
236
</plugin>
237
</plugins>
238
</build>
239
```
240
Mar 17, 2016
Mar 17, 2016
241
### Command line
242
243
You can also use it on the command line
244
Feb 15, 2022
Feb 15, 2022
245
`mvn com.spotify.fmt:fmt-maven-plugin:format`
Mar 17, 2016
Mar 17, 2016
246
247
You can pass parameters via standard `-D` syntax.
Feb 15, 2022
Feb 15, 2022
248
`mvn com.spotify.fmt:fmt-maven-plugin:format -Dverbose=true`
Mar 17, 2016
Mar 17, 2016
249
Feb 22, 2018
Feb 22, 2018
250
`-Dfmt.skip` is whether the plugin should skip the operation.
251
Jun 4, 2020
Jun 4, 2020
252
### Using with Java 8
253
254
Starting from version 1.8, Google Java Formatter requires Java 11 to run. Incidently, all versions of this plugin starting from 2.10 inclusively also require this Java version to properly function. The 2.9.x release branch is the most up-to-date version that still runs on Java 8.
Mar 17, 2016
Mar 17, 2016
255
256
### Deploy
257
Mar 31, 2022
Mar 31, 2022
258
- `git checkout main && git pull`
259
- `mvn release:prepare` - use x.y format for release version and x.y.z for SCM tag. (You can only do this as admin of the repo)
260
- `mvn release:perform -P release` (make sure to use Maven settings which include credentials for the Sonatype staging repo)
261
- `git fetch` - to make sure your local repo is up to date with the commits from the release plugin.
Feb 24, 2022
Feb 24, 2022
262
- Create a GitHub release with merged PRs and other information.
Mar 31, 2022
Mar 31, 2022
263
- Check that the release is available in [Sonatype staging](https://oss.sonatype.org/#nexus-search;quick~com.spotify.fmt)
Sep 19, 2022
Sep 19, 2022
264
- Wait for the release to be available in [Maven Central](https://repo1.maven.org/maven2/com/spotify/fmt/fmt-maven-plugin/)
Mar 31, 2022
Mar 31, 2022
265
- Update version used for actual formatting in the POM.