From a377b94c9bc5581a1b549a3f1dad602a2163e80e Mon Sep 17 00:00:00 2001 From: Dimitry Polivaev Date: Tue, 20 May 2025 12:55:07 +0200 Subject: [PATCH 1/3] feat: add execution ID parameter to SpotlessCheckMojo for improved error messaging, issue #1988 - Introduced a new parameter `executionId` to capture the execution ID from Maven's configuration. - Updated error messages to include the execution ID when suggesting commands to fix format violations. - Enhanced test cases to validate the new behavior of error messages based on the execution ID. --- .../diffplug/spotless/maven/SpotlessCheckMojo.java | 14 +++++++++++++- .../spotless/maven/SpotlessCheckMojoTest.java | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java index 116a24dcf4..bbc25adea7 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java @@ -63,6 +63,12 @@ public int getSeverity() { @Parameter(defaultValue = "WARNING") private MessageSeverity m2eIncrementalBuildMessageSeverity; + /** + * The execution ID from Maven's configuration. + */ + @Parameter(defaultValue = "${mojoExecution.executionId}") + private String executionId; + @Override protected void process(String name, Iterable files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException { ImpactedFilesTracker counter = new ImpactedFilesTracker(); @@ -104,8 +110,14 @@ protected void process(String name, Iterable files, Formatter formatter, U } if (!problemFiles.isEmpty()) { + final String runToFixMessage; + if (executionId != null && !executionId.isEmpty() && !executionId.equals("default-cli")) { + runToFixMessage = "Run 'mvn spotless:apply@" + executionId + "' to fix these violations."; + } else { + runToFixMessage = "Run 'mvn spotless:apply' to fix these violations."; + } throw new MojoExecutionException(DiffMessageFormatter.builder() - .runToFix("Run 'mvn spotless:apply' to fix these violations.") + .runToFix(runToFixMessage) .formatter(baseDir.toPath(), formatter) .problemFiles(problemFiles) .getMessage()); 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 92a78e3923..41705cfc07 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 @@ -65,10 +65,18 @@ void testSpotlessCheckBindingToVerifyPhase() throws Exception { null, null); - testSpotlessCheck(UNFORMATTED_FILE, "verify", true); + testSpotlessCheck(UNFORMATTED_FILE, "verify", "check"); } private void testSpotlessCheck(String fileName, String command, boolean expectError) throws Exception { + testSpotlessCheck(fileName, command, expectError, ""); + } + + private void testSpotlessCheck(String fileName, String command, String expectedFailingExecutionId) throws Exception { + testSpotlessCheck(fileName, command, true, "@" + expectedFailingExecutionId); + } + + private void testSpotlessCheck(String fileName, String command, boolean expectError, String expectedExecutionId) throws Exception { setFile("license.txt").toResource("license/TestLicense"); setFile("src/main/java/com.github.youribonnaffe.gradle.format/Java8Test.java").toResource(fileName); @@ -76,7 +84,9 @@ private void testSpotlessCheck(String fileName, String command, boolean expectEr if (expectError) { ProcessRunner.Result result = mavenRunner.runHasError(); - assertThat(result.stdOutUtf8()).contains("The following files had format violations"); + String stdOutUtf8 = result.stdOutUtf8(); + assertThat(stdOutUtf8).contains("The following files had format violations"); + assertThat(stdOutUtf8).contains("Run 'mvn spotless:apply" + expectedExecutionId + "' to fix these violations."); } else { mavenRunner.runNoError(); } From aa0d8ba29d0ada8e38f854d799e6875eaa0c7e73 Mon Sep 17 00:00:00 2001 From: Dimitry Polivaev Date: Tue, 20 May 2025 13:03:31 +0200 Subject: [PATCH 2/3] chore: update CHANGES.md to include execution ID in error messages for format violations (#1988) --- plugin-maven/CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 2558c7a239..844cfbf09d 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Changed * Bump default `eclipse` version to latest `4.34` -> `4.35`. ([#2458](https://github.com/diffplug/spotless/pull/2458)) * Bump default `greclipse` version to latest `4.32` -> `4.35`. ([#2458](https://github.com/diffplug/spotless/pull/2458)) +* Include execution ID in error messages when format violations are found. ([#1988](https://github.com/diffplug/spotless/issues/1988)) ## [2.44.4] - 2025-04-07 ### Changed From a1903b8b68318eb6be62a542e6291a332f403e0b Mon Sep 17 00:00:00 2001 From: Dimitry Polivaev Date: Wed, 21 May 2025 13:13:30 +0200 Subject: [PATCH 3/3] fix: refine execution ID handling in error messages for SpotlessCheckMojo - Updated the logic to check for execution IDs, ensuring that messages correctly suggest commands based on the presence of default execution IDs. - Added a new test case to validate behavior when no explicit execution ID is provided, confirming that the output message is simplified accordingly. --- .../spotless/maven/SpotlessCheckMojo.java | 4 ++- .../spotless/maven/SpotlessCheckMojoTest.java | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java index bbc25adea7..270e22ad1a 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java @@ -111,7 +111,9 @@ protected void process(String name, Iterable files, Formatter formatter, U if (!problemFiles.isEmpty()) { final String runToFixMessage; - if (executionId != null && !executionId.isEmpty() && !executionId.equals("default-cli")) { + if (executionId != null && !executionId.isEmpty() + && !executionId.equals("default") + && !executionId.startsWith("default-")) { runToFixMessage = "Run 'mvn spotless:apply@" + executionId + "' to fix these violations."; } else { runToFixMessage = "Run 'mvn spotless:apply' to fix these violations."; 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 41705cfc07..0c5837b9d7 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 @@ -68,6 +68,31 @@ void testSpotlessCheckBindingToVerifyPhase() throws Exception { testSpotlessCheck(UNFORMATTED_FILE, "verify", "check"); } + @Test + void testSpotlessCheckWithSimplifiedConfiguration() throws Exception { + // Use configuration with default execution ID + writePom( + new String[]{ + "", + " ", + " verify", + " ", + " check", + " ", + ""}, + new String[]{ + "", + " ", + " ${basedir}/license.txt", + " ", + ""}, + null, + null); + + // Without explicit execution ID, we should get a simple message without @id + testSpotlessCheck(UNFORMATTED_FILE, "verify", true, ""); + } + private void testSpotlessCheck(String fileName, String command, boolean expectError) throws Exception { testSpotlessCheck(fileName, command, expectError, ""); }