Skip to content

Commit df5c5b1

Browse files
lutovichnedtwigg
authored andcommitted
No license headers for package-info and module-info in Maven (#273)
* 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
1 parent b498ef0 commit df5c5b1

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### 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/))
44

5+
* Skip `package-info.java` and `module-info.java` files from license header formatting. ([#273](https://github.com/diffplug/spotless/pull/273))
6+
57
### 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))
68

79
* 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)).

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020
import org.apache.maven.plugins.annotations.Parameter;
2121

2222
import com.diffplug.spotless.FormatterStep;
23+
import com.diffplug.spotless.SerializableFileFilter;
2324
import com.diffplug.spotless.generic.LicenseHeaderStep;
2425
import com.diffplug.spotless.maven.FormatterStepConfig;
2526
import com.diffplug.spotless.maven.FormatterStepFactory;
2627

2728
public class LicenseHeader implements FormatterStepFactory {
29+
30+
private static final SerializableFileFilter UNSUPPORTED_FILES_FILTER = SerializableFileFilter.skipFilesNamed(
31+
"package-info.java", "module-info.java");
32+
2833
@Parameter
2934
private String file;
3035

@@ -42,14 +47,22 @@ public final FormatterStep newFormatterStep(FormatterStepConfig config) {
4247
}
4348

4449
if (file != null ^ content != null) {
45-
if (file != null) {
46-
File licenseHeaderFile = config.getFileLocator().locateFile(file);
47-
return LicenseHeaderStep.createFromFile(licenseHeaderFile, config.getEncoding(), delimiterString);
48-
} else {
49-
return LicenseHeaderStep.createFromHeader(content, delimiterString);
50-
}
50+
FormatterStep step = file != null
51+
? createStepFromFile(config, delimiterString)
52+
: createStepFromContent(delimiterString);
53+
54+
return step.filterByFile(UNSUPPORTED_FILES_FILTER);
5155
} else {
5256
throw new IllegalArgumentException("Must specify exactly one of 'file' or 'content'.");
5357
}
5458
}
59+
60+
private FormatterStep createStepFromFile(FormatterStepConfig config, String delimiterString) {
61+
File licenseHeaderFile = config.getFileLocator().locateFile(file);
62+
return LicenseHeaderStep.createFromFile(licenseHeaderFile, config.getEncoding(), delimiterString);
63+
}
64+
65+
private FormatterStep createStepFromContent(String delimiterString) {
66+
return LicenseHeaderStep.createFromHeader(content, delimiterString);
67+
}
5568
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,37 @@ public void fromContentKotlin() throws Exception {
9797
assertFile(path).hasContent(KOTLIN_LICENSE_HEADER + '\n' + noLicenseHeader);
9898
}
9999

100+
@Test
101+
public void unsupportedPackageInfo() throws Exception {
102+
testUnsupportedFile("package-info.java");
103+
}
104+
105+
@Test
106+
public void unsupportedModuleInfo() throws Exception {
107+
testUnsupportedFile("module-info.java");
108+
}
109+
100110
private void runTest() throws Exception {
101111
String path = "src/main/java/test.java";
102112
setFile(path).toResource("license/MissingLicense.test");
103113
mavenRunner().withArguments("spotless:apply").runNoError();
104114
assertFile(path).sameAsResource("license/HasLicense.test");
105115
}
116+
117+
private void testUnsupportedFile(String file) throws Exception {
118+
writePomWithJavaSteps(
119+
"<licenseHeader>",
120+
" <content>",
121+
"// Hello!",
122+
" </content>",
123+
"</licenseHeader>");
124+
125+
String path = "src/main/java/com/diffplug/spotless/" + file;
126+
setFile(path).toResource("license/" + file);
127+
128+
mavenRunner().withArguments("spotless:apply").runNoError();
129+
130+
// file should remain the same
131+
assertFile(path).sameAsResource("license/" + file);
132+
}
106133
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module com.diffplug.spotless {
2+
3+
requires com.diffplug.durian;
4+
5+
exports com.diffplug.spotless;
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Test package-info.java file.
3+
* <p>
4+
* Used only by integration tests.
5+
*
6+
* @since 1.0
7+
*/
8+
package com.diffplug.spotless;

0 commit comments

Comments
 (0)