Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a property to limit plugin to a list of files #876

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=license:format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a
mock license
34 changes: 34 additions & 0 deletions license-maven-plugin/src/it/legacy-config-checkFiles/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycila.license-maven-plugin.it</groupId>
<artifactId>legacy-config</artifactId>
<version>1.0.0</version>

<name>Check Legacy Configuration with checkFiles</name>
<description>Integration Test for checking legacy configuration with checkFiles</description>

<build>
<plugins>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<header>mock-license.txt</header>
<excludes>
<exclude>invoker.properties</exclude>
<exclude>pom.xml</exclude>
<exclude>*.groovy</exclude>
<exclude>**/*.bak</exclude>
<exclude>*.log</exclude>
</excludes>
<filesToCheck>src/main/java/com/mycilla/it/Unformatted2.java</filesToCheck>
</configuration>
</plugin>
</plugins>
</build>

</project>
23 changes: 23 additions & 0 deletions license-maven-plugin/src/it/legacy-config-checkFiles/setup.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.nio.file.Files
import java.nio.file.Path

final Path base = basedir.toPath()

if (!Files.exists(base) || !Files.isDirectory(base)) {
System.err.println("base directory is missing.")
return false
}

final List<String> ALL_FILES = Arrays.asList("Unformatted1.java", "Unformatted2.java")

for (final String filename : ALL_FILES) {
final Path unformattedJavaFile = base.resolve("src/main/java/com/mycilla/it/" + filename)
if (!Files.exists(unformattedJavaFile)) {
System.err.println(filename + " file is missing.")
return false
}

Files.copy(unformattedJavaFile, unformattedJavaFile.resolveSibling(filename + ".bak"))
}

return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mycila.it;

public class Unformatted1 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mycila.it;

public class Unformatted2 {
}
61 changes: 61 additions & 0 deletions license-maven-plugin/src/it/legacy-config-checkFiles/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.nio.file.Files
import java.nio.file.Path

import static org.junit.jupiter.api.Assertions.assertEquals


final Path base = basedir.toPath()

if (!Files.exists(base) || !Files.isDirectory(base)) {
System.err.println("base directory is missing.")
return false
}

final Path license = base.resolve("mock-license.txt")
if (!Files.exists(license)) {
System.err.println("license file is missing.")
return false
}

final Path unformattedJavaFile1 = base.resolve("src/main/java/com/mycilla/it/Unformatted1.java.bak")
if (!Files.exists(unformattedJavaFile1)) {
System.err.println(unformattedJavaFile1.getFileName() + " file is missing (should have been created by setup.groovy).")
return false
}

final Path unformattedJavaFile2 = base.resolve("src/main/java/com/mycilla/it/Unformatted2.java.bak")
if (!Files.exists(unformattedJavaFile2)) {
System.err.println(unformattedJavaFile2.getFileName() + " file is missing (should have been created by setup.groovy).")
return false
}

final Path formattedJavaFile1 = base.resolve("src/main/java/com/mycilla/it/Unformatted1.java")
if (!Files.exists(formattedJavaFile1)) {
System.err.println(formattedJavaFile1.getFileName() + " file is missing.")
return false
}


final Path formattedJavaFile2 = base.resolve("src/main/java/com/mycilla/it/Unformatted2.java")
if (!Files.exists(formattedJavaFile2)) {
System.err.println(formattedJavaFile2.getFileName() + " file is missing.")
return false
}

assertEquals(new String(Files.readAllBytes(formattedJavaFile1)), new String(Files.readAllBytes(unformattedJavaFile1)))

final StringBuilder expected = new StringBuilder();
expected.append("/*\n");
license.withReader { reader ->
while ((line = reader.readLine()) != null) {
expected.append(" * ").append(line).append('\n')
}
}
expected.append(" */\n")
expected.append(new String(Files.readAllBytes(unformattedJavaFile2)))

final String actual = new String(Files.readAllBytes(formattedJavaFile2))

assertEquals(expected.toString(), actual)

return true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=license:format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is the 1st
mock license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is the 2nd
mock license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is the 3rd
mock license
60 changes: 60 additions & 0 deletions license-maven-plugin/src/it/tri-license-set-checkFiles/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycila.license-maven-plugin.it</groupId>
<artifactId>tri-license-set</artifactId>
<version>1.0.0</version>

<name>Check a Triple License Set</name>
<description>Integration Test for checking a triple license set</description>

<build>
<plugins>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<filesToCheck>src/main/java/com/mycilla/it/Unformatted2.java</filesToCheck>
<licenseSets>
<licenseSet>
<header>mock-license-1.txt</header>
<excludes>
<exclude>invoker.properties</exclude>
<exclude>pom.xml</exclude>
<exclude>*.groovy</exclude>
<exclude>**/*.bak</exclude>
<exclude>*.log</exclude>
<exclude>mock-license-*</exclude>
<exclude>**/Unformatted2.java</exclude>
<exclude>**/Unformatted3.java</exclude>
</excludes>
</licenseSet>
<licenseSet>
<header>mock-license-2.txt</header>
<excludes>
<exclude>invoker.properties</exclude>
<exclude>pom.xml</exclude>
<exclude>*.groovy</exclude>
<exclude>**/*.bak</exclude>
<exclude>*.log</exclude>
<exclude>mock-license-*</exclude>
<exclude>**/Unformatted1.java</exclude>
<exclude>**/Unformatted3.java</exclude>
</excludes>
</licenseSet>
<licenseSet>
<header>mock-license-3.txt</header>
<includes>
<include>**/Unformatted3.java</include>
</includes>
</licenseSet>
</licenseSets>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.nio.file.Files
import java.nio.file.Path

final Path base = basedir.toPath()

if (!Files.exists(base) || !Files.isDirectory(base)) {
System.err.println("base directory is missing.")
return false
}

final List<String> ALL_FILES = Arrays.asList("Unformatted1.java", "Unformatted2.java", "Unformatted3.java")

for (final String filename : ALL_FILES) {
final Path unformattedJavaFile = base.resolve("src/main/java/com/mycilla/it/" + filename)
if (!Files.exists(unformattedJavaFile)) {
System.err.println(filename + " file is missing.")
return false
}

Files.copy(unformattedJavaFile, unformattedJavaFile.resolveSibling(filename + ".bak"))
}

return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mycila.it;

public class Unformatted1 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mycila.it;

public class Unformatted2 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mycila.it;

public class Unformatted3 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.nio.file.Files
import java.nio.file.Path

import static org.junit.jupiter.api.Assertions.assertEquals


final Path base = basedir.toPath()

if (!Files.exists(base) || !Files.isDirectory(base)) {
System.err.println("base directory is missing.")
return false
}

for (int i = 1; i <= 3; i++) {

final Path license = base.resolve("mock-license-" + i + ".txt")
if (!Files.exists(license)) {
System.err.println(license.getFileName() + " file is missing.")
return false
}

final String filename = "Unformatted" + i + ".java"

final Path unformattedJavaFile = base.resolve("src/main/java/com/mycilla/it/" + filename + ".bak")
if (!Files.exists(unformattedJavaFile)) {
System.err.println(unformattedJavaFile.getFileName() + " file is missing (should have been created by setup.groovy).")
return false
}

final Path formattedJavaFile = base.resolve("src/main/java/com/mycilla/it/" + filename)
if (!Files.exists(formattedJavaFile)) {
System.err.println(formattedJavaFile.getFileName() + " file is missing.")
return false
}

final StringBuilder expected = new StringBuilder();
if (i == 2) {
expected.append("/*\n");
license.withReader { reader ->
while ((line = reader.readLine()) != null) {
expected.append(" * ").append(line).append('\n')
}
}
expected.append(" */\n")
}
expected.append(new String(Files.readAllBytes(unformattedJavaFile)))

final String actual = new String(Files.readAllBytes(formattedJavaFile))

assertEquals(expected.toString(), actual)
}

return true
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
@Parameter(alias = "properties")
public Map<String, String> defaultProperties = new HashMap<>();

/**
* Specifies files, which files are to check. By default, all files
* are included.
*
*/
@Parameter(alias = "filesToCheck", property = "license.filesToCheck")
public String[] filesToCheck = new String[0];


/**
* Specifies files, which are included in the check. By default, all files
* are included.
Expand Down Expand Up @@ -786,14 +795,22 @@ private Map<String, String> getDefaultProperties() {
}

private String[] listSelectedFiles(final LicenseSet licenseSet) {
final boolean useDefaultExcludes = (licenseSet.useDefaultExcludes != null ? licenseSet.useDefaultExcludes : defaultUseDefaultExcludes);
final Selection selection = new Selection(
firstNonNull(licenseSet.basedir, defaultBasedir), licenseSet.includes, buildExcludes(licenseSet), useDefaultExcludes,
getLog());
debug("From: %s", firstNonNull(licenseSet.basedir, defaultBasedir));
debug("Including: %s", deepToString(selection.getIncluded()));
debug("Excluding: %s", deepToString(selection.getExcluded()));
return selection.getSelectedFiles();
final boolean useDefaultExcludes = (licenseSet.useDefaultExcludes != null ? licenseSet.useDefaultExcludes
: defaultUseDefaultExcludes);
if (filesToCheck == null || filesToCheck.length == 0) {
final Selection selection = new Selection(firstNonNull(licenseSet.basedir, defaultBasedir), licenseSet.includes, buildExcludes(licenseSet),
useDefaultExcludes, getLog());
debug("From: %s", firstNonNull(licenseSet.basedir, defaultBasedir));
debug("Including: %s", deepToString(selection.getIncluded()));
debug("Excluding: %s", deepToString(selection.getExcluded()));
return selection.getSelectedFiles();
} else {
final Selection selection = new Selection(filesToCheck, licenseSet.includes, buildExcludes(licenseSet),
useDefaultExcludes, getLog());
debug("Including: %s", deepToString(selection.getIncluded()));
debug("Excluding: %s", deepToString(selection.getExcluded()));
return selection.getSelectedFiles();
}
}

private String[] buildExcludes(final LicenseSet licenseSet) {
Expand Down
Loading