Enhance JarCommand to support lists of includes and excludes

The lists are comma separated. In addition, user can add prefixes
"+" or "-", to signal that those values should be removed from the
default list, not added to a fresh one. E.g.

$ spring jar app.jar --include lib/*.jar,-static/** --exclude -**/*.jar

to include a jar file specifically, and make sure it is not excluded,
and additionally not include the static/** resources that would otherwise
be included in the defaults. As soon as "+" or "-" prefixes are detected
the default entries are all added (except the ones exlcuded with "-").

Fixes gh-1090
This commit is contained in:
Dave Syer
2014-06-12 18:08:33 +01:00
parent d842446186
commit 2c691e5ae5
6 changed files with 142 additions and 20 deletions

View File

@@ -169,7 +169,9 @@ public class CommandRunner implements Iterable<Command> {
try {
ExitStatus result = run(argsWithoutDebugFlags);
// The caller will hang up if it gets a non-zero status
return result==null ? 0 : result.isHangup() ? (result.getCode()>0 ? result.getCode() : 1) : 0;
return result == null ? 0
: result.isHangup() ? (result.getCode() > 0 ? result.getCode() : 0)
: 0;
}
catch (NoArgumentsException ex) {
showUsage();

View File

@@ -68,12 +68,6 @@ import org.springframework.util.Assert;
*/
public class JarCommand extends OptionParsingCommand {
private static final String[] DEFAULT_INCLUDES = { "public/**", "resources/**",
"static/**", "templates/**", "META-INF/**", "*" };
private static final String[] DEFAULT_EXCLUDES = { ".*", "repository/**", "build/**",
"target/**", "**/*.jar", "**/*.groovy" };
private static final Layout LAYOUT = new Layouts.Jar();
public JarCommand() {
@@ -98,11 +92,11 @@ public class JarCommand extends OptionParsingCommand {
this.includeOption = option(
"include",
"Pattern applied to directories on the classpath to find files to include in the resulting jar")
.withRequiredArg().defaultsTo(DEFAULT_INCLUDES);
.withRequiredArg().withValuesSeparatedBy(",").defaultsTo("");
this.excludeOption = option(
"exclude",
"Pattern applied to directories on the claspath to find files to exclude from the resulting jar")
.withRequiredArg().defaultsTo(DEFAULT_EXCLUDES);
.withRequiredArg().withValuesSeparatedBy(",").defaultsTo("");
}
@Override

View File

@@ -23,7 +23,9 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResource;
@@ -39,6 +41,12 @@ import org.springframework.util.AntPathMatcher;
*/
class ResourceMatcher {
private static final String[] DEFAULT_INCLUDES = { "public/**", "resources/**",
"static/**", "templates/**", "META-INF/**", "*" };
private static final String[] DEFAULT_EXCLUDES = { ".*", "repository/**", "build/**",
"target/**", "**/*.jar", "**/*.groovy" };
private final AntPathMatcher pathMatcher = new AntPathMatcher();
private final List<String> includes;
@@ -46,8 +54,8 @@ class ResourceMatcher {
private final List<String> excludes;
ResourceMatcher(List<String> includes, List<String> excludes) {
this.includes = includes;
this.excludes = excludes;
this.includes = getOptions(includes, DEFAULT_INCLUDES);
this.excludes = getOptions(excludes, DEFAULT_EXCLUDES);
}
public List<MatchedResource> find(List<File> roots) throws IOException {
@@ -93,6 +101,33 @@ class ResourceMatcher {
return false;
}
private List<String> getOptions(List<String> values, String[] defaults) {
Set<String> result = new LinkedHashSet<String>();
Set<String> minus = new LinkedHashSet<String>();
boolean deltasFound = false;
for (String value : values) {
if (value.startsWith("+")) {
deltasFound = true;
value = value.substring(1);
result.add(value);
}
else if (value.startsWith("-")) {
deltasFound = true;
value = value.substring(1);
minus.add(value);
}
else if (value.trim().length() > 0) {
result.add(value);
}
}
for (String value : defaults) {
if (!minus.contains(value) || !deltasFound) {
result.add(value);
}
}
return new ArrayList<String>(result);
}
/**
* {@link ResourceLoader} to get load resource from a folder.
*/