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 9a17cd2be7..f38e2ff4c9 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -1696,6 +1696,28 @@ 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 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 + +``` + + true + +``` + +In addition Eclipse problem markers are being emitted for goal `check`. 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) 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..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 @@ -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 m2eEnableForIncrementalBuild; + 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() && !m2eEnableForIncrementalBuild) { + 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..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 @@ -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#m2eEnableForIncrementalBuild + */ + @Parameter(defaultValue = "WARNING") + private MessageSeverity m2eIncrementalBuildMessageSeverity; + @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(), m2eIncrementalBuildMessageSeverity.getSeverity(), null); } counter.cleaned(); } else {