Allow meta-data driven version overrides in Gradle
Add a `versionManagement` gradle configuration which can be used to provide alternative version meta-data. Primarily added so that the Spring IO platform can provide version overrides without causing a cyclic build dependency. Fixes gh-750pull/812/merge
parent
38fb8e6874
commit
506c0f50b9
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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 java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Abstract base implementation for {@link ManagedDependencies}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.1.0
|
||||
*/
|
||||
abstract class AbstractManagedDependencies implements ManagedDependencies {
|
||||
|
||||
private final Map<ArtifactAndGroupId, Dependency> byArtifactAndGroupId;
|
||||
|
||||
private final Map<String, Dependency> byArtifactId;
|
||||
|
||||
public AbstractManagedDependencies() {
|
||||
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();
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
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.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class PomManagedDependencies extends AbstractManagedDependencies {
|
||||
|
||||
/**
|
||||
* Create a new {@link PomManagedDependencies} instance.
|
||||
* @param effectivePomInputStream the effective POM containing resolved versions. The
|
||||
* input stream will be closed once content has been loaded.
|
||||
* @param dependenciesInputStream and optional POM used to limit the dependencies. The
|
||||
* input stream will be closed once content has been loaded. which will be added from
|
||||
* the effective POM
|
||||
*/
|
||||
public PomManagedDependencies(InputStream effectivePomInputStream,
|
||||
InputStream dependenciesInputStream) {
|
||||
try {
|
||||
Document effectivePom = readDocument(effectivePomInputStream);
|
||||
Document dependenciesPom = readDocument(dependenciesInputStream);
|
||||
|
||||
if (dependenciesPom == null) {
|
||||
// No dependencies POM, add all items
|
||||
for (Dependency dependency : readDependencies(effectivePom)) {
|
||||
add(new ArtifactAndGroupId(dependency), dependency);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Only add items that are also in the dependencies POM
|
||||
Map<ArtifactAndGroupId, Dependency> all = new HashMap<ArtifactAndGroupId, Dependency>();
|
||||
for (Dependency dependency : readDependencies(effectivePom)) {
|
||||
all.put(new ArtifactAndGroupId(dependency), dependency);
|
||||
}
|
||||
for (Dependency dependency : readDependencies(dependenciesPom)) {
|
||||
ArtifactAndGroupId artifactAndGroupId = new ArtifactAndGroupId(
|
||||
dependency);
|
||||
Dependency effectiveDependency = all.get(artifactAndGroupId);
|
||||
if (effectiveDependency != null) {
|
||||
add(artifactAndGroupId, effectiveDependency);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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(Dependency.fromDependenciesXml((Element) node));
|
||||
}
|
||||
}
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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 ManagedDependencies} backed by an external properties file (of the form created
|
||||
* by the Spring IO platform). The property key should be the groupID and versionId (in
|
||||
* the form {@literal group:version}) and the value should be the version.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class PropertiesFileManagedDependencies extends AbstractManagedDependencies {
|
||||
|
||||
/**
|
||||
* Create a new {@link PropertiesFileManagedDependencies} 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 {
|
||||
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]);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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"), getResource("dependencies-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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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 PomManagedDependencies}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class PomManagedDependenciesTests {
|
||||
|
||||
private PomManagedDependencies dependencies;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
InputStream x = getResource("test-effective-pom.xml");
|
||||
InputStream y = getResource("test-dependencies-pom.xml");
|
||||
this.dependencies = new PomManagedDependencies(x, y);
|
||||
}
|
||||
|
||||
private InputStream getResource(String name) {
|
||||
InputStream inputStream = getClass().getResourceAsStream(name);
|
||||
assertNotNull("Unable to read " + name, inputStream);
|
||||
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();
|
||||
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 {
|
||||
Dependency dependency = this.dependencies.find("org.sample", "sample01");
|
||||
assertThat(dependency.getExclusions().toString(),
|
||||
equalTo("[org.exclude:exclude01]"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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 VersionManagedDependencies}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class VersionManagedDependenciesTests {
|
||||
|
||||
private VersionManagedDependencies 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));
|
||||
}
|
||||
|
||||
@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 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();
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
org.sample\:sample03=2.0.0
|
||||
org.sample\:sample02=2.0.0
|
@ -0,0 +1,3 @@
|
||||
org.sample\:sample01=1.0.0
|
||||
org.sample\:sample02=1.0.0
|
||||
org.springframework.boot\:spring-boot=1.0.0.BUILD-SNAPSHOT
|
@ -1,50 +1,99 @@
|
||||
|
||||
package org.springframework.boot.gradle;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gradle.api.Action;
|
||||
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.gradle.api.artifacts.ResolutionStrategy;
|
||||
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;
|
||||
|
||||
/**
|
||||
* A resolution strategy to resolve missing version numbers using the
|
||||
* 'spring-boot-dependencies' POM.
|
||||
*
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class SpringBootResolutionStrategy {
|
||||
|
||||
public static final String VERSION_MANAGEMENT_CONFIGURATION = "versionManagement";
|
||||
|
||||
private static final String SPRING_BOOT_GROUP = "org.springframework.boot";
|
||||
|
||||
public static void apply(ResolutionStrategy resolutionStrategy) {
|
||||
resolutionStrategy.eachDependency(new Action<DependencyResolveDetails>() {
|
||||
public static void apply(final Project project, Configuration configuration) {
|
||||
if (VERSION_MANAGEMENT_CONFIGURATION.equals(configuration.getName())) {
|
||||
return;
|
||||
}
|
||||
VersionResolver versionResolver = new VersionResolver(project);
|
||||
configuration.getResolutionStrategy().eachDependency(versionResolver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(DependencyResolveDetails resolveDetails) {
|
||||
String version = resolveDetails.getTarget().getVersion();
|
||||
if (version == null || version.trim().length() == 0) {
|
||||
resolve(resolveDetails);
|
||||
}
|
||||
}
|
||||
private static class VersionResolver implements Action<DependencyResolveDetails> {
|
||||
|
||||
});
|
||||
}
|
||||
private Configuration versionManagementConfiguration;
|
||||
|
||||
protected static void resolve(DependencyResolveDetails resolveDetails) {
|
||||
private Collection<ManagedDependencies> versionManagedDependencies;
|
||||
|
||||
ManagedDependencies dependencies = ManagedDependencies.get();
|
||||
ModuleVersionSelector target = resolveDetails.getTarget();
|
||||
public VersionResolver(Project project) {
|
||||
this.versionManagementConfiguration = project.getConfigurations().getByName(
|
||||
VERSION_MANAGEMENT_CONFIGURATION);
|
||||
}
|
||||
|
||||
if (SPRING_BOOT_GROUP.equals(target.getGroup())) {
|
||||
resolveDetails.useVersion(dependencies.getVersion());
|
||||
return;
|
||||
@Override
|
||||
public void execute(DependencyResolveDetails resolveDetails) {
|
||||
String version = resolveDetails.getTarget().getVersion();
|
||||
if (version == null || version.trim().length() == 0) {
|
||||
resolve(resolveDetails);
|
||||
}
|
||||
}
|
||||
|
||||
Dependency dependency = dependencies.find(target.getGroup(), target.getName());
|
||||
if (dependency != null) {
|
||||
resolveDetails.useVersion(dependency.getVersion());
|
||||
private void resolve(DependencyResolveDetails resolveDetails) {
|
||||
ManagedDependencies dependencies = new VersionManagedDependencies(
|
||||
getVersionManagedDependencies());
|
||||
ModuleVersionSelector target = resolveDetails.getTarget();
|
||||
if (SPRING_BOOT_GROUP.equals(target.getGroup())) {
|
||||
resolveDetails.useVersion(dependencies.getSpringBootVersion());
|
||||
return;
|
||||
}
|
||||
Dependency dependency = dependencies.find(target.getGroup(), target.getName());
|
||||
if (dependency != null) {
|
||||
resolveDetails.useVersion(dependency.getVersion());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<ManagedDependencies> getVersionManagedDependencies() {
|
||||
if (versionManagedDependencies == null) {
|
||||
Set<File> files = versionManagementConfiguration.resolve();
|
||||
List<ManagedDependencies> dependencies = new ArrayList<ManagedDependencies>(
|
||||
files.size());
|
||||
for (File file : files) {
|
||||
dependencies.add(getPropertiesFileManagedDependencies(file));
|
||||
}
|
||||
this.versionManagedDependencies = dependencies;
|
||||
}
|
||||
return versionManagedDependencies;
|
||||
}
|
||||
|
||||
private ManagedDependencies 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));
|
||||
} catch (IOException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
buildscript {
|
||||
ext {
|
||||
springBootVersion = '1.1.0.BUILD-SNAPSHOT'
|
||||
}
|
||||
repositories {
|
||||
mavenLocal()
|
||||
}
|
||||
dependencies {
|
||||
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'spring-boot'
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
maven { url "http://repo.spring.io/release" }
|
||||
maven { url "http://repo.spring.io/milestone" }
|
||||
maven { url "http://repo.spring.io/snapshot" }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
versionManagement("io.spring.platform:platform-versions:1.0.0.BUILD-SNAPSHOT@properties")
|
||||
compile("org.springframework.boot:spring-boot-starter")
|
||||
compile("org.springframework.data:spring-data-hadoop")
|
||||
testCompile("org.springframework.boot:spring-boot-starter-test")
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
simple manual test to show how versionManagement dependencies can be used
|
||||
|
||||
run gradle dependencies and check that there are no failures
|
@ -0,0 +1,6 @@
|
||||
public class VersionManagementApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue