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 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..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 @@ -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,16 @@ protected void process(String name, Iterable files, Formatter formatter, U } if (!problemFiles.isEmpty()) { + final String runToFixMessage; + 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."; + } 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..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 @@ -65,10 +65,43 @@ void testSpotlessCheckBindingToVerifyPhase() throws Exception { null, null); - testSpotlessCheck(UNFORMATTED_FILE, "verify", true); + 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, ""); + } + + 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 +109,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(); }