Skip to content

Commit f631f09

Browse files
authored
Update sortpom plugin to newest version (#1675)
2 parents 245867f + c5d88ad commit f631f09

File tree

9 files changed

+50
-23
lines changed

9 files changed

+50
-23
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Changes
14+
* Bump default sortpom version to latest `3.0.0` -> `3.2.1`. ([#1675](https://github.com/diffplug/spotless/pull/1675))
1315

1416
### Added
1517
* `Jvm.Support` now accepts `-SNAPSHOT` versions, treated as the non`-SNAPSHOT`. ([#1583](https://github.com/diffplug/spotless/issues/1583))

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ dependencies {
115115
// scalafmt
116116
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:3.7.3"
117117
// sortPom
118-
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0'
118+
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.2.1'
119119
sortPomCompileOnly 'org.slf4j:slf4j-api:2.0.0'
120120
}
121121

lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121
public class SortPomCfg implements Serializable {
2222
private static final long serialVersionUID = 1L;
2323

24+
public String version = "3.2.1";
25+
2426
public String encoding = "UTF-8";
2527

2628
public String lineSeparator = System.getProperty("line.separator");
@@ -43,6 +45,8 @@ public class SortPomCfg implements Serializable {
4345

4446
public String sortDependencies = null;
4547

48+
public String sortDependencyManagement = null;
49+
4650
public String sortDependencyExclusions = null;
4751

4852
public String sortPlugins = null;

lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,8 @@
2727

2828
public class SortPomStep {
2929
public static final String NAME = "sortPom";
30+
static final String PACKAGE = "com.github.ekryd.sortpom";
31+
static final String MAVEN_COORDINATE = PACKAGE + ":sortpom-sorter:";
3032

3133
private SortPomStep() {}
3234

@@ -42,7 +44,7 @@ static class State implements Serializable {
4244

4345
public State(SortPomCfg cfg, Provisioner provisioner) throws IOException {
4446
this.cfg = cfg;
45-
this.jarState = JarState.from("com.github.ekryd.sortpom:sortpom-sorter:3.0.0", provisioner);
47+
this.jarState = JarState.from(MAVEN_COORDINATE + cfg.version, provisioner);
4648
}
4749

4850
FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2022 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,11 +15,10 @@
1515
*/
1616
package com.diffplug.spotless.glue.pom;
1717

18-
import java.io.File;
19-
import java.io.FileInputStream;
20-
import java.io.FileOutputStream;
18+
import java.io.*;
19+
import java.nio.charset.Charset;
20+
import java.nio.file.Files;
2121

22-
import org.apache.commons.io.IOUtils;
2322
import org.slf4j.Logger;
2423
import org.slf4j.LoggerFactory;
2524

@@ -40,10 +39,12 @@ public SortPomFormatterFunc(SortPomCfg cfg) {
4039

4140
@Override
4241
public String apply(String input) throws Exception {
43-
// SortPom expects a file to sort, so we write the inpout into a temporary file
42+
// SortPom expects a file to sort, so we write the input into a temporary file
4443
File pom = File.createTempFile("pom", ".xml");
4544
pom.deleteOnExit();
46-
IOUtils.write(input, new FileOutputStream(pom), cfg.encoding);
45+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(pom, Charset.forName(cfg.encoding)))) {
46+
writer.write(input);
47+
}
4748
SortPomImpl sortPom = new SortPomImpl();
4849
sortPom.setup(new MySortPomLogger(), PluginParameters.builder()
4950
.setPomFile(pom)
@@ -52,11 +53,12 @@ public String apply(String input) throws Exception {
5253
.setFormatting(cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement, cfg.keepBlankLines)
5354
.setIndent(cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation)
5455
.setSortOrder(cfg.sortOrderFile, cfg.predefinedSortOrder)
55-
.setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions)
56-
.setTriggers(false)
56+
.setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortDependencyManagement,
57+
cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions)
58+
.setIgnoreLineSeparators(false)
5759
.build());
5860
sortPom.sortPom();
59-
return IOUtils.toString(new FileInputStream(pom), cfg.encoding);
61+
return Files.readString(pom.toPath(), Charset.forName(cfg.encoding));
6062
}
6163

6264
private static class MySortPomLogger implements SortPomLogger {

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
66
### Fixed
77
* Added `@DisableCachingByDefault` to `RegisterDependenciesTask`.
88
* When P2 download fails, indicate the responsible formatter. ([#1698](https://github.com/diffplug/spotless/issues/1698))
9+
### Changes
10+
* Bump default sortpom version to latest `3.0.0` -> `3.2.1`. ([#1675](https://github.com/diffplug/spotless/pull/1675))
911

1012
## [6.18.0] - 2023-04-06
1113
### Added

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+
### Changes
7+
* Bump default sortpom version to latest `3.0.0` -> `3.2.1`. ([#1675](https://github.com/diffplug/spotless/pull/1675))
68
### Fixed
79
* `palantir` step now accepts a `style` parameter, which is documentation had already claimed to do. ([#1694](https://github.com/diffplug/spotless/pull/1694))
810
* When P2 download fails, indicate the responsible formatter. ([#1698](https://github.com/diffplug/spotless/issues/1698))

plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,9 @@
2626
public class SortPom implements FormatterStepFactory {
2727
private final SortPomCfg defaultValues = new SortPomCfg();
2828

29+
@Parameter
30+
String version = defaultValues.version;
31+
2932
@Parameter
3033
String encoding = defaultValues.encoding;
3134

@@ -59,6 +62,9 @@ public class SortPom implements FormatterStepFactory {
5962
@Parameter
6063
String sortDependencies = defaultValues.sortDependencies;
6164

65+
@Parameter
66+
String sortDependencyManagement = defaultValues.sortDependencyManagement;
67+
6268
@Parameter
6369
String sortDependencyExclusions = defaultValues.sortDependencyExclusions;
6470

@@ -77,6 +83,7 @@ public class SortPom implements FormatterStepFactory {
7783
@Override
7884
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
7985
SortPomCfg cfg = new SortPomCfg();
86+
cfg.version = version;
8087
cfg.encoding = encoding;
8188
cfg.lineSeparator = lineSeparator;
8289
cfg.expandEmptyElements = expandEmptyElements;
@@ -88,6 +95,7 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
8895
cfg.predefinedSortOrder = predefinedSortOrder;
8996
cfg.sortOrderFile = sortOrderFile;
9097
cfg.sortDependencies = sortDependencies;
98+
cfg.sortDependencyManagement = sortDependencyManagement;
9199
cfg.sortDependencyExclusions = sortDependencyExclusions;
92100
cfg.sortPlugins = sortPlugins;
93101
cfg.sortProperties = sortProperties;

testlib/src/test/java/com/diffplug/spotless/pom/SortPomTest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,16 +17,21 @@
1717

1818
import org.junit.jupiter.api.Test;
1919

20-
import com.diffplug.spotless.Provisioner;
21-
import com.diffplug.spotless.StepHarness;
22-
import com.diffplug.spotless.TestProvisioner;
20+
import com.diffplug.spotless.*;
2321

24-
public class SortPomTest {
22+
public class SortPomTest extends ResourceHarness {
2523
@Test
2624
public void testSortPomWithDefaultConfig() throws Exception {
2725
SortPomCfg cfg = new SortPomCfg();
28-
Provisioner provisioner = TestProvisioner.mavenCentral();
29-
StepHarness harness = StepHarness.forStep(SortPomStep.create(cfg, provisioner));
30-
harness.testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml");
26+
FormatterStep step = SortPomStep.create(cfg, TestProvisioner.mavenCentral());
27+
StepHarness.forStep(step).testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml");
28+
}
29+
30+
@Test
31+
public void testSortPomWithVersion() throws Exception {
32+
SortPomCfg cfg = new SortPomCfg();
33+
cfg.version = "3.2.1";
34+
FormatterStep step = SortPomStep.create(cfg, TestProvisioner.mavenCentral());
35+
StepHarness.forStep(step).testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml");
3136
}
3237
}

0 commit comments

Comments
 (0)