Retry when dependency resolution fails in ModifiedClassPathClassLoader
Closes gh-22763
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 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.
|
||||
@@ -66,6 +66,8 @@ final class ModifiedClassPathClassLoader extends URLClassLoader {
|
||||
|
||||
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern.compile(".*classpath(\\d+)?\\.jar");
|
||||
|
||||
private static final int MAX_RESOLUTION_ATTEMPTS = 5;
|
||||
|
||||
private final ClassLoader junitLoader;
|
||||
|
||||
ModifiedClassPathClassLoader(URL[] urls, ClassLoader parent, ClassLoader junitLoader) {
|
||||
@@ -191,6 +193,7 @@ final class ModifiedClassPathClassLoader extends URLClassLoader {
|
||||
}
|
||||
|
||||
private static List<URL> resolveCoordinates(String[] coordinates) {
|
||||
Exception latestFailure = null;
|
||||
DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator();
|
||||
serviceLocator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
|
||||
serviceLocator.addService(TransporterFactory.class, HttpTransporterFactory.class);
|
||||
@@ -198,22 +201,27 @@ final class ModifiedClassPathClassLoader extends URLClassLoader {
|
||||
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
|
||||
LocalRepository localRepository = new LocalRepository(System.getProperty("user.home") + "/.m2/repository");
|
||||
session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));
|
||||
CollectRequest collectRequest = new CollectRequest(null, Arrays.asList(
|
||||
new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build()));
|
||||
|
||||
collectRequest.setDependencies(createDependencies(coordinates));
|
||||
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);
|
||||
try {
|
||||
DependencyResult result = repositorySystem.resolveDependencies(session, dependencyRequest);
|
||||
List<URL> resolvedArtifacts = new ArrayList<>();
|
||||
for (ArtifactResult artifact : result.getArtifactResults()) {
|
||||
resolvedArtifacts.add(artifact.getArtifact().getFile().toURI().toURL());
|
||||
for (int i = 0; i < MAX_RESOLUTION_ATTEMPTS; i++) {
|
||||
CollectRequest collectRequest = new CollectRequest(null,
|
||||
Arrays.asList(
|
||||
new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2")
|
||||
.build()));
|
||||
collectRequest.setDependencies(createDependencies(coordinates));
|
||||
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);
|
||||
try {
|
||||
DependencyResult result = repositorySystem.resolveDependencies(session, dependencyRequest);
|
||||
List<URL> resolvedArtifacts = new ArrayList<>();
|
||||
for (ArtifactResult artifact : result.getArtifactResults()) {
|
||||
resolvedArtifacts.add(artifact.getArtifact().getFile().toURI().toURL());
|
||||
}
|
||||
return resolvedArtifacts;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
latestFailure = ex;
|
||||
}
|
||||
return resolvedArtifacts;
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
throw new IllegalStateException("Resolution failed after " + MAX_RESOLUTION_ATTEMPTS + " attempts",
|
||||
latestFailure);
|
||||
}
|
||||
|
||||
private static List<Dependency> createDependencies(String[] allCoordinates) {
|
||||
|
||||
Reference in New Issue
Block a user