Skip to content

Commit

Permalink
Skip execution in incremental (m2e) builds by default
Browse files Browse the repository at this point in the history
Clear stale messages during "check" mojo
Allow to parameterize message severity
Add Spotless prefix to messages

This closes #1814
This closes #2037
  • Loading branch information
kwin committed Mar 2, 2024
1 parent aa113ff commit d0460d7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException;

private static final int MINIMUM_JRE = 11;
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
ImpactedFilesTracker counter = new ImpactedFilesTracker();
Expand All @@ -51,14 +76,14 @@ protected void process(Iterable<File> 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<Integer, String> 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 {
Expand Down

0 comments on commit d0460d7

Please sign in to comment.