Add support for @GrabExclude to AetherGrapeEngine

@GrabExclude can now be used to exclude certain transitive dependencies.
In Aether (Maven), exclusions are applied to an individual dependency
rather than being global. In Grape, exclusions are global.
AetherGrapeEngine adheres to the Grape convention by applying every
exclusion create by @GrabExclude to every dependency, effectively making
them global.
This commit is contained in:
Andy Wilkinson 2013-11-06 14:24:58 +00:00
parent 8922a6be4a
commit dc4bf01e95
2 changed files with 55 additions and 9 deletions

View File

@ -139,8 +139,9 @@ public class AetherGrapeEngine implements GrapeEngine {
@Override
public Object grab(Map args, Map... dependencyMaps) {
List<Exclusion> exclusions = createExclusions(args);
List<Dependency> dependencies = createDependencies(dependencyMaps, exclusions);
try {
List<Dependency> dependencies = createDependencies(dependencyMaps);
List<File> files = resolve(dependencies);
GroovyClassLoader classLoader = getClassLoader(args);
for (File file : files) {
@ -156,25 +157,43 @@ public class AetherGrapeEngine implements GrapeEngine {
return null;
}
private GroovyClassLoader getClassLoader(Map args) {
GroovyClassLoader classLoader = (GroovyClassLoader) args.get("classLoader");
return (classLoader == null ? this.classLoader : classLoader);
@SuppressWarnings("unchecked")
private List<Exclusion> createExclusions(Map<?, ?> args) {
List<Exclusion> exclusions = new ArrayList<Exclusion>();
List<Map<String, Object>> exclusionMaps = (List<Map<String, Object>>) args
.get("excludes");
if (exclusionMaps != null) {
for (Map<String, Object> exclusionMap : exclusionMaps) {
exclusions.add(createExclusion(exclusionMap));
}
}
return exclusions;
}
private List<Dependency> createDependencies(Map<?, ?>... dependencyMaps) {
private Exclusion createExclusion(Map<String, Object> exclusionMap) {
String group = (String) exclusionMap.get("group");
String module = (String) exclusionMap.get("module");
return new Exclusion(group, module, "*", "*");
}
private List<Dependency> createDependencies(Map<?, ?>[] dependencyMaps,
List<Exclusion> exclusions) {
List<Dependency> dependencies = new ArrayList<Dependency>(dependencyMaps.length);
for (Map<?, ?> dependencyMap : dependencyMaps) {
dependencies.add(createDependency(dependencyMap));
dependencies.add(createDependency(dependencyMap, exclusions));
}
return dependencies;
}
private Dependency createDependency(Map<?, ?> dependencyMap) {
private Dependency createDependency(Map<?, ?> dependencyMap,
List<Exclusion> exclusions) {
Artifact artifact = createArtifact(dependencyMap);
if (isTransitive(dependencyMap)) {
return new Dependency(artifact, JavaScopes.COMPILE);
return new Dependency(artifact, JavaScopes.COMPILE, false, exclusions);
}
else {
return new Dependency(artifact, JavaScopes.COMPILE, null, WILDCARD_EXCLUSION);
}
return new Dependency(artifact, JavaScopes.COMPILE, null, WILDCARD_EXCLUSION);
}
private Artifact createArtifact(Map<?, ?> dependencyMap) {
@ -216,6 +235,11 @@ public class AetherGrapeEngine implements GrapeEngine {
return files;
}
private GroovyClassLoader getClassLoader(Map args) {
GroovyClassLoader classLoader = (GroovyClassLoader) args.get("classLoader");
return (classLoader == null ? this.classLoader : classLoader);
}
@Override
public void addResolver(Map<String, Object> args) {
String name = (String) args.get("name");

View File

@ -18,6 +18,7 @@ package org.springframework.boot.cli.compiler.grape;
import groovy.lang.GroovyClassLoader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@ -47,6 +48,20 @@ public class AetherGrapeEngineTests {
assertEquals(6, this.groovyClassLoader.getURLs().length);
}
@SuppressWarnings("unchecked")
@Test
public void dependencyResolutionWithExclusions() {
Map<String, Object> args = new HashMap<String, Object>();
args.put("excludes",
Arrays.asList(createExclusion("org.springframework", "spring-core")));
this.grapeEngine.grab(args,
createDependency("org.springframework", "spring-jdbc", "3.2.4.RELEASE"),
createDependency("org.springframework", "spring-beans", "3.2.4.RELEASE"));
assertEquals(4, this.groovyClassLoader.getURLs().length);
}
@Test
public void nonTransitiveDependencyResolution() {
Map<String, Object> args = new HashMap<String, Object>();
@ -117,4 +132,11 @@ public class AetherGrapeEngineTests {
resolver.put("root", url);
return resolver;
}
private Map<String, Object> createExclusion(String group, String module) {
Map<String, Object> exclusion = new HashMap<String, Object>();
exclusion.put("group", group);
exclusion.put("module", module);
return exclusion;
}
}