Support bom-based dependency management in the CLI
Previously, the CLI’s dependency management used proprietary Properties file-based metadata to configure its dependency management. Since spring-boot-gradle-plugin’s move to using the separate dependency management plugin the CLI was the only user of this format. This commit updates the CLI to use Maven boms to configure its dependency management. By default it uses the spring-boot-dependencies bom. This configuration can be augmented and overridden using the new @DependencyManagementBom annotation which replaces @GrabMetadata. Closes gh-2688 Closes gh-2439
This commit is contained in:
@@ -1,128 +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.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.boot.dependency.tools.Dependency.Exclusion;
|
||||
|
||||
/**
|
||||
* Abstract base implementation for {@link Dependencies}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.1.0
|
||||
*/
|
||||
abstract class AbstractDependencies implements Dependencies {
|
||||
|
||||
private final Map<ArtifactAndGroupId, Dependency> byArtifactAndGroupId;
|
||||
|
||||
private final Map<String, Dependency> byArtifactId;
|
||||
|
||||
public AbstractDependencies() {
|
||||
this.byArtifactAndGroupId = new LinkedHashMap<ArtifactAndGroupId, Dependency>();
|
||||
this.byArtifactId = new LinkedHashMap<String, Dependency>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dependency find(String groupId, String artifactId) {
|
||||
return this.byArtifactAndGroupId.get(new ArtifactAndGroupId(groupId, artifactId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dependency find(String artifactId) {
|
||||
return this.byArtifactId.get(artifactId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Dependency> iterator() {
|
||||
return this.byArtifactAndGroupId.values().iterator();
|
||||
}
|
||||
|
||||
protected void add(ArtifactAndGroupId artifactAndGroupId, Dependency dependency) {
|
||||
Dependency existing = this.byArtifactAndGroupId.get(artifactAndGroupId);
|
||||
if (existing != null) {
|
||||
dependency = mergeDependencies(existing, dependency);
|
||||
}
|
||||
this.byArtifactAndGroupId.put(artifactAndGroupId, dependency);
|
||||
this.byArtifactId.put(dependency.getArtifactId(), dependency);
|
||||
}
|
||||
|
||||
private Dependency mergeDependencies(Dependency existingDependency,
|
||||
Dependency newDependency) {
|
||||
Set<Exclusion> combinedExclusions = new LinkedHashSet<Exclusion>();
|
||||
combinedExclusions.addAll(existingDependency.getExclusions());
|
||||
combinedExclusions.addAll(newDependency.getExclusions());
|
||||
return new Dependency(newDependency.getGroupId(), newDependency.getArtifactId(),
|
||||
newDependency.getVersion(), new ArrayList<Exclusion>(combinedExclusions));
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple holder for an artifact+group ID.
|
||||
*/
|
||||
protected static class ArtifactAndGroupId {
|
||||
|
||||
private final String groupId;
|
||||
|
||||
private final String artifactId;
|
||||
|
||||
public ArtifactAndGroupId(Dependency dependency) {
|
||||
this(dependency.getGroupId(), dependency.getArtifactId());
|
||||
}
|
||||
|
||||
public ArtifactAndGroupId(String groupId, String artifactId) {
|
||||
Assert.notNull(groupId, "GroupId must not be null");
|
||||
Assert.notNull(artifactId, "ArtifactId must not be null");
|
||||
this.groupId = groupId;
|
||||
this.artifactId = artifactId;
|
||||
}
|
||||
|
||||
public Dependency newDependency(String version) {
|
||||
return new Dependency(this.groupId, this.artifactId, version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.groupId.hashCode() * 31 + this.artifactId.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() == obj.getClass()) {
|
||||
ArtifactAndGroupId other = (ArtifactAndGroupId) obj;
|
||||
boolean result = true;
|
||||
result &= this.groupId.equals(other.groupId);
|
||||
result &= this.artifactId.equals(other.artifactId);
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +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;
|
||||
|
||||
/**
|
||||
* Simple subset of the Spring Assert utility.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class Assert {
|
||||
|
||||
public static void notNull(Object object, String message) {
|
||||
if (object == null) {
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,51 +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.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();
|
||||
|
||||
}
|
||||
@@ -1,197 +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.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A single dependency.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see Dependencies
|
||||
*/
|
||||
public final class Dependency {
|
||||
|
||||
private final String groupId;
|
||||
|
||||
private final String artifactId;
|
||||
|
||||
private final String version;
|
||||
|
||||
private final List<Exclusion> exclusions;
|
||||
|
||||
/**
|
||||
* Create a new {@link Dependency} instance.
|
||||
* @param groupId the group ID
|
||||
* @param artifactId the artifact ID
|
||||
* @param version the version
|
||||
*/
|
||||
public Dependency(String groupId, String artifactId, String version) {
|
||||
this(groupId, artifactId, version, Collections.<Exclusion> emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link Dependency} instance.
|
||||
* @param groupId the group ID
|
||||
* @param artifactId the artifact ID
|
||||
* @param version the version
|
||||
* @param exclusions the exclusions
|
||||
*/
|
||||
public Dependency(String groupId, String artifactId, String version,
|
||||
List<Exclusion> exclusions) {
|
||||
Assert.notNull(groupId, "GroupId must not be null");
|
||||
Assert.notNull(artifactId, "ArtifactId must not be null");
|
||||
Assert.notNull(version, "Version must not be null");
|
||||
Assert.notNull(exclusions, "Exclusions must not be null");
|
||||
this.groupId = groupId;
|
||||
this.artifactId = artifactId;
|
||||
this.version = version;
|
||||
this.exclusions = Collections.unmodifiableList(exclusions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the dependency group id.
|
||||
* @return the group ID
|
||||
*/
|
||||
public String getGroupId() {
|
||||
return this.groupId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the dependency artifact id.
|
||||
* @return the artifact ID
|
||||
*/
|
||||
public String getArtifactId() {
|
||||
return this.artifactId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the dependency version.
|
||||
* @return the version
|
||||
*/
|
||||
public String getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the dependency exclusions.
|
||||
* @return the exclusions
|
||||
*/
|
||||
public List<Exclusion> getExclusions() {
|
||||
return this.exclusions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.groupId + ":" + this.artifactId + ":" + this.version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + this.groupId.hashCode();
|
||||
result = prime * result + this.artifactId.hashCode();
|
||||
result = prime * result + this.version.hashCode();
|
||||
result = prime * result + this.exclusions.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() == obj.getClass()) {
|
||||
Dependency other = (Dependency) obj;
|
||||
boolean result = true;
|
||||
result &= this.groupId.equals(other.groupId);
|
||||
result &= this.artifactId.equals(other.artifactId);
|
||||
result &= this.version.equals(other.version);
|
||||
result &= this.exclusions.equals(other.exclusions);
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* A dependency exclusion.
|
||||
*/
|
||||
public static final class Exclusion {
|
||||
|
||||
private final String groupId;
|
||||
|
||||
private final 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;
|
||||
this.artifactId = artifactId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the exclusion artifact ID.
|
||||
* @return the exclusion artifact ID
|
||||
*/
|
||||
public String getArtifactId() {
|
||||
return this.artifactId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the exclusion group ID.
|
||||
* @return the exclusion group ID
|
||||
*/
|
||||
public String getGroupId() {
|
||||
return this.groupId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.groupId + ":" + this.artifactId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.groupId.hashCode() * 31 + this.artifactId.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() == obj.getClass()) {
|
||||
Exclusion other = (Exclusion) obj;
|
||||
boolean result = true;
|
||||
result &= this.groupId.equals(other.groupId);
|
||||
result &= this.artifactId.equals(other.artifactId);
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,113 +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.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* {@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 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-dependencies' POM version.
|
||||
* @return the version
|
||||
* @deprecated since 1.1.0 in favor of {@link #getSpringBootVersion()}
|
||||
*/
|
||||
@Deprecated
|
||||
public String getVersion() {
|
||||
return getSpringBootVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the 'spring-boot-dependencies' POM version.
|
||||
* @return the spring boot 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.
|
||||
* @param groupId the group ID
|
||||
* @param artifactId the artifact ID
|
||||
* @return a {@link Dependency} or {@code null}
|
||||
*/
|
||||
@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}
|
||||
*/
|
||||
@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() {
|
||||
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)) {
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +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;
|
||||
|
||||
/**
|
||||
* {@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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,128 +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.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 Dependencies} implementation backed a maven POM.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class PomDependencies extends AbstractDependencies {
|
||||
|
||||
/**
|
||||
* 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 PomDependencies(InputStream effectivePomInputStream) {
|
||||
try {
|
||||
Document effectivePom = readDocument(effectivePomInputStream);
|
||||
for (Dependency dependency : readDependencies(effectivePom)) {
|
||||
add(new ArtifactAndGroupId(dependency), dependency);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Document readDocument(InputStream inputStream) throws Exception {
|
||||
if (inputStream == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
Document document = builder.parse(inputStream);
|
||||
document.getDocumentElement().normalize();
|
||||
return document;
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private List<Dependency> readDependencies(Document document) throws Exception {
|
||||
Element element = (Element) document.getElementsByTagName("project").item(0);
|
||||
element = (Element) element.getElementsByTagName("dependencyManagement").item(0);
|
||||
element = (Element) element.getElementsByTagName("dependencies").item(0);
|
||||
NodeList nodes = element.getChildNodes();
|
||||
List<Dependency> dependencies = new ArrayList<Dependency>();
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
Node node = nodes.item(i);
|
||||
if (node instanceof Element) {
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,73 +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.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* {@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 PropertiesFileDependencies extends AbstractDependencies {
|
||||
|
||||
/**
|
||||
* 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 PropertiesFileDependencies(InputStream inputStream) throws IOException {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
properties.load(inputStream);
|
||||
initialize(properties);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void initialize(Properties properties) {
|
||||
Map<String, String> sortedMap = new TreeMap<String, String>();
|
||||
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
|
||||
sortedMap.put(entry.getKey().toString(), entry.getValue().toString());
|
||||
}
|
||||
for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
|
||||
ArtifactAndGroupId artifactAndGroupId = parse(entry.getKey());
|
||||
Dependency dependency = artifactAndGroupId.newDependency(entry.getValue());
|
||||
add(artifactAndGroupId, dependency);
|
||||
}
|
||||
}
|
||||
|
||||
private ArtifactAndGroupId parse(String value) {
|
||||
String[] parts = value.split("\\:");
|
||||
if (parts.length != 2) {
|
||||
throw new IllegalStateException("Unable to parse " + value);
|
||||
}
|
||||
return new ArtifactAndGroupId(parts[0], parts[1]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Utilities for working with the managed dependencies declared in the
|
||||
* {@literal spring-boot-dependencies} project.
|
||||
*
|
||||
* @see org.springframework.boot.dependency.tools.ManagedDependencies
|
||||
*/
|
||||
package org.springframework.boot.dependency.tools;
|
||||
|
||||
@@ -1,70 +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.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ManagedDependenciesDelegate}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class ManagedDependenciesDelegateTests {
|
||||
|
||||
private ManagedDependenciesDelegate dependencies;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
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
|
||||
public void extra() throws Exception {
|
||||
assertThat(this.dependencies.find("org.sample", "sample03").toString(),
|
||||
equalTo("org.sample:sample03:2.0.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void override() throws Exception {
|
||||
assertThat(this.dependencies.find("org.sample", "sample02").toString(),
|
||||
equalTo("org.sample:sample02:2.0.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void iterator() throws Exception {
|
||||
Iterator<Dependency> iterator = this.dependencies.iterator();
|
||||
assertThat(iterator.next().toString(), equalTo("org.sample:sample01:1.0.0"));
|
||||
assertThat(iterator.next().toString(), equalTo("org.sample:sample02:2.0.0"));
|
||||
assertThat(iterator.next().toString(),
|
||||
equalTo("org.springframework.boot:spring-boot:1.0.0.BUILD-SNAPSHOT"));
|
||||
assertThat(iterator.next().toString(), equalTo("org.sample:sample03:2.0.0"));
|
||||
assertThat(iterator.hasNext(), equalTo(false));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,74 +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 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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,90 +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.Iterator;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link PomDependencies}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class PomDependenciesTests {
|
||||
|
||||
private PomDependencies dependencies;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
InputStream x = getResource("test-effective-pom.xml");
|
||||
this.dependencies = new PomDependencies(x);
|
||||
}
|
||||
|
||||
private InputStream getResource(String name) {
|
||||
InputStream inputStream = getClass().getResourceAsStream(name);
|
||||
assertNotNull("Unable to read " + name, inputStream);
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void iterate() throws Exception {
|
||||
Iterator<Dependency> iterator = this.dependencies.iterator();
|
||||
assertThat(iterator.next().toString(), equalTo("org.sample:sample01:1.0.0"));
|
||||
assertThat(iterator.next().toString(), equalTo("org.sample:sample02:1.0.0"));
|
||||
assertThat(iterator.next().toString(),
|
||||
equalTo("org.springframework.boot:spring-boot:1.0.0.BUILD-SNAPSHOT"));
|
||||
assertThat(iterator.hasNext(), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByArtifactAndGroupId() throws Exception {
|
||||
assertThat(this.dependencies.find("org.sample", "sample02").toString(),
|
||||
equalTo("org.sample:sample02:1.0.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByArtifactAndGroupIdMissing() throws Exception {
|
||||
assertThat(this.dependencies.find("org.sample", "missing"), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByArtifactId() throws Exception {
|
||||
assertThat(this.dependencies.find("sample02").toString(),
|
||||
equalTo("org.sample:sample02:1.0.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByArtifactIdMissing() throws Exception {
|
||||
assertThat(this.dependencies.find("missing"), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exludes() throws Exception {
|
||||
Dependency dependency = this.dependencies.find("org.sample", "sample01");
|
||||
assertThat(dependency.getExclusions().toString(),
|
||||
equalTo("[org.exclude:exclude01]"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,87 +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.util.Iterator;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link PropertiesFileDependencies}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class PropertiesFileDependenciesTests {
|
||||
|
||||
private PropertiesFileDependencies dependencies;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.dependencies = new PropertiesFileDependencies(getClass()
|
||||
.getResourceAsStream("external.properties"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void iterate() throws Exception {
|
||||
Iterator<Dependency> iterator = this.dependencies.iterator();
|
||||
assertThat(iterator.next().toString(), equalTo("org.sample:sample01:1.0.0"));
|
||||
assertThat(iterator.next().toString(), equalTo("org.sample:sample02:1.0.0"));
|
||||
assertThat(iterator.next().toString(),
|
||||
equalTo("org.springframework.boot:spring-boot:1.0.0.BUILD-SNAPSHOT"));
|
||||
assertThat(iterator.hasNext(), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByArtifactAndGroupId() throws Exception {
|
||||
assertThat(this.dependencies.find("org.sample", "sample02").toString(),
|
||||
equalTo("org.sample:sample02:1.0.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByArtifactAndGroupIdMissing() throws Exception {
|
||||
assertThat(this.dependencies.find("org.sample", "missing"), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByArtifactAndGroupIdOnlyInEffectivePom() throws Exception {
|
||||
assertThat(this.dependencies.find("org.extra", "extra01"), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByArtifactId() throws Exception {
|
||||
assertThat(this.dependencies.find("sample02").toString(),
|
||||
equalTo("org.sample:sample02:1.0.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByArtifactIdMissing() throws Exception {
|
||||
assertThat(this.dependencies.find("missing"), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exludes() throws Exception {
|
||||
// No Support for exclusion
|
||||
Dependency dependency = this.dependencies.find("org.sample", "sample01");
|
||||
assertThat(dependency.getExclusions().size(), equalTo(0));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
org.sample\:sample03=2.0.0
|
||||
org.sample\:sample02=2.0.0
|
||||
@@ -1,3 +0,0 @@
|
||||
org.sample\:sample01=1.0.0
|
||||
org.sample\:sample02=1.0.0
|
||||
org.springframework.boot\:spring-boot=1.0.0.BUILD-SNAPSHOT
|
||||
@@ -1,34 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
<properties>
|
||||
<sample.version>1.0.0</sample.version>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.sample</groupId>
|
||||
<artifactId>sample01</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.exclude</groupId>
|
||||
<artifactId>exclude01</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.sample</groupId>
|
||||
<artifactId>sample02</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
</project>
|
||||
Reference in New Issue
Block a user