From d0460d74fa791bc15d03b01f328c99e268e7c308 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Sat, 2 Mar 2024 15:26:37 +0100 Subject: [PATCH 1/7] Skip execution in incremental (m2e) builds by default Clear stale messages during "check" mojo Allow to parameterize message severity Add Spotless prefix to messages This closes #1814 This closes #2037 --- .../spotless/maven/AbstractSpotlessMojo.java | 11 +++++++ .../spotless/maven/SpotlessCheckMojo.java | 31 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index 0528cccbdb..8da27ff8dd 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -201,6 +201,13 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter private UpToDateChecking upToDateChecking = UpToDateChecking.enabled(); + /** + * If set to {@code true} will also run on incremental builds (i.e. within Eclipse with m2e). + * Otherwise this goal is skipped in incremental builds and only runs on full builds. + */ + @Parameter(defaultValue = "false") + protected boolean enableForIncrementalBuild; + protected abstract void process(Iterable files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException; private static final int MINIMUM_JRE = 11; @@ -245,6 +252,10 @@ private boolean shouldSkip() { if (skip) { return true; } + if (buildContext.isIncremental() && !enableForIncrementalBuild) { + getLog().debug("Skipping for incremental builds as parameter 'enableForIncrementalBuilds' is set to 'false'"); + return true; + } switch (goal) { case GOAL_CHECK: 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 cc83b3f3c9..dbb86e805c 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 @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; import org.sonatype.plexus.build.incremental.BuildContext; import com.diffplug.spotless.Formatter; @@ -38,6 +39,30 @@ @Mojo(name = AbstractSpotlessMojo.GOAL_CHECK, defaultPhase = LifecyclePhase.VERIFY, threadSafe = true) public class SpotlessCheckMojo extends AbstractSpotlessMojo { + private static final String INCREMENTAL_MESSAGE_PREFIX = "Spotless Violation: "; + + public enum MessageSeverity { + WARNING(BuildContext.SEVERITY_WARNING), ERROR(BuildContext.SEVERITY_ERROR); + + private final int severity; + + MessageSeverity(int severity) { + this.severity = severity; + } + + public int getSeverity() { + return severity; + } + } + + /** + * The severity used to emit messages during incremental builds. + * Either {@code WARNING} or {@code ERROR}. + * @see AbstractSpotlessMojo#enableForIncrementalBuild + */ + @Parameter(defaultValue = "WARNING") + private MessageSeverity incrementalBuildMessageSeverity; + @Override protected void process(Iterable files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException { ImpactedFilesTracker counter = new ImpactedFilesTracker(); @@ -51,14 +76,14 @@ protected void process(Iterable files, Formatter formatter, UpToDateChecke } continue; } - + buildContext.removeMessages(file); try { PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file); if (!dirtyState.isClean() && !dirtyState.didNotConverge()) { problemFiles.add(file); if (buildContext.isIncremental()) { Map.Entry diffEntry = DiffMessageFormatter.diff(formatter, file); - buildContext.addMessage(file, diffEntry.getKey() + 1, 0, diffEntry.getValue(), BuildContext.SEVERITY_ERROR, null); + buildContext.addMessage(file, diffEntry.getKey() + 1, 0, INCREMENTAL_MESSAGE_PREFIX + diffEntry.getValue(), incrementalBuildMessageSeverity.getSeverity(), null); } counter.cleaned(); } else { From 70ba6eadd46ef1b574c948ba56b2fd2297194f2d Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 4 Mar 2024 10:40:18 +0100 Subject: [PATCH 2/7] Add section to readme how to enable m2e support --- plugin-maven/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 9a17cd2be7..ee598fcca6 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -1696,6 +1696,26 @@ cmd> mvn spotless:apply -DspotlessFiles=my/file/pattern.java,more/generic/.*-pat The patterns are matched using `String#matches(String)` against the absolute file path. +## Does Spotless support incremental builds in Eclipse + +Spotless comes with [m2e](https://eclipse.dev/m2e/) support. However by default its execution is skipped by default in incremental builds. To enable it use the following parameter. + +``` + + true + +``` + +In addition Eclipse problem markers are being emitted. By default they have the severity `WARNING`. +You can adjust this with + +``` + + ERROR + +``` +Note that for Incremental build support the goals have to be bound to a phase prior to `test`. + ## Example configurations (from real-world projects) From 7b1e2f2b94eba06a530c9bf6bea9f3c6c9510c60 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 4 Mar 2024 10:46:25 +0100 Subject: [PATCH 3/7] more markdown fixes --- plugin-maven/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index ee598fcca6..dfc69ee0f0 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -1698,7 +1698,7 @@ The patterns are matched using `String#matches(String)` against the absolute fil ## Does Spotless support incremental builds in Eclipse -Spotless comes with [m2e](https://eclipse.dev/m2e/) support. However by default its execution is skipped by default in incremental builds. To enable it use the following parameter. +Spotless comes with [m2e](https://eclipse.dev/m2e/) support. However by default its execution is skipped by default in incremental builds. To enable it use the following parameter ``` @@ -1714,6 +1714,7 @@ You can adjust this with ERROR ``` + Note that for Incremental build support the goals have to be bound to a phase prior to `test`. From 2c2cdfa596604bb71a340a1df46852831418f19a Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 4 Mar 2024 10:47:01 +0100 Subject: [PATCH 4/7] missing question mark --- plugin-maven/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index dfc69ee0f0..b99743aefe 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -1696,7 +1696,7 @@ cmd> mvn spotless:apply -DspotlessFiles=my/file/pattern.java,more/generic/.*-pat The patterns are matched using `String#matches(String)` against the absolute file path. -## Does Spotless support incremental builds in Eclipse +## Does Spotless support incremental builds in Eclipse? Spotless comes with [m2e](https://eclipse.dev/m2e/) support. However by default its execution is skipped by default in incremental builds. To enable it use the following parameter From 144b173ccc900b46459001354b63ad484d7c7c9d Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 4 Mar 2024 10:51:31 +0100 Subject: [PATCH 5/7] Add reasoning for disabling incremental support by default --- plugin-maven/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index b99743aefe..233f53178b 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -1698,7 +1698,8 @@ The patterns are matched using `String#matches(String)` against the absolute fil ## Does Spotless support incremental builds in Eclipse? -Spotless comes with [m2e](https://eclipse.dev/m2e/) support. However by default its execution is skipped by default in incremental builds. To enable it use the following parameter +Spotless comes with [m2e](https://eclipse.dev/m2e/) support. However, by default its execution is skipped in incremental builds as most developers want to fix all issues in one go via explicit `mvn spotless:apply` prior to raising a PR and don't want to be bothered with Spotless issues during working on the source code in the IDE. +To enable it use the following parameter ``` From 2f45f983c280f4d2fb51a02cb7067f965298f543 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Wed, 3 Apr 2024 18:04:00 +0200 Subject: [PATCH 6/7] rename parameters to have a m2e prefix --- plugin-maven/README.md | 4 ++-- .../com/diffplug/spotless/maven/AbstractSpotlessMojo.java | 4 ++-- .../java/com/diffplug/spotless/maven/SpotlessCheckMojo.java | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 233f53178b..b0ad703928 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -1703,7 +1703,7 @@ To enable it use the following parameter ``` - true + true ``` @@ -1712,7 +1712,7 @@ You can adjust this with ``` - ERROR + ERROR ``` diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index 8da27ff8dd..9f4d0e01bc 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -206,7 +206,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { * Otherwise this goal is skipped in incremental builds and only runs on full builds. */ @Parameter(defaultValue = "false") - protected boolean enableForIncrementalBuild; + protected boolean m2eEnableForIncrementalBuild; protected abstract void process(Iterable files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException; @@ -252,7 +252,7 @@ private boolean shouldSkip() { if (skip) { return true; } - if (buildContext.isIncremental() && !enableForIncrementalBuild) { + if (buildContext.isIncremental() && !m2eEnableForIncrementalBuild) { getLog().debug("Skipping for incremental builds as parameter 'enableForIncrementalBuilds' is set to 'false'"); return true; } 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 dbb86e805c..b326767c3c 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 @@ -58,10 +58,10 @@ public int getSeverity() { /** * The severity used to emit messages during incremental builds. * Either {@code WARNING} or {@code ERROR}. - * @see AbstractSpotlessMojo#enableForIncrementalBuild + * @see AbstractSpotlessMojo#m2eEnableForIncrementalBuild */ @Parameter(defaultValue = "WARNING") - private MessageSeverity incrementalBuildMessageSeverity; + private MessageSeverity m2eIncrementalBuildMessageSeverity; @Override protected void process(Iterable files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException { @@ -83,7 +83,7 @@ protected void process(Iterable files, Formatter formatter, UpToDateChecke problemFiles.add(file); if (buildContext.isIncremental()) { Map.Entry diffEntry = DiffMessageFormatter.diff(formatter, file); - buildContext.addMessage(file, diffEntry.getKey() + 1, 0, INCREMENTAL_MESSAGE_PREFIX + diffEntry.getValue(), incrementalBuildMessageSeverity.getSeverity(), null); + buildContext.addMessage(file, diffEntry.getKey() + 1, 0, INCREMENTAL_MESSAGE_PREFIX + diffEntry.getValue(), m2eIncrementalBuildMessageSeverity.getSeverity(), null); } counter.cleaned(); } else { From a2caf6e8d054766cff39605e42e7715dc32c4e62 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Thu, 4 Apr 2024 18:06:04 +0200 Subject: [PATCH 7/7] add changelog entry --- plugin-maven/CHANGES.md | 1 + plugin-maven/README.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 35de3c18ac..bd5b475f6b 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050)) ### Added * Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031)) +* Skip execution in M2E (incremental) builds by default ([#1814](https://github.com/diffplug/spotless/issues/1814), [#2037](https://github.com/diffplug/spotless/issues/2037)) ## [2.43.0] - 2024-01-23 ### Added diff --git a/plugin-maven/README.md b/plugin-maven/README.md index b0ad703928..f38e2ff4c9 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -1707,7 +1707,7 @@ To enable it use the following parameter ``` -In addition Eclipse problem markers are being emitted. By default they have the severity `WARNING`. +In addition Eclipse problem markers are being emitted for goal `check`. By default they have the severity `WARNING`. You can adjust this with ```