-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathREADME.md
More file actions
265 lines (213 loc) · 10.4 KB
/
README.md
File metadata and controls
265 lines (213 loc) · 10.4 KB
Edit and raw actions
OlderNewer
1
[](https://github.com/spotify/fmt-maven-plugin/actions/workflows/ci.yml?query=branch%3Amain)
2
[](https://github.com/spotify/fmt-maven-plugin/blob/main/LICENSE)
3
[](https://maven-badges.herokuapp.com/maven-central/com.spotify.fmt/fmt-maven-plugin)
4
5
## fmt-maven-plugin
6
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`.
8
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).
10
11
The format cannot be configured by design.
12
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).
14
15
## Usage
16
17
### Standard pom.xml
18
19
To have your sources automatically formatted on each build, add to your pom.xml:
20
21
```xml
22
<build>
23
<plugins>
24
<plugin>
25
<groupId>com.spotify.fmt</groupId>
26
<artifactId>fmt-maven-plugin</artifactId>
27
<version>VERSION</version>
28
<executions>
29
<execution>
30
<goals>
31
<goal>format</goal>
32
</goals>
33
</execution>
34
</executions>
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>
42
</plugin>
43
</plugins>
44
</build>
45
```
46
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
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>
58
<groupId>com.spotify.fmt</groupId>
59
<artifactId>fmt-maven-plugin</artifactId>
60
<version>VERSION</version>
61
<executions>
62
<execution>
63
<goals>
64
<goal>check</goal>
65
</goals>
66
</execution>
67
</executions>
68
</plugin>
69
</plugins>
70
</build>
71
```
72
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
96
### Options
97
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
106
`filesNamePattern` represents the pattern that filters files to format. The defaults value is set to `.*\.java`.
107
108
`skip` is whether the plugin should skip the operation.
109
110
`skipReflowingLongStrings` is whether the plugin should skip reflowing long strings. It defaults to `true`.
111
112
`skipRemovingUnusedImports` is whether the plugin should skip removing unused imports. It defaults to `false`.
113
114
`skipSortingImports` is whether the plugin should skip sorting imports.
115
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
120
`style` sets the formatter style to be `google` or `aosp`. By default this is `google`. Projects using Android conventions may prefer `aosp`.
121
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.
123
124
example:
125
```xml
126
<build>
127
<plugins>
128
<plugin>
129
<groupId>com.spotify.fmt</groupId>
130
<artifactId>fmt-maven-plugin</artifactId>
131
<version>VERSION</version>
132
<configuration>
133
<sourceDirectory>some/source/directory</sourceDirectory>
134
<testSourceDirectory>some/test/directory</testSourceDirectory>
135
<verbose>true</verbose>
136
<filesNamePattern>.*\.java</filesNamePattern>
137
<additionalSourceDirectories>
138
<param>some/dir</param>
139
<param>some/other/dir</param>
140
</additionalSourceDirectories>
141
<skip>false</skip>
142
<skipSourceDirectory>false</skipSourceDirectory>
143
<skipTestSourceDirectory>false</skipTestSourceDirectory>
144
<skipSortingImports>false</skipSortingImports>
145
<skipRemovingUnusedImports>false</skipRemovingUnusedImports>
146
<skipReflowingLongStrings>true</skipReflowingLongStrings>
147
<style>google</style>
148
</configuration>
149
<executions>
150
<execution>
151
<goals>
152
<goal>format</goal>
153
</goals>
154
</execution>
155
</executions>
156
</plugin>
157
</plugins>
158
</build>
159
```
160
161
162
163
### check Options
164
165
`displayFiles` default = true. Display the list of the files that are not compliant.
166
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.
170
171
172
example to not display the non-compliant files:
173
```xml
174
<build>
175
<plugins>
176
<plugin>
177
<groupId>com.spotify.fmt</groupId>
178
<artifactId>fmt-maven-plugin</artifactId>
179
<version>VERSION</version>
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>
200
<groupId>com.spotify.fmt</groupId>
201
<artifactId>fmt-maven-plugin</artifactId>
202
<version>VERSION</version>
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
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
241
### Command line
242
243
You can also use it on the command line
244
245
`mvn com.spotify.fmt:fmt-maven-plugin:format`
246
247
You can pass parameters via standard `-D` syntax.
248
`mvn com.spotify.fmt:fmt-maven-plugin:format -Dverbose=true`
249
250
`-Dfmt.skip` is whether the plugin should skip the operation.
251
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.
255
256
### Deploy
257
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.
262
- Create a GitHub release with merged PRs and other information.
263
- Check that the release is available in [Sonatype staging](https://oss.sonatype.org/#nexus-search;quick~com.spotify.fmt)
264
- Wait for the release to be available in [Maven Central](https://repo1.maven.org/maven2/com/spotify/fmt/fmt-maven-plugin/)
265
- Update version used for actual formatting in the POM.