Skip to content

Commit e69ab4b

Browse files
committed
Add Maven support
1 parent 68e4217 commit e69ab4b

File tree

6 files changed

+100
-3
lines changed

6 files changed

+100
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ lib('java.ImportOrderStep') +'{{yes}} | {{yes}}
6161
lib('java.PalantirJavaFormatStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
6262
lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
6363
extra('java.EclipseJdtFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
64-
lib('java.TypeAnnotationsStep') +'{{yes}} | {{no}} | {{no}} | {{no}} |',
64+
lib('java.TypeAnnotationsStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
6565
lib('json.gson.GsonStep') +'{{yes}} | {{no}} | {{no}} | {{no}} |',
6666
lib('json.JsonSimpleStep') +'{{yes}} | {{no}} | {{no}} | {{no}} |',
6767
lib('kotlin.KtLintStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* `typeAnnotations` step to correct formatting of type annotations. It puts type annotations on the same line as the type that they qualify. Run it after a Java formattng step, such as `googleJavaFormat`. ([#1275](https://github.com/diffplug/spotless/pull/1275))
68

79
## [2.25.0] - 2022-08-23
810
### Added

plugin-maven/README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ output = [
2424
output = prefixDelimiterReplace(input, 'https://{{org}}.github.io/{{name}}/javadoc/spotless-plugin-maven/', '/', versionLast)
2525
-->
2626

27-
Spotless is a general-purpose formatting plugin. It is completely à la carte, but also includes powerful "batteries-included" if you opt-in. Plugin requires a version of Maven higher or equal to 3.1.0.
27+
Spotless is a general-purpose formatting plugin used by [4,000 projects on GitHub (August 2020)](https://github.com/search?l=gradle&q=spotless&type=Code). It is completely à la carte, but also includes powerful "batteries-included" if you opt-in. Plugin requires a version of Maven higher or equal to 3.1.0.
2828

2929
To people who use your build, it looks like this:
3030

@@ -47,7 +47,7 @@ user@machine repo % mvn spotless:check
4747
- [Requirements](#requirements)
4848
- [Binding to maven phase](#binding-to-maven-phase)
4949
- **Languages**
50-
- [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier), [palantir-java-format](#palantir-java-format))
50+
- [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier), [palantir-java-format](#palantir-java-format), [type annotations](#type-annotations))
5151
- [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy))
5252
- [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier))
5353
- [Scala](#scala) ([scalafmt](#scalafmt))
@@ -191,6 +191,8 @@ any other maven phase (i.e. compile) then it can be configured as below;
191191
<eclipse /> <!-- has its own section below -->
192192
<prettier /> <!-- has its own section below -->
193193

194+
<typeAnnotations /> <!-- fixes formatting of type annotations, see below -->
195+
194196
<licenseHeader>
195197
<content>/* (C)$YEAR */</content> <!-- or <file>${project.basedir}/license-header</file> -->
196198
</licenseHeader>
@@ -255,6 +257,33 @@ This is a workaround to a [pending issue](https://github.com/diffplug/spotless/i
255257
</eclipse>
256258
```
257259

260+
### Type annotations
261+
262+
Type annotations should be on the same line as the type that they qualify.
263+
264+
```java
265+
@Override
266+
@Deprecated
267+
@Nullable @Interned String s;
268+
```
269+
270+
However, some tools format them incorrectly, like this:
271+
272+
```java
273+
@Override
274+
@Deprecated
275+
@Nullable
276+
@Interned
277+
String s;
278+
```
279+
280+
To fix the incorrect formatting, add the `typeAnnotations()` rule after a Java formatter. For example:
281+
282+
```XML
283+
<googleJavaFormat />
284+
<typeAnnotations />
285+
```
286+
258287
## Groovy
259288

260289
[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy).

plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@ public void addPalantirJavaFormat(PalantirJavaFormat palantirJavaFormat) {
6161
public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) {
6262
addStepFactory(removeUnusedImports);
6363
}
64+
65+
public void addTypeAnnotations(TypeAnnotations typeAnnotations) {
66+
addStepFactory(typeAnnotations);
67+
}
6468
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.java;
17+
18+
import com.diffplug.spotless.FormatterStep;
19+
import com.diffplug.spotless.java.TypeAnnotationsStep;
20+
import com.diffplug.spotless.maven.FormatterStepConfig;
21+
import com.diffplug.spotless.maven.FormatterStepFactory;
22+
23+
public class TypeAnnotations implements FormatterStepFactory {
24+
25+
@Override
26+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
27+
return TypeAnnotationsStep.create();
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.java;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
21+
22+
class TypeAnnotationsStepTest extends MavenIntegrationHarness {
23+
24+
@Test
25+
void testRemoveUnusedInports() throws Exception {
26+
writePomWithJavaSteps("<typeAnnotations/>");
27+
28+
String path = "src/main/java/test.java";
29+
setFile(path).toResource("java/typeannotations/TypeAnnotationsTestInput.test");
30+
mavenRunner().withArguments("spotless:apply").runNoError();
31+
assertFile(path).sameAsResource("java/typeannotations/TypeAnnotationsTestOutput.test");
32+
}
33+
}

0 commit comments

Comments
 (0)