From d15cfce03b7fc3dba680f656f6068a89495492e9 Mon Sep 17 00:00:00 2001 From: lutovich Date: Tue, 1 May 2018 22:06:52 +0200 Subject: [PATCH 1/2] Support multiple formats in Maven plugin Format is a configuration element that makes it possible to apply generic formatting steps (EndWithNewline, LicenseHeader, etc.) to a set of files defined by custom includes and excludes. Example of format configuration: ``` src/**/txt/**/*.txt src/**/txt/**/*Wrong.txt // License Text start ``` It is currently possible to only define a single `` element in plugin configuration. This commit makes it possible to define multiple formats. Format configuration structure remains the same but all formats should be enclosed in `...`. Example: ``` ...format 1 includes, excludes and steps ...format 2 includes, excludes and steps ...format 3 includes, excludes and steps ``` --- plugin-maven/README.md | 95 ++++++++++--------- .../spotless/maven/AbstractSpotlessMojo.java | 9 +- .../spotless/maven/MavenIntegrationTest.java | 14 +-- .../spotless/maven/MultipleFormatsTest.java | 79 +++++++++++++++ 4 files changed, 140 insertions(+), 57 deletions(-) create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/MultipleFormatsTest.java diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 751a46c47f..b64ab1b621 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -168,53 +168,60 @@ By default, all files matching `src/main/kotlin/**/*.kt` and `src/test/kotlin/** ## Applying to custom sources -By default, no Ant-Style include patterns are defined. Each element under `` is a step, and they will be applied in the order specified. Every step is optional, and they will be applied in the order specified. +By default, no Ant-Style include patterns are defined. Each element under `` is a step, and they will be applied in the order specified. Every step is optional, and they will be applied in the order specified. It is possible to define multiple custom formats. ```xml - - - - src/**/resources/**/*.properties - - - - - /* Licensed under Apache-2.0 */ - ${basedir}/license-header - - # - - - - - - - - - true - true - - 4 - - - - - - - - Say Hello to Mars - World - Mars - - - - - Say Hello to Mars from Regex - (Hello) W[a-z]{3}d - $1 Mars - - + + + + + + + src/**/resources/**/*.properties + + + + + /* Licensed under Apache-2.0 */ + ${basedir}/license-header + + # + + + + + + + + + true + true + + 4 + + + + + + + + Say Hello to Mars + World + Mars + + + + + Say Hello to Mars from Regex + (Hello) W[a-z]{3}d + $1 Mars + + + + + + ``` diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index e83f871e63..8dccb5d63f 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -19,10 +19,7 @@ import java.io.File; import java.io.IOException; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.stream.Stream; import org.apache.maven.plugin.AbstractMojo; @@ -78,7 +75,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { private LicenseHeader licenseHeader; @Parameter - private Format format; + private List formats = Collections.emptyList(); @Parameter private Java java; @@ -146,7 +143,7 @@ private FileLocator getFileLocator() { } private List getFormatterFactories() { - return Stream.of(format, java, scala, kotlin) + return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin)) .filter(Objects::nonNull) .collect(toList()); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java index d5fcfcb4f8..4829fda1d7 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java @@ -87,7 +87,7 @@ private File copy(String path) throws IOException { } protected void writePomWithFormatSteps(String... steps) throws IOException { - writePom(groupWithSteps("format", including("src/**/java/**/*.java"), steps)); + writePom(formats(groupWithSteps("format", including("src/**/java/**/*.java"), steps))); } protected void writePomWithJavaSteps(String... steps) throws IOException { @@ -177,12 +177,12 @@ private static String[] groupWithSteps(String group, String... steps) { return groupWithSteps(group, new String[]{}, steps); } - private static String[] including(String... include) { - String[] result = new String[include.length + 2]; - result[0] = ""; - System.arraycopy(include, 0, result, 1, include.length); - result[result.length - 1] = ""; - return result; + private static String[] including(String... includes) { + return groupWithSteps("includes", includes); + } + + private static String[] formats(String... formats) { + return groupWithSteps("formats", formats); } protected class MultiModuleProjectCreator { diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultipleFormatsTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultipleFormatsTest.java new file mode 100644 index 0000000000..fe758825e6 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultipleFormatsTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven; + +import org.junit.Test; + +public class MultipleFormatsTest extends MavenIntegrationTest { + + @Test + public void testMultipleFormatsWithDifferentIncludes() throws Exception { + writePom( + "", + " ", + " ", + " src/**/java/**/*.java", + " ", + " ", + " Greetings to Mars", + " World", + " Mars", + " ", + " ", + " // License Header #1", + " package", + " ", + " ", + " ", + " ", + " src/**/txt/**/*.txt", + " ", + " ", + " Greetings to Titan", + " World", + " Titan", + " ", + " ", + " // License Header #2", + " Just", + " ", + " ", + ""); + + String path1 = "src/main/java/test1.java"; + String path2 = "src/main/java/test2.java"; + + String path3 = "src/main/txt/test1.txt"; + String path4 = "src/main/txt/test2.txt"; + String path5 = "src/main/txt/test3.txt"; + + setFile(path1).toContent("package test;\npublic class JavaWorld1 {}"); + setFile(path2).toContent("package test;\npublic class JavaWorld2 {}"); + + setFile(path3).toContent("Just a text file #1\nHello World!"); + setFile(path4).toContent("Just a text file #2\nHello World!"); + setFile(path5).toContent("Just a text file #3\nHello World!"); + + mavenRunner().withArguments("spotless:apply").runNoError(); + + assertFile(path1).hasContent("// License Header #1\npackage test;\npublic class JavaMars1 {}"); + assertFile(path2).hasContent("// License Header #1\npackage test;\npublic class JavaMars2 {}"); + + assertFile(path3).hasContent("// License Header #2\nJust a text file #1\nHello Titan!"); + assertFile(path4).hasContent("// License Header #2\nJust a text file #2\nHello Titan!"); + assertFile(path5).hasContent("// License Header #2\nJust a text file #3\nHello Titan!"); + } +} From 2b70d4752f1e44aab4c4387acbe575019e871ec4 Mon Sep 17 00:00:00 2001 From: lutovich Date: Tue, 1 May 2018 22:13:20 +0200 Subject: [PATCH 2/2] Update changelog for Maven plugin --- plugin-maven/CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 6a83ec14f5..5dc5a55239 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,6 +5,7 @@ * Fixed a bug in `LicenseHeaderStep` which caused an exception with some malformed date-aware licenses. ([#222](https://github.com/diffplug/spotless/pull/222)) * Added support for Kotlin and Ktlint in Maven plugin ([#223](https://github.com/diffplug/spotless/pull/223)). * Updated default ktlint from 0.14.0 to 0.21.0 +* Added support for multiple generic formatters in Maven plugin ([#242](https://github.com/diffplug/spotless/pull/242)). ### Version 1.0.0.BETA4 - February 27th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.0.0.BETA4/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.0.0.BETA4))