From 32fc2c3d1c0a1ed532221dc14aabdd40fc73877b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 6 Nov 2013 17:46:53 +0000 Subject: [PATCH] Remove Boot-specifics from AetherGrapeEngine With the goal of making AetherGrapeEngine generally useful with Groovy, this commit removes any Boot specifics from it. Specifically, there is now only a single default repository: Maven Central. The Boot-specific Spring milestone and snapshot repositories are now added via @GrabResolver annotations that are added using an ASTTransformation. As part of this change, AetherGrapeEngine has also been updated to store its repositories using a LinkedHashSet, this ensures that the same repository is not used more than once while maintaining their ordering. --- .../boot/cli/compiler/GroovyCompiler.java | 2 + .../cli/compiler/grape/AetherGrapeEngine.java | 18 +++--- ...olversAutoConfigurationTransformation.java | 61 +++++++++++++++++++ .../grape/AetherGrapeEngineTests.java | 17 ------ 4 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/transformation/GrabResolversAutoConfigurationTransformation.java diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java index 8c299637f4c..3a06e3586cc 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java @@ -44,6 +44,7 @@ import org.codehaus.groovy.transform.ASTTransformationVisitor; import org.springframework.boot.cli.compiler.grape.AetherGrapeEngine; import org.springframework.boot.cli.compiler.grape.GrapeEngineInstaller; import org.springframework.boot.cli.compiler.transformation.DependencyAutoConfigurationTransformation; +import org.springframework.boot.cli.compiler.transformation.GrabResolversAutoConfigurationTransformation; import org.springframework.boot.cli.compiler.transformation.ResolveDependencyCoordinatesTransformation; /** @@ -95,6 +96,7 @@ public class GroovyCompiler { CompilerAutoConfiguration.class, GroovyCompiler.class.getClassLoader()); this.transformations = new ArrayList(); + this.transformations.add(new GrabResolversAutoConfigurationTransformation()); this.transformations.add(new DependencyAutoConfigurationTransformation( this.loader, this.coordinatesResolver, this.compilerAutoConfigurations)); if (this.configuration.isGuessDependencies()) { 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 7a5a7e3f660..fe27ac8212e 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 @@ -25,8 +25,10 @@ import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.eclipse.aether.DefaultRepositorySystemSession; @@ -78,7 +80,7 @@ public class AetherGrapeEngine implements GrapeEngine { private final RepositorySystem repositorySystem; - private final List repositories; + private final Set repositories; public AetherGrapeEngine(GroovyClassLoader classLoader) { this.classLoader = classLoader; @@ -115,19 +117,13 @@ public class AetherGrapeEngine implements GrapeEngine { return new File(System.getProperty("user.home"), ".m2"); } - private List getRemoteRepositories() { - List repositories = new ArrayList(); + private Set getRemoteRepositories() { + LinkedHashSet repositories = new LinkedHashSet(); addRemoteRepository(repositories, "central", "http://repo1.maven.org/maven2/"); - if (!Boolean.getBoolean("disableSpringSnapshotRepos")) { - addRemoteRepository(repositories, "spring-snapshot", - "http://repo.spring.io/snapshot"); - addRemoteRepository(repositories, "spring-milestone", - "http://repo.spring.io/milestone"); - } return repositories; } - private void addRemoteRepository(List repositories, String id, + private void addRemoteRepository(Set repositories, String id, String url) { repositories.add(new RemoteRepository.Builder(id, "default", url).build()); } @@ -221,7 +217,7 @@ public class AetherGrapeEngine implements GrapeEngine { throws ArtifactResolutionException { try { CollectRequest collectRequest = new CollectRequest((Dependency) null, - dependencies, this.repositories); + dependencies, new ArrayList(this.repositories)); DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE)); DependencyResult dependencyResult = this.repositorySystem diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/transformation/GrabResolversAutoConfigurationTransformation.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/transformation/GrabResolversAutoConfigurationTransformation.java new file mode 100644 index 00000000000..1a333093e39 --- /dev/null +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/transformation/GrabResolversAutoConfigurationTransformation.java @@ -0,0 +1,61 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli.compiler.transformation; + +import groovy.lang.GrabResolver; + +import org.codehaus.groovy.ast.ASTNode; +import org.codehaus.groovy.ast.AnnotationNode; +import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.ast.ModuleNode; +import org.codehaus.groovy.ast.expr.ConstantExpression; +import org.codehaus.groovy.control.SourceUnit; +import org.codehaus.groovy.transform.ASTTransformation; + +/** + * {@link ASTTransformation} to apply Grab resolver auto-configuration, thereby + * configuring the repositories available for dependency resolution. + * + * @author Andy Wilkinson + */ +public final class GrabResolversAutoConfigurationTransformation implements + ASTTransformation { + + @Override + public void visit(ASTNode[] nodes, SourceUnit source) { + if (!Boolean.getBoolean("disableSpringSnapshotRepos")) { + for (ASTNode node : nodes) { + if (node instanceof ModuleNode) { + ModuleNode module = (ModuleNode) node; + ClassNode classNode = module.getClasses().get(0); + classNode.addAnnotation(createGrabResolverAnnotation( + "spring-snapshot", "http://repo.spring.io/snapshot")); + classNode.addAnnotation(createGrabResolverAnnotation( + "spring-milestone", "http://repo.spring.io/milestone")); + } + } + } + } + + private AnnotationNode createGrabResolverAnnotation(String name, String url) { + AnnotationNode resolverAnnotation = new AnnotationNode(new ClassNode( + GrabResolver.class)); + resolverAnnotation.addMember("name", new ConstantExpression(name)); + resolverAnnotation.addMember("root", new ConstantExpression(url)); + return resolverAnnotation; + } +} 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 c48ad62e11e..21880af3d08 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 @@ -87,23 +87,6 @@ public class AetherGrapeEngineTests { assertEquals(6, customClassLoader.getURLs().length); } - @Test(expected = DependencyResolutionFailedException.class) - public void resolutionWithSnapshotRepositoriesDisabled() { - Map args = new HashMap(); - System.setProperty("disableSpringSnapshotRepos", "true"); - try { - AetherGrapeEngine aetherGrapeEngine = new AetherGrapeEngine( - this.groovyClassLoader); - aetherGrapeEngine.addResolver(createResolver("restlet.org", - "http://maven.restlet.org")); - aetherGrapeEngine.grab(args, - createDependency("org.springframework", "spring-jdbc", "3.2.0.M1")); - } - finally { - System.clearProperty("disableSpringSnapshotRepos"); - } - } - @Test public void resolutionWithCustomResolver() { Map args = new HashMap();