Skip to content

Commit d0460d7

Browse files
committed
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
1 parent aa113ff commit d0460d7

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

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

Lines changed: 11 additions & 0 deletions
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 enableForIncrementalBuild;
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() && !enableForIncrementalBuild) {
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

Lines changed: 28 additions & 3 deletions
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#enableForIncrementalBuild
62+
*/
63+
@Parameter(defaultValue = "WARNING")
64+
private MessageSeverity incrementalBuildMessageSeverity;
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(), incrementalBuildMessageSeverity.getSeverity(), null);
6287
}
6388
counter.cleaned();
6489
} else {

0 commit comments

Comments
 (0)