diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 6a8edb2988..6fd011c458 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -2,6 +2,8 @@ ### Version 1.15.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/)) +* Skip `package-info.java` and `module-info.java` files from license header formatting. ([#273](https://github.com/diffplug/spotless/pull/273)) + ### Version 1.14.0 - July 24th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.14.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.14.0)) * Updated default eclipse-jdt from 4.7.2 to 4.7.3a ([#263](https://github.com/diffplug/spotless/issues/263)). New version fixes a bug preventing Java code formatting within JavaDoc comments ([#191](https://github.com/diffplug/spotless/issues/191)). diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java index 028ebc46cd..4b885a4ae9 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java @@ -20,11 +20,16 @@ import org.apache.maven.plugins.annotations.Parameter; import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.SerializableFileFilter; import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.maven.FormatterStepConfig; import com.diffplug.spotless.maven.FormatterStepFactory; public class LicenseHeader implements FormatterStepFactory { + + private static final SerializableFileFilter UNSUPPORTED_FILES_FILTER = SerializableFileFilter.skipFilesNamed( + "package-info.java", "module-info.java"); + @Parameter private String file; @@ -42,14 +47,22 @@ public final FormatterStep newFormatterStep(FormatterStepConfig config) { } if (file != null ^ content != null) { - if (file != null) { - File licenseHeaderFile = config.getFileLocator().locateFile(file); - return LicenseHeaderStep.createFromFile(licenseHeaderFile, config.getEncoding(), delimiterString); - } else { - return LicenseHeaderStep.createFromHeader(content, delimiterString); - } + FormatterStep step = file != null + ? createStepFromFile(config, delimiterString) + : createStepFromContent(delimiterString); + + return step.filterByFile(UNSUPPORTED_FILES_FILTER); } else { throw new IllegalArgumentException("Must specify exactly one of 'file' or 'content'."); } } + + private FormatterStep createStepFromFile(FormatterStepConfig config, String delimiterString) { + File licenseHeaderFile = config.getFileLocator().locateFile(file); + return LicenseHeaderStep.createFromFile(licenseHeaderFile, config.getEncoding(), delimiterString); + } + + private FormatterStep createStepFromContent(String delimiterString) { + return LicenseHeaderStep.createFromHeader(content, delimiterString); + } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java index 5110f84dc6..41ba37ed78 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java @@ -97,10 +97,37 @@ public void fromContentKotlin() throws Exception { assertFile(path).hasContent(KOTLIN_LICENSE_HEADER + '\n' + noLicenseHeader); } + @Test + public void unsupportedPackageInfo() throws Exception { + testUnsupportedFile("package-info.java"); + } + + @Test + public void unsupportedModuleInfo() throws Exception { + testUnsupportedFile("module-info.java"); + } + private void runTest() throws Exception { String path = "src/main/java/test.java"; setFile(path).toResource("license/MissingLicense.test"); mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("license/HasLicense.test"); } + + private void testUnsupportedFile(String file) throws Exception { + writePomWithJavaSteps( + "", + " ", + "// Hello!", + " ", + ""); + + String path = "src/main/java/com/diffplug/spotless/" + file; + setFile(path).toResource("license/" + file); + + mavenRunner().withArguments("spotless:apply").runNoError(); + + // file should remain the same + assertFile(path).sameAsResource("license/" + file); + } } diff --git a/testlib/src/main/resources/license/module-info.java b/testlib/src/main/resources/license/module-info.java new file mode 100644 index 0000000000..ef73d122db --- /dev/null +++ b/testlib/src/main/resources/license/module-info.java @@ -0,0 +1,6 @@ +module com.diffplug.spotless { + + requires com.diffplug.durian; + + exports com.diffplug.spotless; +} diff --git a/testlib/src/main/resources/license/package-info.java b/testlib/src/main/resources/license/package-info.java new file mode 100644 index 0000000000..eb6821a8db --- /dev/null +++ b/testlib/src/main/resources/license/package-info.java @@ -0,0 +1,8 @@ +/** + * Test package-info.java file. + *

+ * Used only by integration tests. + * + * @since 1.0 + */ +package com.diffplug.spotless;