Skip to content

Commit

Permalink
Merge pull request #147 from mycila/issue/143
Browse files Browse the repository at this point in the history
Fix #143: Add ability to override a default exclusion to include a pattern from the latter
  • Loading branch information
mathieucarbou authored Feb 6, 2019
2 parents 2fa21cc + 413e36c commit 3ad24c4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.Arrays.*;
import static java.util.Arrays.asList;

/**
Expand All @@ -38,8 +40,9 @@ public final class Selection {

public Selection(File basedir, String[] included, String[] excluded, boolean useDefaultExcludes) {
this.basedir = basedir;
this.included = buildInclusions(included);
this.excluded = buildExclusions(useDefaultExcludes, excluded);
String[] overrides = buildOverrideInclusions(useDefaultExcludes, included);
this.included = buildInclusions(included, overrides);
this.excluded = buildExclusions(useDefaultExcludes, excluded, overrides);
}

public String[] getSelectedFiles() {
Expand Down Expand Up @@ -69,17 +72,39 @@ private void scanIfneeded() {
}
}

private static String[] buildExclusions(boolean useDefaultExcludes, String... excludes) {
private static String[] buildExclusions(boolean useDefaultExcludes, String[] excludes, String[] overrides) {
List<String> exclusions = new ArrayList<String>();
if (useDefaultExcludes)
if (useDefaultExcludes) {
exclusions.addAll(asList(Default.EXCLUDES));
if (excludes != null && excludes.length > 0)
}
// Remove from the default exclusion list the patterns that have been explicitly included
for (String override : overrides) {
exclusions.remove(override);
}
if (excludes != null && excludes.length > 0) {
exclusions.addAll(asList(excludes));
}
return exclusions.toArray(new String[exclusions.size()]);
}

private static String[] buildInclusions(String... includes) {
return includes != null && includes.length > 0 ? includes : Default.INCLUDE;
private static String[] buildInclusions(String[] includes, String[] overrides) {
// if we use the default exclusion list, we just remove
List<String> inclusions = new ArrayList<String>(asList(includes != null && includes.length > 0 ? includes : Default.INCLUDE));
inclusions.removeAll(asList(overrides));
if (inclusions.isEmpty()) {
inclusions.addAll(asList(Default.INCLUDE));
}
return inclusions.toArray(new String[inclusions.size()]);
}

private static String[] buildOverrideInclusions(boolean useDefaultExcludes, String[] includes) {
// return the list of patterns that we have explicitly included when using default exclude list
if (!useDefaultExcludes || includes == null || includes.length == 0) {
return new String[0];
}
List<String> overrides = new ArrayList<String>(asList(Default.EXCLUDES));
overrides.retainAll(asList(includes));
return overrides.toArray(new String[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.maven.plugin.MojoFailureException;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
Expand Down Expand Up @@ -62,7 +63,12 @@ public void setPluginClassPath(ClassLoader classLoader) {
*/
public URL findResource(String resource) throws MojoFailureException {
// first search relatively to the base directory
URL res = toURL(new File(basedir, resource));
URL res ;
try {
res = toURL(new File(basedir, resource).getCanonicalFile());
} catch (IOException e) {
throw new MojoFailureException("Resource " + resource + " not found in file system, classpath or URL: " + e.getMessage(), e);
}
if (res != null) {
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ public void test_include_and_fail() throws Exception {
check.execute();
}

@Test(expected = MojoExecutionException.class)
public void test_include_overrides_default_exclusion() throws Exception {
LicenseCheckMojo check = new LicenseCheckMojo();
check.basedir = new File("src/test/resources/issues/issue-71");
check.header = "../../check/header.txt";
check.project = new MavenProjectStub();
check.includes = new String[]{"**/.travis.yml"};
check.execute();
}

}
Empty file.

0 comments on commit 3ad24c4

Please sign in to comment.