Restore dependency-tools API compatibility
Refactor dependency-tools to restore API compatibility with Spring Boot 1.0. This should reduce reflection hacks that tools such as Gretty would otherwise have to make. See gh-1035
This commit is contained in:
@@ -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<Map<String, String>> metadataDependencies) {
|
||||
URI[] uris = Grape.getInstance().resolve(null,
|
||||
metadataDependencies.toArray(new Map[metadataDependencies.size()]));
|
||||
List<ManagedDependencies> managedDependencies = new ArrayList<ManagedDependencies>(
|
||||
uris.length);
|
||||
List<Dependencies> managedDependencies = new ArrayList<Dependencies>(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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<ArtifactAndGroupId, Dependency> byArtifactAndGroupId;
|
||||
|
||||
private final Map<String, Dependency> byArtifactId;
|
||||
|
||||
public AbstractManagedDependencies() {
|
||||
public AbstractDependencies() {
|
||||
this.byArtifactAndGroupId = new LinkedHashMap<ArtifactAndGroupId, Dependency>();
|
||||
this.byArtifactId = new LinkedHashMap<String, Dependency>();
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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<Dependency> {
|
||||
|
||||
/**
|
||||
* 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<Dependency> iterator();
|
||||
|
||||
}
|
||||
@@ -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<Exclusion> 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<Exclusion> fromExclusionsXml(NodeList exclusion) {
|
||||
if (exclusion == null || exclusion.getLength() == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return fromExclusionsXml(exclusion.item(0));
|
||||
}
|
||||
|
||||
private static List<Exclusion> fromExclusionsXml(Node item) {
|
||||
List<Exclusion> exclusions = new ArrayList<Dependency.Exclusion>();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<Dependency> {
|
||||
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<Dependency> {
|
||||
* @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<Dependency> iterator();
|
||||
public Iterator<Dependency> iterator() {
|
||||
return this.delegate.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return spring-boot managed dependencies.
|
||||
* @return The dependencies.
|
||||
* @see #get(Collection)
|
||||
*/
|
||||
public static ManagedDependencies get() {
|
||||
return get(Collections.<Dependencies> 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<Dependencies> versionManagedDependencies) {
|
||||
return new ManagedDependencies(new ManagedDependenciesDelegate(
|
||||
versionManagedDependencies)) {
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<Dependencies> versionManagedDependencies) {
|
||||
this(getSpringBootDependencies(), versionManagedDependencies);
|
||||
}
|
||||
|
||||
ManagedDependenciesDelegate(Dependencies rootDependencies,
|
||||
Collection<Dependencies> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Exclusion> exclusions = createExclusions(element
|
||||
.getElementsByTagName("exclusions"));
|
||||
return new Dependency(groupId, artifactId, version, exclusions);
|
||||
}
|
||||
|
||||
private List<Exclusion> createExclusions(NodeList exclusion) {
|
||||
if (exclusion == null || exclusion.getLength() == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return createExclusions(exclusion.item(0));
|
||||
}
|
||||
|
||||
private List<Exclusion> createExclusions(Node item) {
|
||||
List<Exclusion> exclusions = new ArrayList<Dependency.Exclusion>();
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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.<ManagedDependencies> 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<ManagedDependencies> versionManagedDependencies) {
|
||||
this(getSpringBootDependencies(), versionManagedDependencies);
|
||||
}
|
||||
|
||||
VersionManagedDependencies(ManagedDependencies rootDependencies,
|
||||
Collection<ManagedDependencies> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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.<ManagedDependencies> 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.<Dependencies> 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<Dependency> iterator = this.dependencies.iterator();
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Dependency> iterator = this.dependencies.iterator();
|
||||
@@ -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<Dependency> iterator = this.dependencies.iterator();
|
||||
@@ -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<ManagedDependencies> versionManagedDependencies;
|
||||
private Collection<Dependencies> 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<ManagedDependencies> getVersionManagedDependencies() {
|
||||
private Collection<Dependencies> getVersionManagedDependencies() {
|
||||
if (versionManagedDependencies == null) {
|
||||
Set<File> files = versionManagementConfiguration.resolve();
|
||||
List<ManagedDependencies> dependencies = new ArrayList<ManagedDependencies>(
|
||||
List<Dependencies> dependencies = new ArrayList<Dependencies>(
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user