Skip to content

Commit 9b982da

Browse files
author
Phillip Webb
committed
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
1 parent f1d216a commit 9b982da

File tree

18 files changed

+357
-218
lines changed

18 files changed

+357
-218
lines changed

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrabMetadataTransformation.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
import org.codehaus.groovy.syntax.SyntaxException;
4040
import org.codehaus.groovy.transform.ASTTransformation;
4141
import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
42+
import org.springframework.boot.dependency.tools.Dependencies;
4243
import org.springframework.boot.dependency.tools.ManagedDependencies;
43-
import org.springframework.boot.dependency.tools.PropertiesFileManagedDependencies;
44-
import org.springframework.boot.dependency.tools.VersionManagedDependencies;
44+
import org.springframework.boot.dependency.tools.PropertiesFileDependencies;
4545
import org.springframework.boot.groovy.GrabMetadata;
4646

4747
/**
@@ -151,11 +151,10 @@ private void updateArtifactCoordinatesResolver(
151151
List<Map<String, String>> metadataDependencies) {
152152
URI[] uris = Grape.getInstance().resolve(null,
153153
metadataDependencies.toArray(new Map[metadataDependencies.size()]));
154-
List<ManagedDependencies> managedDependencies = new ArrayList<ManagedDependencies>(
155-
uris.length);
154+
List<Dependencies> managedDependencies = new ArrayList<Dependencies>(uris.length);
156155
for (URI uri : uris) {
157156
try {
158-
managedDependencies.add(new PropertiesFileManagedDependencies(uri.toURL()
157+
managedDependencies.add(new PropertiesFileDependencies(uri.toURL()
159158
.openStream()));
160159
}
161160
catch (IOException ex) {
@@ -164,8 +163,8 @@ private void updateArtifactCoordinatesResolver(
164163
}
165164
}
166165

167-
this.resolutionContext.setManagedDependencies(new VersionManagedDependencies(
168-
managedDependencies));
166+
this.resolutionContext.setManagedDependencies(ManagedDependencies
167+
.get(managedDependencies));
169168
}
170169

171170
private void handleDuplicateGrabMetadataAnnotation(AnnotationNode annotationNode) {

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/dependencies/ManagedDependenciesArtifactCoordinatesResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
package org.springframework.boot.cli.compiler.dependencies;
1818

19+
import org.springframework.boot.dependency.tools.Dependencies;
1920
import org.springframework.boot.dependency.tools.Dependency;
2021
import org.springframework.boot.dependency.tools.ManagedDependencies;
21-
import org.springframework.boot.dependency.tools.VersionManagedDependencies;
2222
import org.springframework.util.StringUtils;
2323

2424
/**
25-
* {@link ArtifactCoordinatesResolver} backed by {@link ManagedDependencies}.
25+
* {@link ArtifactCoordinatesResolver} backed by {@link Dependencies}.
2626
*
2727
* @author Phillip Webb
2828
*/
@@ -32,7 +32,7 @@ public class ManagedDependenciesArtifactCoordinatesResolver implements
3232
private final ManagedDependencies dependencies;
3333

3434
public ManagedDependenciesArtifactCoordinatesResolver() {
35-
this(new VersionManagedDependencies());
35+
this(ManagedDependencies.get());
3636
}
3737

3838
public ManagedDependenciesArtifactCoordinatesResolver(ManagedDependencies dependencies) {

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/ManagedDependenciesFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
import org.eclipse.aether.graph.Dependency;
2525
import org.eclipse.aether.util.artifact.JavaScopes;
2626
import org.springframework.boot.dependency.tools.ManagedDependencies;
27-
import org.springframework.boot.dependency.tools.PomManagedDependencies;
28-
import org.springframework.boot.dependency.tools.VersionManagedDependencies;
27+
import org.springframework.boot.dependency.tools.PomDependencies;
2928

3029
/**
3130
* Factory to create Maven {@link Dependency} objects from Boot
32-
* {@link PomManagedDependencies}.
31+
* {@link PomDependencies}.
3332
*
3433
* @author Phillip Webb
3534
*/
@@ -38,7 +37,7 @@ public class ManagedDependenciesFactory {
3837
private final ManagedDependencies dependencies;
3938

4039
ManagedDependenciesFactory() {
41-
this(new VersionManagedDependencies());
40+
this(ManagedDependencies.get());
4241
}
4342

4443
public ManagedDependenciesFactory(ManagedDependencies dependencies) {

spring-boot-integration-tests/src/test/java/org/springframework/boot/starter/StarterDependenciesIntegrationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
import org.junit.runner.RunWith;
3333
import org.junit.runners.Parameterized;
3434
import org.junit.runners.Parameterized.Parameters;
35-
import org.springframework.boot.dependency.tools.VersionManagedDependencies;
35+
import org.springframework.boot.dependency.tools.ManagedDependencies;
3636
import org.springframework.util.FileCopyUtils;
3737

3838
import static org.junit.Assert.fail;
3939

4040
/**
4141
* Tests for the various starter projects to check that they don't pull in unwanted
4242
* transitive dependencies when used with Gradle
43-
*
43+
*
4444
* @author Andy Wilkinson
4545
*/
4646
@RunWith(Parameterized.class)
@@ -89,8 +89,8 @@ public static void createProject() throws IOException {
8989

9090
@BeforeClass
9191
public static void determineVersions() throws Exception {
92-
springVersion = new VersionManagedDependencies().find("spring-core").getVersion();
93-
bootVersion = new VersionManagedDependencies().find("spring-boot").getVersion();
92+
springVersion = ManagedDependencies.get().find("spring-core").getVersion();
93+
bootVersion = ManagedDependencies.get().find("spring-boot").getVersion();
9494
}
9595

9696
@AfterClass
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121
import java.util.Map;
2222

2323
/**
24-
* Abstract base implementation for {@link ManagedDependencies}.
24+
* Abstract base implementation for {@link Dependencies}.
2525
*
2626
* @author Phillip Webb
2727
* @since 1.1.0
2828
*/
29-
abstract class AbstractManagedDependencies implements ManagedDependencies {
29+
abstract class AbstractDependencies implements Dependencies {
3030

3131
private final Map<ArtifactAndGroupId, Dependency> byArtifactAndGroupId;
3232

3333
private final Map<String, Dependency> byArtifactId;
3434

35-
public AbstractManagedDependencies() {
35+
public AbstractDependencies() {
3636
this.byArtifactAndGroupId = new LinkedHashMap<ArtifactAndGroupId, Dependency>();
3737
this.byArtifactId = new LinkedHashMap<String, Dependency>();
3838
}
@@ -52,12 +52,6 @@ public Iterator<Dependency> iterator() {
5252
return this.byArtifactAndGroupId.values().iterator();
5353
}
5454

55-
@Override
56-
public String getSpringBootVersion() {
57-
Dependency dependency = find("org.springframework.boot", "spring-boot");
58-
return (dependency == null ? null : dependency.getVersion());
59-
}
60-
6155
protected void add(ArtifactAndGroupId artifactAndGroupId, Dependency dependency) {
6256
this.byArtifactAndGroupId.put(artifactAndGroupId, dependency);
6357
this.byArtifactId.put(dependency.getArtifactId(), dependency);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.dependency.tools;
18+
19+
import java.util.Iterator;
20+
21+
/**
22+
* Interface for accessing a known set of dependencies.
23+
*
24+
* @author Phillip Webb
25+
* @see Dependency
26+
* @since 1.1.0
27+
*/
28+
public interface Dependencies extends Iterable<Dependency> {
29+
30+
/**
31+
* Find a single dependency for the given group and artifact IDs.
32+
* @param groupId the group ID
33+
* @param artifactId the artifact ID
34+
* @return a {@link Dependency} or {@code null}
35+
*/
36+
public Dependency find(String groupId, String artifactId);
37+
38+
/**
39+
* Find a single dependency for the artifact IDs.
40+
* @param artifactId the artifact ID
41+
* @return a {@link Dependency} or {@code null}
42+
*/
43+
public Dependency find(String artifactId);
44+
45+
/**
46+
* Provide an {@link Iterator} over all managed {@link Dependency Dependencies}.
47+
*/
48+
@Override
49+
public Iterator<Dependency> iterator();
50+
51+
}

spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/Dependency.java

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@
1616

1717
package org.springframework.boot.dependency.tools;
1818

19-
import java.util.ArrayList;
2019
import java.util.Collections;
2120
import java.util.List;
2221

23-
import org.w3c.dom.Element;
24-
import org.w3c.dom.Node;
25-
import org.w3c.dom.NodeList;
26-
2722
/**
28-
* A single managed dependency.
23+
* A single dependency.
2924
*
3025
* @author Phillip Webb
31-
* @see ManagedDependencies
26+
* @see Dependencies
3227
*/
3328
public final class Dependency {
3429

@@ -133,19 +128,6 @@ public boolean equals(Object obj) {
133128
return false;
134129
}
135130

136-
static Dependency fromDependenciesXml(Element element) throws Exception {
137-
String groupId = getTextContent(element, "groupId");
138-
String artifactId = getTextContent(element, "artifactId");
139-
String version = getTextContent(element, "version");
140-
List<Exclusion> exclusions = Exclusion.fromExclusionsXml(element
141-
.getElementsByTagName("exclusions"));
142-
return new Dependency(groupId, artifactId, version, exclusions);
143-
}
144-
145-
private static String getTextContent(Element element, String tagName) {
146-
return element.getElementsByTagName(tagName).item(0).getTextContent();
147-
}
148-
149131
/**
150132
* A dependency exclusion.
151133
*/
@@ -155,7 +137,7 @@ public static final class Exclusion {
155137

156138
private final String artifactId;
157139

158-
private Exclusion(String groupId, String artifactId) {
140+
Exclusion(String groupId, String artifactId) {
159141
Assert.notNull(groupId, "GroupId must not be null");
160142
Assert.notNull(groupId, "ArtifactId must not be null");
161143
this.groupId = groupId;
@@ -204,31 +186,6 @@ public boolean equals(Object obj) {
204186
return false;
205187
}
206188

207-
private static List<Exclusion> fromExclusionsXml(NodeList exclusion) {
208-
if (exclusion == null || exclusion.getLength() == 0) {
209-
return Collections.emptyList();
210-
}
211-
return fromExclusionsXml(exclusion.item(0));
212-
}
213-
214-
private static List<Exclusion> fromExclusionsXml(Node item) {
215-
List<Exclusion> exclusions = new ArrayList<Dependency.Exclusion>();
216-
NodeList children = item.getChildNodes();
217-
for (int i = 0; i < children.getLength(); i++) {
218-
Node child = children.item(i);
219-
if (child instanceof Element) {
220-
exclusions.add(fromExclusionXml((Element) child));
221-
}
222-
}
223-
return exclusions;
224-
}
225-
226-
private static Exclusion fromExclusionXml(Element element) {
227-
String groupId = getTextContent(element, "groupId");
228-
String artifactId = getTextContent(element, "artifactId");
229-
return new Exclusion(groupId, artifactId);
230-
}
231-
232189
}
233190

234191
}

spring-boot-tools/spring-boot-dependency-tools/src/main/java/org/springframework/boot/dependency/tools/ManagedDependencies.java

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,96 @@
1616

1717
package org.springframework.boot.dependency.tools;
1818

19+
import java.util.Collection;
20+
import java.util.Collections;
1921
import java.util.Iterator;
2022

2123
/**
22-
* Interface for accessing a known managed set of dependencies.
24+
* {@link Dependencies} used by various spring boot tools. Provides programmatic access to
25+
* 'spring-boot-dependencies' and can also support user defined version managed
26+
* dependencies.
2327
*
2428
* @author Phillip Webb
2529
* @see Dependency
2630
*/
27-
public interface ManagedDependencies extends Iterable<Dependency> {
31+
public abstract class ManagedDependencies implements Dependencies {
32+
33+
// NOTE: Take care if changing the API of this class, it is used by the third-party
34+
// Gretty tool (https://github.com/akhikhl/gretty)
35+
36+
private final Dependencies delegate;
37+
38+
ManagedDependencies(Dependencies delegate) {
39+
this.delegate = delegate;
40+
}
2841

2942
/**
30-
* @return The Spring Boot version being managed.
43+
* Return the 'spring-boot-dependencies' POM version.
44+
* @deprecated since 1.1.0 in favor of {@link #getSpringBootVersion()}
3145
*/
32-
public String getSpringBootVersion();
46+
@Deprecated
47+
public String getVersion() {
48+
return getSpringBootVersion();
49+
}
50+
51+
/**
52+
* Return the 'spring-boot-dependencies' POM version.
53+
*/
54+
public String getSpringBootVersion() {
55+
Dependency dependency = find("org.springframework.boot", "spring-boot");
56+
return (dependency == null ? null : dependency.getVersion());
57+
}
3358

3459
/**
3560
* Find a single dependency for the given group and artifact IDs.
3661
* @param groupId the group ID
3762
* @param artifactId the artifact ID
3863
* @return a {@link Dependency} or {@code null}
3964
*/
40-
public Dependency find(String groupId, String artifactId);
65+
@Override
66+
public Dependency find(String groupId, String artifactId) {
67+
return this.delegate.find(groupId, artifactId);
68+
}
4169

4270
/**
4371
* Find a single dependency for the artifact IDs.
4472
* @param artifactId the artifact ID
4573
* @return a {@link Dependency} or {@code null}
4674
*/
47-
public Dependency find(String artifactId);
75+
@Override
76+
public Dependency find(String artifactId) {
77+
return this.delegate.find(artifactId);
78+
}
4879

4980
/**
5081
* Provide an {@link Iterator} over all managed {@link Dependency Dependencies}.
5182
*/
5283
@Override
53-
public Iterator<Dependency> iterator();
84+
public Iterator<Dependency> iterator() {
85+
return this.delegate.iterator();
86+
}
87+
88+
/**
89+
* Return spring-boot managed dependencies.
90+
* @return The dependencies.
91+
* @see #get(Collection)
92+
*/
93+
public static ManagedDependencies get() {
94+
return get(Collections.<Dependencies> emptySet());
95+
}
96+
97+
/**
98+
* Return spring-boot managed dependencies with optional version managed dependencies.
99+
* @param versionManagedDependencies a collection of {@link Dependencies} that take
100+
* precedence over the {@literal spring-boot-dependencies}.
101+
* @return the dependencies
102+
* @since 1.1.0
103+
*/
104+
public static ManagedDependencies get(
105+
Collection<Dependencies> versionManagedDependencies) {
106+
return new ManagedDependencies(new ManagedDependenciesDelegate(
107+
versionManagedDependencies)) {
108+
};
109+
}
54110

55111
}

0 commit comments

Comments
 (0)