Skip to content

Commit 4e30602

Browse files
authored
Customize artifact of google java format (#944)
2 parents 1a9d710 + c478183 commit 4e30602

File tree

9 files changed

+79
-8
lines changed

9 files changed

+79
-8
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Changed
1414
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-cdt`, `eclipse-jdt`, `eclipse-wtp`. Change is only applied for JVM 11+.
15+
* Added `groupArtifact` option for `google-java-format` ([#944](https://github.com/diffplug/spotless/pull/944))
1516

1617
## [2.16.1] - 2021-09-20
1718
### Changed

lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private GoogleJavaFormatStep() {}
3636
private static final String DEFAULT_STYLE = "GOOGLE";
3737
private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false;
3838
static final String NAME = "google-java-format";
39-
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format:";
39+
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
4040
static final String FORMATTER_CLASS = "com.google.googlejavaformat.java.Formatter";
4141
static final String FORMATTER_METHOD = "formatSource";
4242

@@ -76,16 +76,29 @@ public static FormatterStep create(String version, String style, Provisioner pro
7676

7777
/** Creates a step which formats everything - code, import order, and unused imports - and optionally reflows long strings. */
7878
public static FormatterStep create(String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
79+
return create(MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
80+
}
81+
82+
/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
83+
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
84+
Objects.requireNonNull(groupArtifact, "groupArtifact");
85+
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) {
86+
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'");
87+
}
7988
Objects.requireNonNull(version, "version");
8089
Objects.requireNonNull(style, "style");
8190
Objects.requireNonNull(provisioner, "provisioner");
8291
return FormatterStep.createLazy(NAME,
83-
() -> new State(NAME, version, style, provisioner, reflowLongStrings),
92+
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings),
8493
State::createFormat);
8594
}
8695

8796
static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.7").add(11, "1.11.0");
8897

98+
public static String defaultGroupArtifact() {
99+
return MAVEN_COORDINATE;
100+
}
101+
89102
/** Get default formatter version */
90103
public static String defaultVersion() {
91104
return JVM_SUPPORT.getRecommendedFormatterVersion();
@@ -118,8 +131,12 @@ static final class State implements Serializable {
118131
}
119132

120133
State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
134+
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
135+
}
136+
137+
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
121138
JVM_SUPPORT.assertFormatterSupported(version);
122-
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
139+
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
123140
this.stepName = stepName;
124141
this.version = version;
125142
this.style = style;

plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Changed
77
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-cdt`, `eclipse-jdt`, `eclipse-wtp`. Change is only applied for JVM 11+.
8+
* Added `groupArtifact` option for `google-java-format` ([#944](https://github.com/diffplug/spotless/pull/944))
89

910
## [5.15.1] - 2021-09-20
1011
### Changed

plugin-gradle/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,10 @@ spotless {
180180
spotless {
181181
java {
182182
googleJavaFormat()
183-
// optional: you can specify a specific version and/or switch to AOSP style and/or reflow long strings (requires at least 1.8)
184-
//
185-
googleJavaFormat('1.8').aosp().reflowLongStrings()
183+
// optional: you can specify a specific version and/or switch to AOSP style
184+
// and/or reflow long strings (requires at least 1.8)
185+
// and/or use custom group artifact (you probably don't need this)
186+
googleJavaFormat('1.8').aosp().reflowLongStrings().groupArtifact('com.google.googlejavaformat:google-java-format')
186187
```
187188

188189
### eclipse jdt

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,23 @@ public GoogleJavaFormatConfig googleJavaFormat(String version) {
8989

9090
public class GoogleJavaFormatConfig {
9191
final String version;
92+
String groupArtifact;
9293
String style;
9394
boolean reflowLongStrings;
9495

9596
GoogleJavaFormatConfig(String version) {
9697
this.version = Objects.requireNonNull(version);
98+
this.groupArtifact = GoogleJavaFormatStep.defaultGroupArtifact();
9799
this.style = GoogleJavaFormatStep.defaultStyle();
98100
addStep(createStep());
99101
}
100102

103+
public GoogleJavaFormatConfig groupArtifact(String groupArtifact) {
104+
this.groupArtifact = Objects.requireNonNull(groupArtifact);
105+
replaceStep(createStep());
106+
return this;
107+
}
108+
101109
public GoogleJavaFormatConfig style(String style) {
102110
this.style = Objects.requireNonNull(style);
103111
replaceStep(createStep());
@@ -119,7 +127,9 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) {
119127
}
120128

121129
private FormatterStep createStep() {
122-
return GoogleJavaFormatStep.create(version,
130+
return GoogleJavaFormatStep.create(
131+
groupArtifact,
132+
version,
123133
style,
124134
provisioner(),
125135
reflowLongStrings);

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Changed
77
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-cdt`, `eclipse-jdt`, `eclipse-wtp`. Change is only applied for JVM 11+.
8+
* Added `groupArtifact` option for `google-java-format` ([#944](https://github.com/diffplug/spotless/pull/944))
89

910
## [2.13.1] - 2021-09-20
1011
### Changed

plugin-maven/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ any other maven phase (i.e. compile) then it can be configured as below;
205205
<version>1.8</version> <!-- optional -->
206206
<style>GOOGLE</style> <!-- or AOSP (optional) -->
207207
<reflowLongStrings>true</reflowLongStrings> <!-- optional (requires at least 1.8) -->
208+
<!-- optional: custom group artifact (you probably don't need this) -->
209+
<groupArtifact>com.google.googlejavaformat:google-java-format</groupArtifact>
208210
</googleJavaFormat>
209211
```
210212

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import com.diffplug.spotless.maven.FormatterStepFactory;
2424

2525
public class GoogleJavaFormat implements FormatterStepFactory {
26+
@Parameter
27+
private String groupArtifact;
28+
2629
@Parameter
2730
private String version;
2831

@@ -34,9 +37,10 @@ public class GoogleJavaFormat implements FormatterStepFactory {
3437

3538
@Override
3639
public FormatterStep newFormatterStep(FormatterStepConfig config) {
40+
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : GoogleJavaFormatStep.defaultGroupArtifact();
3741
String version = this.version != null ? this.version : GoogleJavaFormatStep.defaultVersion();
3842
String style = this.style != null ? this.style : GoogleJavaFormatStep.defaultStyle();
3943
boolean reflowLongStrings = this.reflowLongStrings != null ? this.reflowLongStrings : GoogleJavaFormatStep.defaultReflowLongStrings();
40-
return GoogleJavaFormatStep.create(version, style, config.getProvisioner(), reflowLongStrings);
44+
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings);
4145
}
4246
}

testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ void behaviorWithReflowLongStrings() throws Exception {
8383
}
8484
}
8585

86+
@Test
87+
void behaviorWithCustomGroupArtifact() throws Exception {
88+
FormatterStep step = GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), "1.2", GoogleJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral(), false);
89+
StepHarness.forStep(step)
90+
.testResource("java/googlejavaformat/JavaCodeUnformatted.test", "java/googlejavaformat/JavaCodeFormatted.test")
91+
.testResource("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test", "java/googlejavaformat/JavaCodeWithLicenseFormatted.test")
92+
.testResource("java/googlejavaformat/JavaCodeWithLicensePackageUnformatted.test", "java/googlejavaformat/JavaCodeWithLicensePackageFormatted.test")
93+
.testResource("java/googlejavaformat/JavaCodeWithPackageUnformatted.test", "java/googlejavaformat/JavaCodeWithPackageFormatted.test");
94+
}
95+
8696
@Test
8797
void equality() throws Exception {
8898
new SerializableEqualityTester() {
@@ -113,6 +123,30 @@ protected FormatterStep create() {
113123
}.testEquals();
114124
}
115125

126+
@Test
127+
void equalityGroupArtifact() throws Exception {
128+
new SerializableEqualityTester() {
129+
String groupArtifact = GoogleJavaFormatStep.defaultGroupArtifact();
130+
String version = "1.11.0";
131+
String style = "";
132+
boolean reflowLongStrings = false;
133+
134+
@Override
135+
protected void setupTest(API api) {
136+
// same version == same
137+
api.areDifferentThan();
138+
// change the groupArtifact, and it's different
139+
groupArtifact = "io.opil:google-java-format";
140+
api.areDifferentThan();
141+
}
142+
143+
@Override
144+
protected FormatterStep create() {
145+
return GoogleJavaFormatStep.create(groupArtifact, version, style, TestProvisioner.mavenCentral(), reflowLongStrings);
146+
}
147+
}.testEquals();
148+
}
149+
116150
@Test
117151
void fixWindowsBugForGfj1Point1() {
118152
fixWindowsBugTestcase("");

0 commit comments

Comments
 (0)