diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrabMetadataTransformation.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrabMetadataTransformation.java index d1be735c43..22308bc95a 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrabMetadataTransformation.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrabMetadataTransformation.java @@ -39,9 +39,9 @@ import org.codehaus.groovy.control.messages.SyntaxErrorMessage; import org.codehaus.groovy.syntax.SyntaxException; import org.codehaus.groovy.transform.ASTTransformation; import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext; +import org.springframework.boot.dependency.tools.Dependencies; import org.springframework.boot.dependency.tools.ManagedDependencies; -import org.springframework.boot.dependency.tools.PropertiesFileManagedDependencies; -import org.springframework.boot.dependency.tools.VersionManagedDependencies; +import org.springframework.boot.dependency.tools.PropertiesFileDependencies; import org.springframework.boot.groovy.GrabMetadata; /** @@ -151,11 +151,10 @@ public class GrabMetadataTransformation extends AnnotatedNodeASTTransformation { List> metadataDependencies) { URI[] uris = Grape.getInstance().resolve(null, metadataDependencies.toArray(new Map[metadataDependencies.size()])); - List managedDependencies = new ArrayList( - uris.length); + List managedDependencies = new ArrayList(uris.length); for (URI uri : uris) { try { - managedDependencies.add(new PropertiesFileManagedDependencies(uri.toURL() + managedDependencies.add(new PropertiesFileDependencies(uri.toURL() .openStream())); } catch (IOException ex) { @@ -164,8 +163,8 @@ public class GrabMetadataTransformation extends AnnotatedNodeASTTransformation { } } - this.resolutionContext.setManagedDependencies(new VersionManagedDependencies( - managedDependencies)); + this.resolutionContext.setManagedDependencies(ManagedDependencies + .get(managedDependencies)); } private void handleDuplicateGrabMetadataAnnotation(AnnotationNode annotationNode) { diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/dependencies/ManagedDependenciesArtifactCoordinatesResolver.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/dependencies/ManagedDependenciesArtifactCoordinatesResolver.java index a864829d09..3bee4cae07 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/dependencies/ManagedDependenciesArtifactCoordinatesResolver.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/dependencies/ManagedDependenciesArtifactCoordinatesResolver.java @@ -16,13 +16,13 @@ package org.springframework.boot.cli.compiler.dependencies; +import org.springframework.boot.dependency.tools.Dependencies; import org.springframework.boot.dependency.tools.Dependency; import org.springframework.boot.dependency.tools.ManagedDependencies; -import org.springframework.boot.dependency.tools.VersionManagedDependencies; import org.springframework.util.StringUtils; /** - * {@link ArtifactCoordinatesResolver} backed by {@link ManagedDependencies}. + * {@link ArtifactCoordinatesResolver} backed by {@link Dependencies}. * * @author Phillip Webb */ @@ -32,7 +32,7 @@ public class ManagedDependenciesArtifactCoordinatesResolver implements private final ManagedDependencies dependencies; public ManagedDependenciesArtifactCoordinatesResolver() { - this(new VersionManagedDependencies()); + this(ManagedDependencies.get()); } public ManagedDependenciesArtifactCoordinatesResolver(ManagedDependencies dependencies) { diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/ManagedDependenciesFactory.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/ManagedDependenciesFactory.java index e3742ad47d..54f540b98a 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/ManagedDependenciesFactory.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/ManagedDependenciesFactory.java @@ -24,12 +24,11 @@ import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.graph.Dependency; import org.eclipse.aether.util.artifact.JavaScopes; import org.springframework.boot.dependency.tools.ManagedDependencies; -import org.springframework.boot.dependency.tools.PomManagedDependencies; -import org.springframework.boot.dependency.tools.VersionManagedDependencies; +import org.springframework.boot.dependency.tools.PomDependencies; /** * Factory to create Maven {@link Dependency} objects from Boot - * {@link PomManagedDependencies}. + * {@link PomDependencies}. * * @author Phillip Webb */ @@ -38,7 +37,7 @@ public class ManagedDependenciesFactory { private final ManagedDependencies dependencies; ManagedDependenciesFactory() { - this(new VersionManagedDependencies()); + this(ManagedDependencies.get()); } public ManagedDependenciesFactory(ManagedDependencies dependencies) { diff --git a/spring-boot-integration-tests/src/test/java/org/springframework/boot/starter/StarterDependenciesIntegrationTests.java b/spring-boot-integration-tests/src/test/java/org/springframework/boot/starter/StarterDependenciesIntegrationTests.java index b92d112452..f6874e66d3 100644 --- a/spring-boot-integration-tests/src/test/java/org/springframework/boot/starter/StarterDependenciesIntegrationTests.java +++ b/spring-boot-integration-tests/src/test/java/org/springframework/boot/starter/StarterDependenciesIntegrationTests.java @@ -32,7 +32,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -import org.springframework.boot.dependency.tools.VersionManagedDependencies; +import org.springframework.boot.dependency.tools.ManagedDependencies; import org.springframework.util.FileCopyUtils; import static org.junit.Assert.fail; @@ -40,7 +40,7 @@ import static org.junit.Assert.fail; /** * Tests for the various starter projects to check that they don't pull in unwanted * transitive dependencies when used with Gradle - * + * * @author Andy Wilkinson */ @RunWith(Parameterized.class) @@ -89,8 +89,8 @@ public class StarterDependenciesIntegrationTests { @BeforeClass public static void determineVersions() throws Exception { - springVersion = new VersionManagedDependencies().find("spring-core").getVersion(); - bootVersion = new VersionManagedDependencies().find("spring-boot").getVersion(); + springVersion = ManagedDependencies.get().find("spring-core").getVersion(); + bootVersion = ManagedDependencies.get().find("spring-boot").getVersion(); } @AfterClass diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/AbstractManagedDependencies.java b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/AbstractDependencies.java similarity index 88% rename from spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/AbstractManagedDependencies.java rename to spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/AbstractDependencies.java index 099adbcbda..0f8be49c18 100644 --- a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/AbstractManagedDependencies.java +++ b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/AbstractDependencies.java @@ -21,18 +21,18 @@ import java.util.LinkedHashMap; import java.util.Map; /** - * Abstract base implementation for {@link ManagedDependencies}. + * Abstract base implementation for {@link Dependencies}. * * @author Phillip Webb * @since 1.1.0 */ -abstract class AbstractManagedDependencies implements ManagedDependencies { +abstract class AbstractDependencies implements Dependencies { private final Map byArtifactAndGroupId; private final Map byArtifactId; - public AbstractManagedDependencies() { + public AbstractDependencies() { this.byArtifactAndGroupId = new LinkedHashMap(); this.byArtifactId = new LinkedHashMap(); } @@ -52,12 +52,6 @@ abstract class AbstractManagedDependencies implements ManagedDependencies { return this.byArtifactAndGroupId.values().iterator(); } - @Override - public String getSpringBootVersion() { - Dependency dependency = find("org.springframework.boot", "spring-boot"); - return (dependency == null ? null : dependency.getVersion()); - } - protected void add(ArtifactAndGroupId artifactAndGroupId, Dependency dependency) { this.byArtifactAndGroupId.put(artifactAndGroupId, dependency); this.byArtifactId.put(dependency.getArtifactId(), dependency); diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/Dependencies.java b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/Dependencies.java new file mode 100644 index 0000000000..e46fd6354a --- /dev/null +++ b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/Dependencies.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2014 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.dependency.tools; + +import java.util.Iterator; + +/** + * Interface for accessing a known set of dependencies. + * + * @author Phillip Webb + * @see Dependency + * @since 1.1.0 + */ +public interface Dependencies extends Iterable { + + /** + * Find a single dependency for the given group and artifact IDs. + * @param groupId the group ID + * @param artifactId the artifact ID + * @return a {@link Dependency} or {@code null} + */ + public Dependency find(String groupId, String artifactId); + + /** + * Find a single dependency for the artifact IDs. + * @param artifactId the artifact ID + * @return a {@link Dependency} or {@code null} + */ + public Dependency find(String artifactId); + + /** + * Provide an {@link Iterator} over all managed {@link Dependency Dependencies}. + */ + @Override + public Iterator iterator(); + +} diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/Dependency.java b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/Dependency.java index c0773989ec..8b9dd64daa 100644 --- a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/Dependency.java +++ b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/Dependency.java @@ -16,19 +16,14 @@ package org.springframework.boot.dependency.tools; -import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - /** - * A single managed dependency. + * A single dependency. * * @author Phillip Webb - * @see ManagedDependencies + * @see Dependencies */ public final class Dependency { @@ -133,19 +128,6 @@ public final class Dependency { return false; } - static Dependency fromDependenciesXml(Element element) throws Exception { - String groupId = getTextContent(element, "groupId"); - String artifactId = getTextContent(element, "artifactId"); - String version = getTextContent(element, "version"); - List exclusions = Exclusion.fromExclusionsXml(element - .getElementsByTagName("exclusions")); - return new Dependency(groupId, artifactId, version, exclusions); - } - - private static String getTextContent(Element element, String tagName) { - return element.getElementsByTagName(tagName).item(0).getTextContent(); - } - /** * A dependency exclusion. */ @@ -155,7 +137,7 @@ public final class Dependency { private final String artifactId; - private Exclusion(String groupId, String artifactId) { + Exclusion(String groupId, String artifactId) { Assert.notNull(groupId, "GroupId must not be null"); Assert.notNull(groupId, "ArtifactId must not be null"); this.groupId = groupId; @@ -204,31 +186,6 @@ public final class Dependency { return false; } - private static List fromExclusionsXml(NodeList exclusion) { - if (exclusion == null || exclusion.getLength() == 0) { - return Collections.emptyList(); - } - return fromExclusionsXml(exclusion.item(0)); - } - - private static List fromExclusionsXml(Node item) { - List exclusions = new ArrayList(); - NodeList children = item.getChildNodes(); - for (int i = 0; i < children.getLength(); i++) { - Node child = children.item(i); - if (child instanceof Element) { - exclusions.add(fromExclusionXml((Element) child)); - } - } - return exclusions; - } - - private static Exclusion fromExclusionXml(Element element) { - String groupId = getTextContent(element, "groupId"); - String artifactId = getTextContent(element, "artifactId"); - return new Exclusion(groupId, artifactId); - } - } } diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/ManagedDependencies.java b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/ManagedDependencies.java index ad288c5cf9..289d772b1e 100644 --- a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/ManagedDependencies.java +++ b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/ManagedDependencies.java @@ -16,20 +16,45 @@ package org.springframework.boot.dependency.tools; +import java.util.Collection; +import java.util.Collections; import java.util.Iterator; /** - * Interface for accessing a known managed set of dependencies. + * {@link Dependencies} used by various spring boot tools. Provides programmatic access to + * 'spring-boot-dependencies' and can also support user defined version managed + * dependencies. * * @author Phillip Webb * @see Dependency */ -public interface ManagedDependencies extends Iterable { +public abstract class ManagedDependencies implements Dependencies { + + // NOTE: Take care if changing the API of this class, it is used by the third-party + // Gretty tool (https://github.com/akhikhl/gretty) + + private final Dependencies delegate; + + ManagedDependencies(Dependencies delegate) { + this.delegate = delegate; + } /** - * @return The Spring Boot version being managed. + * Return the 'spring-boot-dependencies' POM version. + * @deprecated since 1.1.0 in favor of {@link #getSpringBootVersion()} */ - public String getSpringBootVersion(); + @Deprecated + public String getVersion() { + return getSpringBootVersion(); + } + + /** + * Return the 'spring-boot-dependencies' POM version. + */ + public String getSpringBootVersion() { + Dependency dependency = find("org.springframework.boot", "spring-boot"); + return (dependency == null ? null : dependency.getVersion()); + } /** * Find a single dependency for the given group and artifact IDs. @@ -37,19 +62,50 @@ public interface ManagedDependencies extends Iterable { * @param artifactId the artifact ID * @return a {@link Dependency} or {@code null} */ - public Dependency find(String groupId, String artifactId); + @Override + public Dependency find(String groupId, String artifactId) { + return this.delegate.find(groupId, artifactId); + } /** * Find a single dependency for the artifact IDs. * @param artifactId the artifact ID * @return a {@link Dependency} or {@code null} */ - public Dependency find(String artifactId); + @Override + public Dependency find(String artifactId) { + return this.delegate.find(artifactId); + } /** * Provide an {@link Iterator} over all managed {@link Dependency Dependencies}. */ @Override - public Iterator iterator(); + public Iterator iterator() { + return this.delegate.iterator(); + } + + /** + * Return spring-boot managed dependencies. + * @return The dependencies. + * @see #get(Collection) + */ + public static ManagedDependencies get() { + return get(Collections. emptySet()); + } + + /** + * Return spring-boot managed dependencies with optional version managed dependencies. + * @param versionManagedDependencies a collection of {@link Dependencies} that take + * precedence over the {@literal spring-boot-dependencies}. + * @return the dependencies + * @since 1.1.0 + */ + public static ManagedDependencies get( + Collection versionManagedDependencies) { + return new ManagedDependencies(new ManagedDependenciesDelegate( + versionManagedDependencies)) { + }; + } } diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/ManagedDependenciesDelegate.java b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/ManagedDependenciesDelegate.java new file mode 100644 index 0000000000..770fb7ccc8 --- /dev/null +++ b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/ManagedDependenciesDelegate.java @@ -0,0 +1,72 @@ +/* + * Copyright 2012-2014 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.dependency.tools; + +import java.io.InputStream; +import java.util.Collection; + +/** + * {@link Dependencies} delegate used internally by {@link ManagedDependencies}. + * + * @author Phillip Webb + * @since 1.1.0 + */ +class ManagedDependenciesDelegate extends AbstractDependencies { + + private static Dependencies springBootDependencies; + + /** + * Create a new {@link ManagedDependenciesDelegate} instance with optional version + * managed dependencies. + * @param versionManagedDependencies a collection of {@link Dependencies} that take + * precedence over the `spring-boot-dependencies`. + */ + public ManagedDependenciesDelegate(Collection versionManagedDependencies) { + this(getSpringBootDependencies(), versionManagedDependencies); + } + + ManagedDependenciesDelegate(Dependencies rootDependencies, + Collection versionManagedDependencies) { + addAll(rootDependencies); + if (versionManagedDependencies != null) { + for (Dependencies managedDependencies : versionManagedDependencies) { + addAll(managedDependencies); + } + } + } + + private void addAll(Dependencies dependencies) { + for (Dependency dependency : dependencies) { + add(new ArtifactAndGroupId(dependency), dependency); + } + } + + private static Dependencies getSpringBootDependencies() { + if (springBootDependencies == null) { + springBootDependencies = new PomDependencies(getResource("effective-pom.xml")); + } + return springBootDependencies; + } + + private static InputStream getResource(String name) { + InputStream inputStream = ManagedDependenciesDelegate.class + .getResourceAsStream(name); + Assert.notNull(inputStream, "Unable to load " + name); + return inputStream; + } + +} diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PomManagedDependencies.java b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PomDependencies.java similarity index 58% rename from spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PomManagedDependencies.java rename to spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PomDependencies.java index 7b19a54c10..2bfa3920f7 100644 --- a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PomManagedDependencies.java +++ b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PomDependencies.java @@ -18,31 +18,33 @@ package org.springframework.boot.dependency.tools; import java.io.InputStream; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import org.springframework.boot.dependency.tools.Dependency.Exclusion; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** - * {@link ManagedDependencies} implementation backed a maven POM. + * {@link Dependencies} implementation backed a maven POM. * * @author Phillip Webb * @author Andy Wilkinson * @since 1.1.0 */ -public class PomManagedDependencies extends AbstractManagedDependencies { +public class PomDependencies extends AbstractDependencies { /** - * Create a new {@link PomManagedDependencies} instance. + * Create a new {@link PomDependencies} instance. * @param effectivePomInputStream the effective POM containing resolved versions. The * input stream will be closed once content has been loaded. */ - public PomManagedDependencies(InputStream effectivePomInputStream) { + public PomDependencies(InputStream effectivePomInputStream) { try { Document effectivePom = readDocument(effectivePomInputStream); for (Dependency dependency : readDependencies(effectivePom)) { @@ -79,10 +81,48 @@ public class PomManagedDependencies extends AbstractManagedDependencies { for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element) { - dependencies.add(Dependency.fromDependenciesXml((Element) node)); + dependencies.add(createDependency((Element) node)); } } return dependencies; } + private Dependency createDependency(Element element) throws Exception { + String groupId = getTextContent(element, "groupId"); + String artifactId = getTextContent(element, "artifactId"); + String version = getTextContent(element, "version"); + List exclusions = createExclusions(element + .getElementsByTagName("exclusions")); + return new Dependency(groupId, artifactId, version, exclusions); + } + + private List createExclusions(NodeList exclusion) { + if (exclusion == null || exclusion.getLength() == 0) { + return Collections.emptyList(); + } + return createExclusions(exclusion.item(0)); + } + + private List createExclusions(Node item) { + List exclusions = new ArrayList(); + NodeList children = item.getChildNodes(); + for (int i = 0; i < children.getLength(); i++) { + Node child = children.item(i); + if (child instanceof Element) { + exclusions.add(createExclusion((Element) child)); + } + } + return exclusions; + } + + private Exclusion createExclusion(Element element) { + String groupId = getTextContent(element, "groupId"); + String artifactId = getTextContent(element, "artifactId"); + return new Exclusion(groupId, artifactId); + } + + private String getTextContent(Element element, String tagName) { + return element.getElementsByTagName(tagName).item(0).getTextContent(); + } + } diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PropertiesFileManagedDependencies.java b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PropertiesFileDependencies.java similarity index 85% rename from spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PropertiesFileManagedDependencies.java rename to spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PropertiesFileDependencies.java index 1305883311..bcb4b76bb5 100644 --- a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PropertiesFileManagedDependencies.java +++ b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/PropertiesFileDependencies.java @@ -23,23 +23,23 @@ import java.util.Properties; import java.util.TreeMap; /** - * {@link ManagedDependencies} backed by an external properties file (of the form created + * {@link Dependencies} backed by an external properties file (of the form created * by the Spring IO platform). The property key should be the groupId and artifactId (in * the form {@literal groupId:artifactId}) and the value should be the version. * * @author Phillip Webb * @since 1.1.0 */ -public class PropertiesFileManagedDependencies extends AbstractManagedDependencies { +public class PropertiesFileDependencies extends AbstractDependencies { /** - * Create a new {@link PropertiesFileManagedDependencies} instance from the specified + * Create a new {@link PropertiesFileDependencies} instance from the specified * input stream. * @param inputStream source input stream (will be closed when properties have been * loaded) * @throws IOException */ - public PropertiesFileManagedDependencies(InputStream inputStream) throws IOException { + public PropertiesFileDependencies(InputStream inputStream) throws IOException { try { Properties properties = new Properties(); properties.load(inputStream); diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/VersionManagedDependencies.java b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/VersionManagedDependencies.java deleted file mode 100644 index d4f327cf52..0000000000 --- a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/VersionManagedDependencies.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2012-2014 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.dependency.tools; - -import java.io.InputStream; -import java.util.Collection; -import java.util.Collections; - -/** - * {@link ManagedDependencies} used by various spring boot tools. Provides programmatic - * access to 'spring-boot-dependencies' and can also support user defined version managed - * dependencies. - * - * @author Phillip Webb - * @since 1.1.0 - */ -public class VersionManagedDependencies extends AbstractManagedDependencies { - - private static ManagedDependencies springBootDependencies; - - /** - * Create a new {@link VersionManagedDependencies} instance for - * 'spring-boot-dependencies'. - */ - public VersionManagedDependencies() { - this(Collections. emptySet()); - } - - /** - * Create a new {@link VersionManagedDependencies} instance with optional version - * managed dependencies. - * @param versionManagedDependencies a collection of {@link ManagedDependencies} that - * take precedence over the `spring-boot-dependencies`. - */ - public VersionManagedDependencies( - Collection versionManagedDependencies) { - this(getSpringBootDependencies(), versionManagedDependencies); - } - - VersionManagedDependencies(ManagedDependencies rootDependencies, - Collection versionManagedDependencies) { - addAll(rootDependencies); - if (versionManagedDependencies != null) { - for (ManagedDependencies managedDependencies : versionManagedDependencies) { - addAll(managedDependencies); - } - } - } - - private void addAll(ManagedDependencies dependencies) { - for (Dependency dependency : dependencies) { - add(new ArtifactAndGroupId(dependency), dependency); - } - } - - private static ManagedDependencies getSpringBootDependencies() { - if (springBootDependencies == null) { - springBootDependencies = new PomManagedDependencies( - getResource("effective-pom.xml")); - } - return springBootDependencies; - } - - private static InputStream getResource(String name) { - InputStream inputStream = VersionManagedDependencies.class - .getResourceAsStream(name); - Assert.notNull(inputStream, "Unable to load " + name); - return inputStream; - } - -} diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/package-info.java b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/package-info.java index fd8846f8dd..9271ab60ae 100644 --- a/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/package-info.java +++ b/spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/package-info.java @@ -18,7 +18,7 @@ * Utilities for working with the managed dependencies declared in the * {@literal spring-boot-dependencies} project. * - * @see org.springframework.boot.dependency.tools.VersionManagedDependencies + * @see org.springframework.boot.dependency.tools.ManagedDependencies */ package org.springframework.boot.dependency.tools; diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/VersionManagedDependenciesTests.java b/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/ManagedDependenciesDelegateTests.java similarity index 71% rename from spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/VersionManagedDependenciesTests.java rename to spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/ManagedDependenciesDelegateTests.java index fb8724f2f5..ad11cad725 100644 --- a/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/VersionManagedDependenciesTests.java +++ b/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/ManagedDependenciesDelegateTests.java @@ -26,22 +26,22 @@ import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; /** - * Tests for {@link VersionManagedDependencies}. + * Tests for {@link ManagedDependenciesDelegate}. * * @author Phillip Webb */ -public class VersionManagedDependenciesTests { +public class ManagedDependenciesDelegateTests { - private VersionManagedDependencies dependencies; + private ManagedDependenciesDelegate dependencies; @Before public void setup() throws Exception { - PropertiesFileManagedDependencies root = new PropertiesFileManagedDependencies( - getClass().getResourceAsStream("external.properties")); - PropertiesFileManagedDependencies extra = new PropertiesFileManagedDependencies( - getClass().getResourceAsStream("additional-external.properties")); - this.dependencies = new VersionManagedDependencies(root, - Collections. singleton(extra)); + PropertiesFileDependencies root = new PropertiesFileDependencies(getClass() + .getResourceAsStream("external.properties")); + PropertiesFileDependencies extra = new PropertiesFileDependencies(getClass() + .getResourceAsStream("additional-external.properties")); + this.dependencies = new ManagedDependenciesDelegate(root, + Collections. singleton(extra)); } @Test @@ -56,12 +56,6 @@ public class VersionManagedDependenciesTests { equalTo("org.sample:sample02:2.0.0")); } - @Test - public void getSpringBootVersion() throws Exception { - assertThat(this.dependencies.getSpringBootVersion(), - equalTo("1.0.0.BUILD-SNAPSHOT")); - } - @Test public void iterator() throws Exception { Iterator iterator = this.dependencies.iterator(); diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/ManagedDependenciesTests.java b/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/ManagedDependenciesTests.java new file mode 100644 index 0000000000..99b5dde4a4 --- /dev/null +++ b/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/ManagedDependenciesTests.java @@ -0,0 +1,74 @@ +/* + * Copyright 2012-2014 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.dependency.tools; + +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +/** + * Tests for {@link ManagedDependencies}. + * + * @author Phillip Webb + */ +public class ManagedDependenciesTests { + + private ManagedDependencies managedDependencies; + + private Dependencies delegate; + + @Before + public void setup() { + this.delegate = mock(Dependencies.class); + this.managedDependencies = new ManagedDependencies(this.delegate) { + }; + } + + @Test + @Deprecated + public void getVersion() throws Exception { + this.managedDependencies.getVersion(); + verify(this.delegate).find("org.springframework.boot", "spring-boot"); + } + + @Test + public void getSpringBootVersion() throws Exception { + this.managedDependencies.getSpringBootVersion(); + verify(this.delegate).find("org.springframework.boot", "spring-boot"); + } + + @Test + public void findGroupIdArtifactId() throws Exception { + this.managedDependencies.find("groupId", "artifactId"); + verify(this.delegate).find("groupId", "artifactId"); + } + + @Test + public void findArtifactId() throws Exception { + this.managedDependencies.find("artifactId"); + verify(this.delegate).find("artifactId"); + } + + @Test + public void iterator() throws Exception { + this.managedDependencies.iterator(); + verify(this.delegate).iterator(); + } + +} diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PomManagedDependenciesTests.java b/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PomDependenciesTests.java similarity index 88% rename from spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PomManagedDependenciesTests.java rename to spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PomDependenciesTests.java index ba93460df9..f6675dd84e 100644 --- a/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PomManagedDependenciesTests.java +++ b/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PomDependenciesTests.java @@ -28,18 +28,18 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; /** - * Tests for {@link PomManagedDependencies}. + * Tests for {@link PomDependencies}. * * @author Phillip Webb */ -public class PomManagedDependenciesTests { +public class PomDependenciesTests { - private PomManagedDependencies dependencies; + private PomDependencies dependencies; @Before public void setup() { InputStream x = getResource("test-effective-pom.xml"); - this.dependencies = new PomManagedDependencies(x); + this.dependencies = new PomDependencies(x); } private InputStream getResource(String name) { @@ -48,12 +48,6 @@ public class PomManagedDependenciesTests { return inputStream; } - @Test - public void springBootVersion() throws Exception { - assertThat(this.dependencies.getSpringBootVersion(), - equalTo("1.0.0.BUILD-SNAPSHOT")); - } - @Test public void iterate() throws Exception { Iterator iterator = this.dependencies.iterator(); diff --git a/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PropertiesFileManagedDependenciesTests.java b/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PropertiesFileDependenciesTests.java similarity index 86% rename from spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PropertiesFileManagedDependenciesTests.java rename to spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PropertiesFileDependenciesTests.java index 996af35b1f..e7036969d6 100644 --- a/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PropertiesFileManagedDependenciesTests.java +++ b/spring-boot-tools/spring-boot-dependency-tools/src/test/java/org/springframework/boot/dependency/tools/PropertiesFileDependenciesTests.java @@ -26,26 +26,20 @@ import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; /** - * Tests for {@link PropertiesFileManagedDependencies}. + * Tests for {@link PropertiesFileDependencies}. * * @author Phillip Webb */ -public class PropertiesFileManagedDependenciesTests { +public class PropertiesFileDependenciesTests { - private PropertiesFileManagedDependencies dependencies; + private PropertiesFileDependencies dependencies; @Before public void setup() throws Exception { - this.dependencies = new PropertiesFileManagedDependencies(getClass() + this.dependencies = new PropertiesFileDependencies(getClass() .getResourceAsStream("external.properties")); } - @Test - public void springBootVersion() throws Exception { - assertThat(this.dependencies.getSpringBootVersion(), - equalTo("1.0.0.BUILD-SNAPSHOT")); - } - @Test public void iterate() throws Exception { Iterator iterator = this.dependencies.iterator(); diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/resolve/SpringBootResolutionStrategy.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/resolve/SpringBootResolutionStrategy.java index 474209f2ff..13083a9dba 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/resolve/SpringBootResolutionStrategy.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/resolve/SpringBootResolutionStrategy.java @@ -29,10 +29,10 @@ import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.DependencyResolveDetails; import org.gradle.api.artifacts.ModuleVersionSelector; +import org.springframework.boot.dependency.tools.Dependencies; import org.springframework.boot.dependency.tools.Dependency; import org.springframework.boot.dependency.tools.ManagedDependencies; -import org.springframework.boot.dependency.tools.PropertiesFileManagedDependencies; -import org.springframework.boot.dependency.tools.VersionManagedDependencies; +import org.springframework.boot.dependency.tools.PropertiesFileDependencies; /** * A resolution strategy to resolve missing version numbers using the @@ -58,7 +58,7 @@ public class SpringBootResolutionStrategy { private Configuration versionManagementConfiguration; - private Collection versionManagedDependencies; + private Collection versionManagedDependencies; public VersionResolver(Project project) { this.versionManagementConfiguration = project.getConfigurations().getByName( @@ -74,7 +74,7 @@ public class SpringBootResolutionStrategy { } private void resolve(DependencyResolveDetails resolveDetails) { - ManagedDependencies dependencies = new VersionManagedDependencies( + ManagedDependencies dependencies = ManagedDependencies.get( getVersionManagedDependencies()); ModuleVersionSelector target = resolveDetails.getTarget(); if (SPRING_BOOT_GROUP.equals(target.getGroup())) { @@ -87,10 +87,10 @@ public class SpringBootResolutionStrategy { } } - private Collection getVersionManagedDependencies() { + private Collection getVersionManagedDependencies() { if (versionManagedDependencies == null) { Set files = versionManagementConfiguration.resolve(); - List dependencies = new ArrayList( + List dependencies = new ArrayList( files.size()); for (File file : files) { dependencies.add(getPropertiesFileManagedDependencies(file)); @@ -100,12 +100,12 @@ public class SpringBootResolutionStrategy { return versionManagedDependencies; } - private ManagedDependencies getPropertiesFileManagedDependencies(File file) { + private Dependencies getPropertiesFileManagedDependencies(File file) { if (!file.getName().toLowerCase().endsWith(".properties")) { throw new IllegalStateException(file + " is not a version property file"); } try { - return new PropertiesFileManagedDependencies(new FileInputStream(file)); + return new PropertiesFileDependencies(new FileInputStream(file)); } catch (IOException ex) { throw new IllegalStateException(ex); }