Skip to content

Commit f7346d5

Browse files
authored
Skip execution in incremental (m2e) builds by default (#2059)
2 parents e90f0a3 + a2caf6e commit f7346d5

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1414
* Bump default `sortpom` version to latest `3.4.0` -> `3.4.1`. ([#2078](https://github.com/diffplug/spotless/pull/2078))
1515
### Added
1616
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
17+
* Skip execution in M2E (incremental) builds by default ([#1814](https://github.com/diffplug/spotless/issues/1814), [#2037](https://github.com/diffplug/spotless/issues/2037))
1718

1819
## [2.43.0] - 2024-01-23
1920
### Added

plugin-maven/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,28 @@ cmd> mvn spotless:apply -DspotlessFiles=my/file/pattern.java,more/generic/.*-pat
16981698

16991699
The patterns are matched using `String#matches(String)` against the absolute file path.
17001700

1701+
## Does Spotless support incremental builds in Eclipse?
1702+
1703+
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.
1704+
To enable it use the following parameter
1705+
1706+
```
1707+
<configuration>
1708+
<m2eEnableForIncrementalBuild>true</m2eEnableForIncrementalBuild><!-- this is false by default -->
1709+
</configuration>
1710+
```
1711+
1712+
In addition Eclipse problem markers are being emitted for goal `check`. By default they have the severity `WARNING`.
1713+
You can adjust this with
1714+
1715+
```
1716+
<configuration>
1717+
<m2eIncrementalBuildMessageSeverity>ERROR</m2eIncrementalBuildMessageSeverity><!-- WARNING or ERROR -->
1718+
</configuration>
1719+
```
1720+
1721+
Note that for Incremental build support the goals have to be bound to a phase prior to `test`.
1722+
17011723
<a name="examples"></a>
17021724

17031725
## Example configurations (from real-world projects)

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

+11
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
201201
@Parameter
202202
private UpToDateChecking upToDateChecking = UpToDateChecking.enabled();
203203

204+
/**
205+
* If set to {@code true} will also run on incremental builds (i.e. within Eclipse with m2e).
206+
* Otherwise this goal is skipped in incremental builds and only runs on full builds.
207+
*/
208+
@Parameter(defaultValue = "false")
209+
protected boolean m2eEnableForIncrementalBuild;
210+
204211
protected abstract void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException;
205212

206213
private static final int MINIMUM_JRE = 11;
@@ -245,6 +252,10 @@ private boolean shouldSkip() {
245252
if (skip) {
246253
return true;
247254
}
255+
if (buildContext.isIncremental() && !m2eEnableForIncrementalBuild) {
256+
getLog().debug("Skipping for incremental builds as parameter 'enableForIncrementalBuilds' is set to 'false'");
257+
return true;
258+
}
248259

249260
switch (goal) {
250261
case GOAL_CHECK:

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.apache.maven.plugin.MojoExecutionException;
2525
import org.apache.maven.plugins.annotations.LifecyclePhase;
2626
import org.apache.maven.plugins.annotations.Mojo;
27+
import org.apache.maven.plugins.annotations.Parameter;
2728
import org.sonatype.plexus.build.incremental.BuildContext;
2829

2930
import com.diffplug.spotless.Formatter;
@@ -38,6 +39,30 @@
3839
@Mojo(name = AbstractSpotlessMojo.GOAL_CHECK, defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
3940
public class SpotlessCheckMojo extends AbstractSpotlessMojo {
4041

42+
private static final String INCREMENTAL_MESSAGE_PREFIX = "Spotless Violation: ";
43+
44+
public enum MessageSeverity {
45+
WARNING(BuildContext.SEVERITY_WARNING), ERROR(BuildContext.SEVERITY_ERROR);
46+
47+
private final int severity;
48+
49+
MessageSeverity(int severity) {
50+
this.severity = severity;
51+
}
52+
53+
public int getSeverity() {
54+
return severity;
55+
}
56+
}
57+
58+
/**
59+
* The severity used to emit messages during incremental builds.
60+
* Either {@code WARNING} or {@code ERROR}.
61+
* @see AbstractSpotlessMojo#m2eEnableForIncrementalBuild
62+
*/
63+
@Parameter(defaultValue = "WARNING")
64+
private MessageSeverity m2eIncrementalBuildMessageSeverity;
65+
4166
@Override
4267
protected void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
4368
ImpactedFilesTracker counter = new ImpactedFilesTracker();
@@ -51,14 +76,14 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
5176
}
5277
continue;
5378
}
54-
79+
buildContext.removeMessages(file);
5580
try {
5681
PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file);
5782
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
5883
problemFiles.add(file);
5984
if (buildContext.isIncremental()) {
6085
Map.Entry<Integer, String> diffEntry = DiffMessageFormatter.diff(formatter, file);
61-
buildContext.addMessage(file, diffEntry.getKey() + 1, 0, diffEntry.getValue(), BuildContext.SEVERITY_ERROR, null);
86+
buildContext.addMessage(file, diffEntry.getKey() + 1, 0, INCREMENTAL_MESSAGE_PREFIX + diffEntry.getValue(), m2eIncrementalBuildMessageSeverity.getSeverity(), null);
6287
}
6388
counter.cleaned();
6489
} else {

0 commit comments

Comments
 (0)