Skip to content

Commit

Permalink
No license headers for package-info and module-info in Maven (#273)
Browse files Browse the repository at this point in the history
* No license headers for package-info and module-info in Maven

These two files are currently not supported by the license header
step and might get formatted incorrectly.

This commit makes Maven plugin exclude both `package-info.java`
and `module-info.java`.

* Update changelog
  • Loading branch information
lutovich authored and nedtwigg committed Aug 1, 2018
1 parent b498ef0 commit df5c5b1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<licenseHeader>",
" <content>",
"// Hello!",
" </content>",
"</licenseHeader>");

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);
}
}
6 changes: 6 additions & 0 deletions testlib/src/main/resources/license/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module com.diffplug.spotless {

requires com.diffplug.durian;

exports com.diffplug.spotless;
}
8 changes: 8 additions & 0 deletions testlib/src/main/resources/license/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Test package-info.java file.
* <p>
* Used only by integration tests.
*
* @since 1.0
*/
package com.diffplug.spotless;

0 comments on commit df5c5b1

Please sign in to comment.