Skip to content

Commit

Permalink
GH-299: Do not fail if we have a partial dependency resolution failure
Browse files Browse the repository at this point in the history
If we only have a partial dependency resolution failure, log the error
details and continue with the dependencies that we do have access to.
This prevents us having immediate issues with erroneous transitive
dependencies that we do not care about, letting Maven fail later
if the resources were actually required.

This change makes builds more resillient to poorly configured
dependencies, such as the JAXB dependency used in ehcache.
  • Loading branch information
ascopes committed Jul 21, 2024
1 parent bf4cd42 commit eb3e89d
Showing 1 changed file with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.eclipse.aether.resolution.DependencyResult;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -140,6 +141,8 @@ public Collection<Path> resolveDependencies(
DependencyResolutionDepth defaultDependencyResolutionDepth,
boolean includeProjectDependencies
) throws ResolutionException {
DependencyResult dependencyResult;

try {
var dependenciesToResolve = Stream
.concat(
Expand All @@ -154,18 +157,35 @@ public Collection<Path> resolveDependencies(
);

var collectRequest = new CollectRequest(dependenciesToResolve, null, remoteRepositories);
var dependencyRequest = new DependencyRequest(collectRequest, null);

return repositorySystem.resolveDependencies(repositorySession, dependencyRequest)
.getArtifactResults()
.stream()
.map(ArtifactResult::getArtifact)
.map(this::determinePath)
.collect(Collectors.toUnmodifiableList());
var dependencyRequest = new DependencyRequest(collectRequest, null);
dependencyResult = repositorySystem.resolveDependencies(repositorySession, dependencyRequest);

} catch (DependencyResolutionException ex) {
throw new ResolutionException("Failed to resolve dependencies", ex);
// GH-299: if this exception is raised, we may still have some results we can use. If this is
// the case then resolution only partially failed, so continue for now unless strict
// resolution is enabled. We do not fail-fast here anymore (since 2.4.0) as any resolution
// errors should be dealt with by the maven-compiler-plugin later on if needed.
//
// If we didn't get any result, then something more fatal has occurred, so raise.
dependencyResult = ex.getResult();

if (dependencyResult == null) {
throw new ResolutionException("Failed to resolve dependencies", ex);
}

// Log the message as well here as we omit it by default if `--errors' is not passed to Maven.
log.error(
"Error resolving one or more dependencies, dependencies may be missing during "
+ "protobuf compilation! {}", ex.getMessage(), ex);
}

return dependencyResult
.getArtifactResults()
.stream()
.map(ArtifactResult::getArtifact)
.map(this::determinePath)
.collect(Collectors.toUnmodifiableList());
}

private Stream<org.eclipse.aether.graph.Dependency> getProjectDependencies() {
Expand Down

0 comments on commit eb3e89d

Please sign in to comment.