|
31 | 31 | import java.util.List;
|
32 | 32 | import java.util.Map;
|
33 | 33 | import java.util.Properties;
|
| 34 | +import java.util.regex.Pattern; |
34 | 35 | import java.util.stream.Collectors;
|
35 | 36 |
|
36 | 37 | import org.apache.maven.plugin.AbstractMojo;
|
@@ -89,6 +90,8 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {
|
89 | 90 | private static final String[] DEFAULT_INCLUDES = new String[] { "**/*.css", "**/*.json", "**/*.html", "**/*.java",
|
90 | 91 | "**/*.js", "**/*.xml" };
|
91 | 92 |
|
| 93 | + private static final Pattern REMOVE_TRAILING_PATTERN = Pattern.compile("\\p{Blank}+$", Pattern.MULTILINE); |
| 94 | + |
92 | 95 | /**
|
93 | 96 | * ResourceManager for retrieving the configFile resource.
|
94 | 97 | */
|
@@ -328,6 +331,13 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {
|
328 | 331 | @Parameter(property = "formatter.java.exclusion_pattern")
|
329 | 332 | private String javaExclusionPattern;
|
330 | 333 |
|
| 334 | + /** |
| 335 | + * When set to true, remove trailing whitespace on all lines after the formatter has finished. |
| 336 | + * |
| 337 | + * @since 2.17.0 |
| 338 | + */ |
| 339 | + @Parameter(defaultValue = "false", property = "formatter.removeTrailingWhitespace") |
| 340 | + private boolean removeTrailingWhitespace; |
331 | 341 | private JavaFormatter javaFormatter = new JavaFormatter();
|
332 | 342 |
|
333 | 343 | private JavascriptFormatter jsFormatter = new JavascriptFormatter();
|
@@ -652,6 +662,11 @@ protected void doFormatFile(File file, ResultCollector rc, Properties hashCache,
|
652 | 662 | return;
|
653 | 663 | }
|
654 | 664 |
|
| 665 | + // Process the source one more time and remove any trailing whitespace found |
| 666 | + if (removeTrailingWhitespace && formattedCode != null) { |
| 667 | + formattedCode = REMOVE_TRAILING_PATTERN.matcher(formattedCode).replaceAll(""); |
| 668 | + } |
| 669 | + |
655 | 670 | // Write the cache
|
656 | 671 | String formattedHash;
|
657 | 672 | if (Result.SKIPPED.equals(result)) {
|
|
0 commit comments