Skip to content

Commit 07699ea

Browse files
committed
Retry when dependency resolution fails in ModifiedClassPathClassLoader
Closes gh-22763
1 parent 63f7c75 commit 07699ea

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,6 +66,8 @@ final class ModifiedClassPathClassLoader extends URLClassLoader {
6666

6767
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern.compile(".*classpath(\\d+)?\\.jar");
6868

69+
private static final int MAX_RESOLUTION_ATTEMPTS = 5;
70+
6971
private final ClassLoader junitLoader;
7072

7173
ModifiedClassPathClassLoader(URL[] urls, ClassLoader parent, ClassLoader junitLoader) {
@@ -191,29 +193,35 @@ private static List<URL> getAdditionalUrls(MergedAnnotation<ClassPathOverrides>
191193
}
192194

193195
private static List<URL> resolveCoordinates(String[] coordinates) {
196+
Exception latestFailure = null;
194197
DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator();
195198
serviceLocator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
196199
serviceLocator.addService(TransporterFactory.class, HttpTransporterFactory.class);
197200
RepositorySystem repositorySystem = serviceLocator.getService(RepositorySystem.class);
198201
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
199202
LocalRepository localRepository = new LocalRepository(System.getProperty("user.home") + "/.m2/repository");
200203
session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));
201-
CollectRequest collectRequest = new CollectRequest(null, Arrays.asList(
202-
new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build()));
203-
204-
collectRequest.setDependencies(createDependencies(coordinates));
205-
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);
206-
try {
207-
DependencyResult result = repositorySystem.resolveDependencies(session, dependencyRequest);
208-
List<URL> resolvedArtifacts = new ArrayList<>();
209-
for (ArtifactResult artifact : result.getArtifactResults()) {
210-
resolvedArtifacts.add(artifact.getArtifact().getFile().toURI().toURL());
204+
for (int i = 0; i < MAX_RESOLUTION_ATTEMPTS; i++) {
205+
CollectRequest collectRequest = new CollectRequest(null,
206+
Arrays.asList(
207+
new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2")
208+
.build()));
209+
collectRequest.setDependencies(createDependencies(coordinates));
210+
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);
211+
try {
212+
DependencyResult result = repositorySystem.resolveDependencies(session, dependencyRequest);
213+
List<URL> resolvedArtifacts = new ArrayList<>();
214+
for (ArtifactResult artifact : result.getArtifactResults()) {
215+
resolvedArtifacts.add(artifact.getArtifact().getFile().toURI().toURL());
216+
}
217+
return resolvedArtifacts;
218+
}
219+
catch (Exception ex) {
220+
latestFailure = ex;
211221
}
212-
return resolvedArtifacts;
213-
}
214-
catch (Exception ignored) {
215-
return Collections.emptyList();
216222
}
223+
throw new IllegalStateException("Resolution failed after " + MAX_RESOLUTION_ATTEMPTS + " attempts",
224+
latestFailure);
217225
}
218226

219227
private static List<Dependency> createDependencies(String[] allCoordinates) {

0 commit comments

Comments
 (0)