From c40afe736c23c7ff55e0090784cf1663c02baff9 Mon Sep 17 00:00:00 2001 From: "Tyler B. Thrailkill" Date: Tue, 18 Apr 2023 16:09:07 -0600 Subject: [PATCH 1/4] Update sortpom plugin to newest version Update the sortpom plugin to add sortDependencyManagement element. Remove dependency on IOUtils --- lib/build.gradle | 2 +- .../com/diffplug/spotless/pom/SortPomCfg.java | 4 +++- .../diffplug/spotless/pom/SortPomStep.java | 4 ++-- .../glue/pom/SortPomFormatterFunc.java | 22 ++++++++++--------- .../diffplug/spotless/maven/pom/SortPom.java | 6 ++++- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/build.gradle b/lib/build.gradle index 9e31bed698..8db911e33e 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -115,7 +115,7 @@ dependencies { // scalafmt scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:3.7.3" // sortPom - sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0' + sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.2.1' sortPomCompileOnly 'org.slf4j:slf4j-api:2.0.0' } diff --git a/lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java b/lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java index 93c11e1e31..7c9f0f09a9 100644 --- a/lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java +++ b/lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,8 @@ public class SortPomCfg implements Serializable { public String sortDependencies = null; + public String sortDependencyManagement = null; + public String sortDependencyExclusions = null; public String sortPlugins = null; diff --git a/lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java b/lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java index 69d833f8e7..7f672d5c05 100644 --- a/lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java +++ b/lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,7 +42,7 @@ static class State implements Serializable { public State(SortPomCfg cfg, Provisioner provisioner) throws IOException { this.cfg = cfg; - this.jarState = JarState.from("com.github.ekryd.sortpom:sortpom-sorter:3.0.0", provisioner); + this.jarState = JarState.from("com.github.ekryd.sortpom:sortpom-sorter:3.2.1", provisioner); } FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { diff --git a/lib/src/sortPom/java/com/diffplug/spotless/glue/pom/SortPomFormatterFunc.java b/lib/src/sortPom/java/com/diffplug/spotless/glue/pom/SortPomFormatterFunc.java index 22a0249634..4dcde62bae 100644 --- a/lib/src/sortPom/java/com/diffplug/spotless/glue/pom/SortPomFormatterFunc.java +++ b/lib/src/sortPom/java/com/diffplug/spotless/glue/pom/SortPomFormatterFunc.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2022 DiffPlug + * Copyright 2021-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,11 +15,10 @@ */ package com.diffplug.spotless.glue.pom; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.*; +import java.nio.charset.Charset; +import java.nio.file.Files; -import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,10 +39,12 @@ public SortPomFormatterFunc(SortPomCfg cfg) { @Override public String apply(String input) throws Exception { - // SortPom expects a file to sort, so we write the inpout into a temporary file + // SortPom expects a file to sort, so we write the input into a temporary file File pom = File.createTempFile("pom", ".xml"); pom.deleteOnExit(); - IOUtils.write(input, new FileOutputStream(pom), cfg.encoding); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(pom, Charset.forName(cfg.encoding)))) { + writer.write(input); + } SortPomImpl sortPom = new SortPomImpl(); sortPom.setup(new MySortPomLogger(), PluginParameters.builder() .setPomFile(pom) @@ -52,11 +53,12 @@ public String apply(String input) throws Exception { .setFormatting(cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement, cfg.keepBlankLines) .setIndent(cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation) .setSortOrder(cfg.sortOrderFile, cfg.predefinedSortOrder) - .setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions) - .setTriggers(false) + .setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortDependencyManagement, + cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions) + .setIgnoreLineSeparators(false) .build()); sortPom.sortPom(); - return IOUtils.toString(new FileInputStream(pom), cfg.encoding); + return Files.readString(pom.toPath(), Charset.forName(cfg.encoding)); } private static class MySortPomLogger implements SortPomLogger { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java index f4fe8cb96b..a1706ee381 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,6 +59,9 @@ public class SortPom implements FormatterStepFactory { @Parameter String sortDependencies = defaultValues.sortDependencies; + @Parameter + String sortDependencyManagement = defaultValues.sortDependencyManagement; + @Parameter String sortDependencyExclusions = defaultValues.sortDependencyExclusions; @@ -88,6 +91,7 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { cfg.predefinedSortOrder = predefinedSortOrder; cfg.sortOrderFile = sortOrderFile; cfg.sortDependencies = sortDependencies; + cfg.sortDependencyManagement = sortDependencyManagement; cfg.sortDependencyExclusions = sortDependencyExclusions; cfg.sortPlugins = sortPlugins; cfg.sortProperties = sortProperties; From a4ddf9d85bf867b10037fa5c84fcafc0e4e92dd2 Mon Sep 17 00:00:00 2001 From: "Tyler B. Thrailkill" Date: Tue, 18 Apr 2023 16:09:12 -0600 Subject: [PATCH 2/4] Add sortpom change to changelog --- CHANGES.md | 1 + plugin-gradle/CHANGES.md | 1 + plugin-maven/CHANGES.md | 1 + 3 files changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 50141ece9c..323912888b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +* Update sortpom plugin to the newest version ## [2.38.0] - 2023-04-06 ### Added diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index becf2e5ac7..f64b54b65d 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed * Added `@DisableCachingByDefault` to `RegisterDependenciesTask`. +* Update sortpom plugin to the newest version ## [6.18.0] - 2023-04-06 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index df994a2b44..96a7b5189f 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +* Update sortpom plugin to the newest version ## [2.36.0] - 2023-04-06 ### Added From 1028dd1689aa8cda1ac957d3d40c47153fe4b683 Mon Sep 17 00:00:00 2001 From: "Tyler B. Thrailkill" Date: Tue, 18 Apr 2023 16:09:15 -0600 Subject: [PATCH 3/4] Update changelog with pr reference --- CHANGES.md | 3 ++- plugin-gradle/CHANGES.md | 3 ++- plugin-maven/CHANGES.md | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 323912888b..6d5b4d9ead 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,7 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] -* Update sortpom plugin to the newest version +### Changes +* Bump default sortpom version to latest `3.0.0` -> `3.2.1`. ([#1675](https://github.com/diffplug/spotless/pull/1675)) ## [2.38.0] - 2023-04-06 ### Added diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index f64b54b65d..1e875dff9c 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,7 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed * Added `@DisableCachingByDefault` to `RegisterDependenciesTask`. -* Update sortpom plugin to the newest version +### Changes +* Bump default sortpom version to latest `3.0.0` -> `3.2.1`. ([#1675](https://github.com/diffplug/spotless/pull/1675)) ## [6.18.0] - 2023-04-06 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 96a7b5189f..911ffc306c 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,7 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] -* Update sortpom plugin to the newest version +### Changes +* Bump default sortpom version to latest `3.0.0` -> `3.2.1`. ([#1675](https://github.com/diffplug/spotless/pull/1675)) ## [2.36.0] - 2023-04-06 ### Added From e686e4f58263b6a046c62b124404a032ebce9a35 Mon Sep 17 00:00:00 2001 From: "Tyler B. Thrailkill" Date: Tue, 18 Apr 2023 16:09:18 -0600 Subject: [PATCH 4/4] Make sortpom version configurable Add test for version config Update SortPomTest to use ResourceHarness --- .../com/diffplug/spotless/pom/SortPomCfg.java | 2 ++ .../diffplug/spotless/pom/SortPomStep.java | 4 +++- .../diffplug/spotless/maven/pom/SortPom.java | 4 ++++ .../diffplug/spotless/pom/SortPomTest.java | 21 ++++++++++++------- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java b/lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java index 7c9f0f09a9..8315035dc7 100644 --- a/lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java +++ b/lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java @@ -21,6 +21,8 @@ public class SortPomCfg implements Serializable { private static final long serialVersionUID = 1L; + public String version = "3.2.1"; + public String encoding = "UTF-8"; public String lineSeparator = System.getProperty("line.separator"); diff --git a/lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java b/lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java index 7f672d5c05..4325d16516 100644 --- a/lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java +++ b/lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java @@ -27,6 +27,8 @@ public class SortPomStep { public static final String NAME = "sortPom"; + static final String PACKAGE = "com.github.ekryd.sortpom"; + static final String MAVEN_COORDINATE = PACKAGE + ":sortpom-sorter:"; private SortPomStep() {} @@ -42,7 +44,7 @@ static class State implements Serializable { public State(SortPomCfg cfg, Provisioner provisioner) throws IOException { this.cfg = cfg; - this.jarState = JarState.from("com.github.ekryd.sortpom:sortpom-sorter:3.2.1", provisioner); + this.jarState = JarState.from(MAVEN_COORDINATE + cfg.version, provisioner); } FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java index a1706ee381..8620427f79 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java @@ -26,6 +26,9 @@ public class SortPom implements FormatterStepFactory { private final SortPomCfg defaultValues = new SortPomCfg(); + @Parameter + String version = defaultValues.version; + @Parameter String encoding = defaultValues.encoding; @@ -80,6 +83,7 @@ public class SortPom implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { SortPomCfg cfg = new SortPomCfg(); + cfg.version = version; cfg.encoding = encoding; cfg.lineSeparator = lineSeparator; cfg.expandEmptyElements = expandEmptyElements; diff --git a/testlib/src/test/java/com/diffplug/spotless/pom/SortPomTest.java b/testlib/src/test/java/com/diffplug/spotless/pom/SortPomTest.java index f397962086..c88918f505 100644 --- a/testlib/src/test/java/com/diffplug/spotless/pom/SortPomTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/pom/SortPomTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,16 +17,21 @@ import org.junit.jupiter.api.Test; -import com.diffplug.spotless.Provisioner; -import com.diffplug.spotless.StepHarness; -import com.diffplug.spotless.TestProvisioner; +import com.diffplug.spotless.*; -public class SortPomTest { +public class SortPomTest extends ResourceHarness { @Test public void testSortPomWithDefaultConfig() throws Exception { SortPomCfg cfg = new SortPomCfg(); - Provisioner provisioner = TestProvisioner.mavenCentral(); - StepHarness harness = StepHarness.forStep(SortPomStep.create(cfg, provisioner)); - harness.testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml"); + FormatterStep step = SortPomStep.create(cfg, TestProvisioner.mavenCentral()); + StepHarness.forStep(step).testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml"); + } + + @Test + public void testSortPomWithVersion() throws Exception { + SortPomCfg cfg = new SortPomCfg(); + cfg.version = "3.2.1"; + FormatterStep step = SortPomStep.create(cfg, TestProvisioner.mavenCentral()); + StepHarness.forStep(step).testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml"); } }