Skip to content

Commit e7c2206

Browse files
committed
feat: allow dynamic version selection for gjf
1 parent 20eb619 commit e7c2206

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

README.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -436,16 +436,6 @@ spotless --target '**/src/**/*.java' format-annotations \
436436

437437
### google-java-format
438438

439-
<!---freshmark gjfshields
440-
output = [
441-
link(shield('Google Java Format version', 'google-java-format', '{{libs.versions.native.include.googleJavaFormat}}', 'blue'), 'https://github.com/google/google-java-format'),
442-
].join('\n')
443-
-->
444-
445-
[![Google Java Format version](https://img.shields.io/badge/google--java--format-1.27.0-blue.svg)](https://github.com/google/google-java-format)
446-
447-
<!---freshmark /gjfshields -->
448-
449439
Formats Java files according to the [google-java-format](https://github.com/google/google-java-format) style guide.
450440

451441
To see usage instructions for the google-java-format formatter, run: `spotless google-java-format --help`
@@ -458,7 +448,7 @@ output =
458448
-->
459449

460450
```
461-
Usage: spotless google-java-format [-hijrV] [-s=<style>]
451+
Usage: spotless google-java-format [-hijrV] [-s=<style>] [-v=<useVersion>]
462452
Runs google java format
463453
-h, --help Show this help message and exit.
464454
-i, --reorder-imports Reorder imports.
@@ -471,6 +461,10 @@ Runs google java format
471461
-s, --style=<style> The style to use for the google java format.
472462
One of: AOSP, GOOGLE
473463
(default: GOOGLE)
464+
-v, --use-version=<useVersion>
465+
The version of google java format to use. Must be >=
466+
1.8.
467+
(default: 1.28.0)
474468
-V, --version Print version information and exit.
475469
476470
✅ This step supports the following file type: Java

app/src/main/java/com/diffplug/spotless/cli/steps/GoogleJavaFormat.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
@AdditionalInfoLinks("https://github.com/google/google-java-format")
3232
public class GoogleJavaFormat extends SpotlessFormatterStep {
3333

34+
private static final String DEFAULT_VERSION_SYSPROP = "steps.google-java-format.default-version";
35+
36+
static {
37+
// workaround for dynamic property values in annotations
38+
System.setProperty(DEFAULT_VERSION_SYSPROP, GoogleJavaFormatStep.defaultVersion());
39+
}
40+
3441
@CommandLine.Option(
3542
names = {"--style", "-s"},
3643
defaultValue = "GOOGLE",
@@ -61,11 +68,18 @@ public enum Style {
6168
description = "Format javadoc." + OptionConstants.DEFAULT_VALUE_SUFFIX)
6269
boolean formatJavadoc;
6370

71+
@CommandLine.Option(
72+
names = {"--use-version", "-v"},
73+
defaultValue = "${sys:" + DEFAULT_VERSION_SYSPROP + "}",
74+
description =
75+
"The version of google java format to use. Must be >= 1.8." + OptionConstants.DEFAULT_VALUE_SUFFIX)
76+
String useVersion;
77+
6478
@Override
6579
public List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) {
6680
return List.of(GoogleJavaFormatStep.create(
6781
GoogleJavaFormatStep.defaultGroupArtifact(),
68-
GoogleJavaFormatStep.defaultVersion(),
82+
useVersion,
6983
style.name(),
7084
context.provisioner(),
7185
reflowLongStrings,

gradle/libs.versions.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ junit = "5.8.1"
88
maven-resolver = "2.0.13"
99
mockito = "5.17.0"
1010
native-include-cleanThat = "2.23"
11-
native-include-googleJavaFormat = "1.27.0"
1211
native-include-palantirJavaFormat = "2.67.0"
1312
# to find required version here check eclipse_wtp_formatter/v4.21.0.lockfile
1413
# where <4.21.0> is the default version of com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep#defaultVersion
@@ -40,7 +39,6 @@ maven-resolver-transport-file = { module = "org.apache.maven.resolver:maven-reso
4039
maven-resolver-util = { module = "org.apache.maven.resolver:maven-resolver-util" } # version from bom
4140
mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" }
4241
native-include-cleanThat = { module = "io.github.solven-eu.cleanthat:java", version.ref = "native-include-cleanThat" }
43-
native-include-googleJavaFormat = { module = "com.google.googlejavaformat:google-java-format", version.ref = "native-include-googleJavaFormat" }
4442
native-include-palantirJavaFormat = { module = "com.palantir.javaformat:palantir-java-format", version.ref = "native-include-palantirJavaFormat" }
4543
native-include-spotlessEclipseWtp = { module = "com.diffplug.spotless:spotless-eclipse-wtp", version.ref = "native-include-spotlessEclipseWtp" }
4644
picocli = { module = "info.picocli:picocli", version.ref = "picocli" }
@@ -66,7 +64,6 @@ maven-resolver-impl-libs = [
6664
]
6765
native-includes = [
6866
"native-include-cleanThat",
69-
"native-include-googleJavaFormat",
7067
"native-include-palantirJavaFormat",
7168
"native-include-spotlessEclipseWtp",
7269
]

0 commit comments

Comments
 (0)