Skip to content

Commit 51b106f

Browse files
committed
Support sortpom versions back to 3.2.1
1 parent cdb0510 commit 51b106f

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1919
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
2020
* Bump default `ktlint` version to latest `1.1.1` -> `1.2.1`. ([#2057](https://github.com/diffplug/spotless/pull/2057))
2121
* Bump default `sortpom` version to latest `3.4.0` -> `3.4.1`. ([#2078](https://github.com/diffplug/spotless/pull/2078))
22-
* Bump default `sortpom` version to latest `3.4.1` -> `4.0.0`. ([#2115](https://github.com/diffplug/spotless/pull/2115))
22+
* Bump default `sortpom` version to latest `3.4.1` -> `4.0.0` and support versions back to `3.2.1`. ([#2115](https://github.com/diffplug/spotless/pull/2115))
2323
### Removed
2424
* **BREAKING** Remove `JarState.getMavenCoordinate(String prefix)`. ([#1945](https://github.com/diffplug/spotless/pull/1945))
2525
* **BREAKING** Replace `PipeStepPair` with `FenceStep`. ([#1954](https://github.com/diffplug/spotless/pull/1954))

lib/src/sortPom/java/com/diffplug/spotless/glue/pom/SortPomFormatterFunc.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.diffplug.spotless.glue.pom;
1717

1818
import java.io.*;
19+
import java.lang.reflect.Method;
1920
import java.nio.charset.Charset;
2021
import java.nio.file.Files;
2122

@@ -46,17 +47,45 @@ public String apply(String input) throws Exception {
4647
writer.write(input);
4748
}
4849
SortPomImpl sortPom = new SortPomImpl();
49-
sortPom.setup(new MySortPomLogger(), PluginParameters.builder()
50+
PluginParameters.Builder builder = PluginParameters.builder()
5051
.setPomFile(pom)
5152
.setFileOutput(false, null, null, false)
52-
.setEncoding(cfg.encoding)
53-
.setFormatting(cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement, cfg.keepBlankLines, cfg.endWithNewline)
54-
.setIndent(cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation, cfg.indentAttribute)
53+
.setEncoding(cfg.encoding);
54+
try {
55+
builder = builder
56+
.setFormatting(cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement,
57+
cfg.keepBlankLines, cfg.endWithNewline);
58+
} catch (NoSuchMethodError e) {
59+
try {
60+
Method method = PluginParameters.Builder.class
61+
.getMethod("setFormatting", String.class, boolean.class, boolean.class, boolean.class);
62+
builder = (PluginParameters.Builder) method
63+
.invoke(builder, cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement,
64+
cfg.keepBlankLines);
65+
} catch (Exception ignore) {
66+
throw e;
67+
}
68+
}
69+
try {
70+
builder = builder
71+
.setIndent(cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation,
72+
cfg.indentAttribute);
73+
} catch (NoSuchMethodError e) {
74+
try {
75+
Method method = PluginParameters.Builder.class
76+
.getMethod("setIndent", int.class, boolean.class, boolean.class);
77+
builder = (PluginParameters.Builder) method
78+
.invoke(builder, cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation);
79+
} catch (Exception ignore) {
80+
throw e;
81+
}
82+
}
83+
builder = builder
5584
.setSortOrder(cfg.sortOrderFile, cfg.predefinedSortOrder)
5685
.setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortDependencyManagement,
5786
cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions)
58-
.setIgnoreLineSeparators(false)
59-
.build());
87+
.setIgnoreLineSeparators(false);
88+
sortPom.setup(new MySortPomLogger(), builder.build());
6089
sortPom.sortPom();
6190
return Files.readString(pom.toPath(), Charset.forName(cfg.encoding));
6291
}

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1414
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
1515
* Bump default `ktlint` version to latest `1.1.1` -> `1.2.1`. ([#2057](https://github.com/diffplug/spotless/pull/2057))
1616
* Bump default `sortpom` version to latest `3.4.0` -> `3.4.1`. ([#2078](https://github.com/diffplug/spotless/pull/2078))
17-
* Bump default `sortpom` version to latest `3.4.1` -> `4.0.0`. ([#2115](https://github.com/diffplug/spotless/pull/2115))
17+
* Bump default `sortpom` version to latest `3.4.1` -> `4.0.0` and support versions back to `3.2.1`. ([#2115](https://github.com/diffplug/spotless/pull/2115))
1818
### Added
1919
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
2020
* Add support for formatting and sorting Maven POMs ([#2082](https://github.com/diffplug/spotless/issues/2082))

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SortPomGradleTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.diffplug.gradle.spotless;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.ValueSource;
1921

2022
class SortPomGradleTest extends GradleIntegrationHarness {
2123
@Test
@@ -63,8 +65,9 @@ void sortPomWithTarget() throws Exception {
6365
assertFile("test.xml").sameAsResource("pom/pom_clean_default.xml");
6466
}
6567

66-
@Test
67-
void sortPomWithVersion() throws Exception {
68+
@ParameterizedTest
69+
@ValueSource(strings = {"3.2.1", "3.3.0", "3.4.1", "4.0.0"})
70+
void sortPomWithVersion(String version) throws Exception {
6871
// given
6972
setFile("build.gradle").toLines(
7073
"plugins {",
@@ -73,7 +76,7 @@ void sortPomWithVersion() throws Exception {
7376
"repositories { mavenCentral() }",
7477
"spotless {",
7578
" pom {",
76-
" sortPom '4.0.0'",
79+
" sortPom '" + version + "'",
7780
" }",
7881
"}");
7982
setFile("pom.xml").toResource("pom/pom_dirty.xml");

plugin-maven/CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
1313
* Bump default `ktlint` version to latest `1.1.1` -> `1.2.1`. ([#2057](https://github.com/diffplug/spotless/pull/2057))
1414
* Bump default `sortpom` version to latest `3.4.0` -> `3.4.1`. ([#2078](https://github.com/diffplug/spotless/pull/2078))
15-
* Bump default `sortpom` version to latest `3.4.1` -> `4.0.0`. ([#2115](https://github.com/diffplug/spotless/pull/2115))
15+
* Bump default `sortpom` version to latest `3.4.1` -> `4.0.0` and support versions back to `3.2.1`. ([#2115](https://github.com/diffplug/spotless/pull/2115))
1616
### Added
1717
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
1818
* Skip execution in M2E (incremental) builds by default ([#1814](https://github.com/diffplug/spotless/issues/1814), [#2037](https://github.com/diffplug/spotless/issues/2037))

0 commit comments

Comments
 (0)