Skip to content

Commit 695137e

Browse files
committed
Bring maven in-line with gradle.
1 parent e0d1933 commit 695137e

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,24 @@ protected void process(String name, Iterable<File> files, Formatter formatter, U
8080
counter.checkedButAlreadyClean();
8181
}
8282

83-
// In apply mode, lints are usually warnings, but fatal errors should still fail the build
83+
// In apply mode, any lints should fail the build (matching Gradle behavior)
8484
if (hasUnsuppressedLints) {
85-
// Check if any lint represents a fatal error (like parsing failures)
86-
boolean hasFatalError = lintState.getLintsByStep(formatter).values().stream()
87-
.flatMap(List::stream)
88-
.anyMatch(lint -> lint.getShortCode().contains("Exception"));
89-
90-
if (hasFatalError) {
91-
String stepName = lintState.getLintsByStep(formatter).keySet().iterator().next();
92-
throw new MojoExecutionException(String.format("Unable to format file %s%nStep '%s' found problem in '%s':%n%s", file, stepName, file.getName(), lintState.asStringDetailed(file, formatter)));
93-
} else {
94-
getLog().warn(String.format("File %s has lint issues that cannot be auto-fixed:%n%s", file, lintState.asStringDetailed(file, formatter)));
85+
int lintCount = lintState.getLintsByStep(formatter).values().stream()
86+
.mapToInt(List::size)
87+
.sum();
88+
StringBuilder message = new StringBuilder();
89+
message.append("There were ").append(lintCount).append(" lint error(s), they must be fixed or suppressed.");
90+
91+
// Build lint messages in Gradle format (using relative path, not just filename)
92+
for (Map.Entry<String, List<com.diffplug.spotless.Lint>> stepEntry : lintState.getLintsByStep(formatter).entrySet()) {
93+
String stepName = stepEntry.getKey();
94+
for (com.diffplug.spotless.Lint lint : stepEntry.getValue()) {
95+
message.append("\n ").append(relativePath).append(":");
96+
lint.addWarningMessageTo(message, stepName, true);
97+
}
9598
}
99+
message.append("\n Resolve these lints or suppress with `suppressLintsFor`");
100+
throw new MojoExecutionException(message.toString());
96101
}
97102
} catch (IOException | RuntimeException e) {
98103
throw new MojoExecutionException("Unable to format file " + file, e);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,21 @@ protected void process(String name, Iterable<File> files, Formatter formatter, U
117117
getLog().debug(String.format("Spotless.%s has no target files. Examine your `<includes>`: https://github.com/diffplug/spotless/tree/main/plugin-maven#quickstart", name));
118118
}
119119

120-
if (!lintProblems.isEmpty()) {
121-
// If we have lint problems, prioritize showing them with detailed messages
122-
Map.Entry<File, LintState> firstLintProblem = lintProblems.get(0);
123-
File file = firstLintProblem.getKey();
124-
LintState lintState = firstLintProblem.getValue();
125-
String stepName = lintState.getLintsByStep(formatter).keySet().iterator().next();
126-
throw new MojoExecutionException(String.format("Unable to format file %s%nStep '%s' found problem in '%s':%n%s",
127-
file, stepName, file.getName(), lintState.asStringDetailed(file, formatter)));
128-
} else if (!problemFiles.isEmpty()) {
120+
if (!problemFiles.isEmpty()) {
121+
// Prioritize formatting violations first (matching Gradle behavior)
129122
throw new MojoExecutionException(DiffMessageFormatter.builder()
130123
.runToFix("Run 'mvn spotless:apply' to fix these violations.")
131124
.formatter(baseDir.toPath(), formatter)
132125
.problemFiles(problemFiles)
133126
.getMessage());
127+
} else if (!lintProblems.isEmpty()) {
128+
// Show lints only if there are no formatting violations
129+
Map.Entry<File, LintState> firstLintProblem = lintProblems.get(0);
130+
File file = firstLintProblem.getKey();
131+
LintState lintState = firstLintProblem.getValue();
132+
String stepName = lintState.getLintsByStep(formatter).keySet().iterator().next();
133+
throw new MojoExecutionException(String.format("Unable to format file %s%nStep '%s' found problem in '%s':%n%s",
134+
file, stepName, file.getName(), lintState.asStringOneLine(file, formatter)));
134135
}
135136
}
136137
}

0 commit comments

Comments
 (0)