From dc4bf01e95c2fa4e501db05621eeadbc4de8089b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 6 Nov 2013 14:24:58 +0000 Subject: [PATCH] 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. --- .../cli/compiler/grape/AetherGrapeEngine.java | 42 +++++++++++++++---- .../grape/AetherGrapeEngineTests.java | 22 ++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java index 7fb1d56ecc..f74a016d69 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java @@ -139,8 +139,9 @@ public class AetherGrapeEngine implements GrapeEngine { @Override public Object grab(Map args, Map... dependencyMaps) { + List exclusions = createExclusions(args); + List dependencies = createDependencies(dependencyMaps, exclusions); try { - List dependencies = createDependencies(dependencyMaps); List 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 createExclusions(Map args) { + List exclusions = new ArrayList(); + List> exclusionMaps = (List>) args + .get("excludes"); + if (exclusionMaps != null) { + for (Map exclusionMap : exclusionMaps) { + exclusions.add(createExclusion(exclusionMap)); + } + } + return exclusions; + } + + private Exclusion createExclusion(Map exclusionMap) { + String group = (String) exclusionMap.get("group"); + String module = (String) exclusionMap.get("module"); + return new Exclusion(group, module, "*", "*"); } - private List createDependencies(Map... dependencyMaps) { + private List createDependencies(Map[] dependencyMaps, + List exclusions) { List dependencies = new ArrayList(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 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 args) { String name = (String) args.get("name"); diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java index 76f47ac4dd..80c68803c9 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineTests.java @@ -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 args = new HashMap(); + 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 args = new HashMap(); @@ -117,4 +132,11 @@ public class AetherGrapeEngineTests { resolver.put("root", url); return resolver; } + + private Map createExclusion(String group, String module) { + Map exclusion = new HashMap(); + exclusion.put("group", group); + exclusion.put("module", module); + return exclusion; + } }