From bc9fc74634f666c8f845a1754f2c656699afbc57 Mon Sep 17 00:00:00 2001 From: Markus Heberling Date: Fri, 7 Jan 2022 11:32:07 +0100 Subject: [PATCH 1/4] update editorconfig to relect crret formatting of .xml.mustache files --- .editorconfig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.editorconfig b/.editorconfig index 5ce3d79b66..a8061a0816 100644 --- a/.editorconfig +++ b/.editorconfig @@ -16,3 +16,6 @@ indent_size = 2 # Doc: https://youtrack.jetbrains.com/issue/IDEA-170643#focus=streamItem-27-3708697.0-0 ij_java_imports_layout = java.**,|,javax.**,|,org.**,|,com.**,|,com.diffplug.**,|,* ij_java_use_single_class_imports = true + +[*.xml.mustache] +indent_style = space From 59051ff6f5cc9598fdc9566f4c92772a7f6d7f00 Mon Sep 17 00:00:00 2001 From: Markus Heberling Date: Fri, 7 Jan 2022 10:35:01 +0100 Subject: [PATCH 2/4] Demonstrator test for #1073 java.io.NotSerializableException with upToDateChecking enabled --- .../maven/MavenIntegrationHarness.java | 29 +++++++---- .../spotless/maven/SpotlessCheckMojoTest.java | 5 +- .../incremental/PluginFingerprintTest.java | 49 ++++++++++++++++++- .../incremental/UpToDateCheckingTest.java | 45 +++++++++++++++++ .../pom-test-management.xml.mustache | 47 ++++++++++++++++++ .../src/test/resources/pom-test.xml.mustache | 1 + 6 files changed, 163 insertions(+), 13 deletions(-) create mode 100644 plugin-maven/src/test/resources/pom-test-management.xml.mustache diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java index 9a043c594b..d227aa51cf 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,6 +57,7 @@ public class MavenIntegrationHarness extends ResourceHarness { private static final String CONFIGURATION = "configuration"; private static final String EXECUTIONS = "executions"; private static final String MODULES = "modules"; + private static final String DEPENDENCIES = "dependencies"; private static final String MODULE_NAME = "name"; private static final String CHILD_ID = "childId"; private static final int REMOTE_DEBUG_PORT = 5005; @@ -144,11 +145,11 @@ protected void writePomWithMarkdownSteps(String... steps) throws IOException { } protected void writePom(String... configuration) throws IOException { - writePom(null, configuration); + writePom(null, configuration, null); } - protected void writePom(String[] executions, String[] configuration) throws IOException { - String pomXmlContent = createPomXmlContent(executions, configuration); + protected void writePom(String[] executions, String[] configuration, String[] dependencies) throws IOException { + String pomXmlContent = createPomXmlContent(null, executions, configuration, dependencies); setFile("pom.xml").toContent(pomXmlContent); } @@ -171,13 +172,17 @@ protected MultiModuleProjectCreator multiModuleProject() { return new MultiModuleProjectCreator(); } - protected String createPomXmlContent(String[] executions, String[] configuration) throws IOException { - return createPomXmlContent(null, executions, configuration); + protected String createPomXmlContent(String pluginVersion, String[] executions, String[] configuration, String[] dependencies) throws IOException { + return createPomXmlContent("/pom-test.xml.mustache", pluginVersion, executions, configuration, dependencies); + } + + protected String createPomXmlContent(String pomTemplate, String pluginVersion, String[] executions, String[] configuration, String[] dependencies) throws IOException { + Map params = buildPomXmlParams(pluginVersion, executions, configuration, null, dependencies); + return createPomXmlContent(pomTemplate, params); } protected String createPomXmlContent(String pluginVersion, String[] executions, String[] configuration) throws IOException { - Map params = buildPomXmlParams(pluginVersion, executions, configuration, null); - return createPomXmlContent("/pom-test.xml.mustache", params); + return createPomXmlContent(pluginVersion, executions, configuration, null); } private String createPomXmlContent(String pomTemplate, Map params) throws IOException { @@ -190,7 +195,7 @@ private String createPomXmlContent(String pomTemplate, Map param } } - private static Map buildPomXmlParams(String pluginVersion, String[] executions, String[] configuration, String[] modules) { + private static Map buildPomXmlParams(String pluginVersion, String[] executions, String[] configuration, String[] modules, String[] dependencies) { Map params = new HashMap<>(); params.put(SPOTLESS_MAVEN_PLUGIN_VERSION, pluginVersion == null ? getSystemProperty(SPOTLESS_MAVEN_PLUGIN_VERSION) : pluginVersion); @@ -207,6 +212,10 @@ private static Map buildPomXmlParams(String pluginVersion, Strin params.put(MODULES, moduleNames); } + if (dependencies != null) { + params.put(DEPENDENCIES, String.join("\n", dependencies)); + } + return params; } @@ -283,7 +292,7 @@ private void createRootPom() throws IOException { modulesList.addAll(subProjects.keySet()); String[] modules = modulesList.toArray(new String[0]); - Map rootPomParams = buildPomXmlParams(null, null, configuration, modules); + Map rootPomParams = buildPomXmlParams(null, null, configuration, modules, null); setFile("pom.xml").toContent(createPomXmlContent("/multi-module/pom-parent.xml.mustache", rootPomParams)); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessCheckMojoTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessCheckMojoTest.java index 7df83c86e7..cb37e4a4c5 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessCheckMojoTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessCheckMojoTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,7 +59,8 @@ void testSpotlessCheckBindingToVerifyPhase() throws Exception { " ", " ${basedir}/license.txt", " ", - ""}); + ""}, + null); testSpotlessCheck(UNFORMATTED_FILE, "verify", true); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java index f0e8a0e0f0..3cb4acad5b 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,6 +64,25 @@ class PluginFingerprintTest extends MavenIntegrationHarness { "" }; + private static final String[] DEPENDENCIES_1 = { + "", + " ", + " unknown", + " unknown", + " 1.0", + " ", + "" + }; + private static final String[] DEPENDENCIES_2 = { + "", + " ", + " unknown", + " unknown", + " 2.0", + " ", + "" + }; + private static final List FORMATTERS = singletonList(formatter(formatterStep("default"))); @Test @@ -80,6 +99,34 @@ void sameFingerprint() throws Exception { assertThat(fingerprint1).isEqualTo(fingerprint2); } + @Test + void sameFingerprintWithDependencies() throws Exception { + String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_1); + String xml2 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_1); + + MavenProject project1 = mavenProject(xml1); + MavenProject project2 = mavenProject(xml2); + + PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS); + PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS); + + assertThat(fingerprint1).isEqualTo(fingerprint2); + } + + @Test + void differentFingerprintForDifferentDependencies() throws Exception { + String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_1); + String xml2 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_2); + + MavenProject project1 = mavenProject(xml1); + MavenProject project2 = mavenProject(xml2); + + PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS); + PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS); + + assertThat(fingerprint1).isNotEqualTo(fingerprint2); + } + @Test void differentFingerprintForDifferentPluginVersion() throws Exception { String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1); diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java index c0e4ac2495..f315c7eefe 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java @@ -56,6 +56,51 @@ void enableUpToDateChecking() throws Exception { assertFormatted(files); } + @Test + void enableUpToDateCheckingWithPluginDependencies() throws Exception { + writePomWithPluginManagementAndDependency(); + + List files = writeUnformattedFiles(1); + String output = runSpotlessApply(); + + assertThat(output).contains("Up-to-date checking enabled"); + assertFormatted(files); + } + + @Test + void enableUpToDateCheckingWithPluginDependenciesMaven3_6_3() throws Exception { + writePomWithPluginManagementAndDependency(); + + setFile(".mvn/wrapper/maven-wrapper.properties").toContent("distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip\n"); + + List files = writeUnformattedFiles(1); + String output = runSpotlessApply(); + + assertThat(output).contains("Up-to-date checking enabled"); + assertFormatted(files); + } + + private void writePomWithPluginManagementAndDependency() throws IOException { + setFile("pom.xml").toContent(createPomXmlContent("/pom-test-management.xml.mustache", + null, + null, + new String[]{ + "", + " ", + "", + "", + " true", + ""}, + new String[]{ + "", + " ", + " javax.inject", + " javax.inject", + " 1", + " ", + ""})); + } + @Test void disableUpToDateChecking() throws Exception { writePomWithUpToDateCheckingEnabled(false); diff --git a/plugin-maven/src/test/resources/pom-test-management.xml.mustache b/plugin-maven/src/test/resources/pom-test-management.xml.mustache new file mode 100644 index 0000000000..611e2e05c6 --- /dev/null +++ b/plugin-maven/src/test/resources/pom-test-management.xml.mustache @@ -0,0 +1,47 @@ + + 4.0.0 + + com.diffplug.spotless + spotless-maven-plugin-tests + 1.0.0-SNAPSHOT + + Spotless Maven Plugin Tests + + + + 3.1.0 + + + + UTF-8 + 1.8 + 1.8 + + + + + + + com.diffplug.spotless + spotless-maven-plugin + {{spotlessMavenPluginVersion}} + + {{{configuration}}} + + {{{dependencies}}} + + + + + + com.diffplug.spotless + spotless-maven-plugin + + {{{executions}}} + + + + + + diff --git a/plugin-maven/src/test/resources/pom-test.xml.mustache b/plugin-maven/src/test/resources/pom-test.xml.mustache index b03c0b8205..122f5d9218 100644 --- a/plugin-maven/src/test/resources/pom-test.xml.mustache +++ b/plugin-maven/src/test/resources/pom-test.xml.mustache @@ -31,6 +31,7 @@ {{{executions}}} + {{{dependencies}}} From a1de9993b8256ea7191cd7edfc4f86fae822d879 Mon Sep 17 00:00:00 2001 From: Markus Heberling Date: Fri, 7 Jan 2022 10:33:56 +0100 Subject: [PATCH 3/4] Fix PluginFingerprint for plugin with dependencies Fixes #1073 --- .../maven/incremental/PluginFingerprint.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java index 48dfb1914d..c869963f32 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,9 +17,12 @@ import java.io.IOException; import java.io.UncheckedIOException; +import java.util.ArrayList; import java.util.Base64; +import java.util.List; import java.util.Objects; +import org.apache.maven.model.Dependency; import org.apache.maven.model.Plugin; import org.apache.maven.project.MavenProject; @@ -77,6 +80,13 @@ public String toString() { } private static byte[] digest(Plugin plugin, Iterable formatters) { + // dependencies can be an unserializable org.apache.maven.model.merge.ModelMerger$MergingList + // replace it with a serializable ArrayList + List dependencies = null; + if (plugin != null) { + dependencies = plugin.getDependencies(); + plugin.setDependencies(new ArrayList<>(dependencies)); + } try (ObjectDigestOutputStream out = ObjectDigestOutputStream.create()) { out.writeObject(plugin); for (Formatter formatter : formatters) { @@ -86,6 +96,11 @@ private static byte[] digest(Plugin plugin, Iterable formatters) { return out.digest(); } catch (IOException e) { throw new UncheckedIOException("Unable to serialize plugin " + plugin, e); + } finally { + // reset the original list + if (plugin != null) { + plugin.setDependencies(dependencies); + } } } } From 4d60d62ffdcb561ca8fb519a2ea652b92273b76f Mon Sep 17 00:00:00 2001 From: Markus Heberling Date: Fri, 7 Jan 2022 12:04:04 +0100 Subject: [PATCH 4/4] added changelog entry --- plugin-maven/CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 50c9ce98aa..9fbe4e1d12 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* Enabling the upToDateChecking with the plugin configured inside pluginManagement, with an additional dependency and running under Maven 3.6.3 leads to a java.io.NotSerializableException. ([#1074](https://github.com/diffplug/spotless/pull/1074)). ## [2.19.1] - 2022-01-07 ### Fixed