From eb3e89d70ac5f9a876c56e46849d5f21376216ac Mon Sep 17 00:00:00 2001 From: Ashley Scopes <73482956+ascopes@users.noreply.github.com> Date: Sun, 21 Jul 2024 16:00:04 +0100 Subject: [PATCH] GH-299: Do not fail if we have a partial dependency resolution failure 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. --- .../AetherMavenArtifactPathResolver.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/dependencies/aether/AetherMavenArtifactPathResolver.java b/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/dependencies/aether/AetherMavenArtifactPathResolver.java index b09eafb3..e5af6199 100644 --- a/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/dependencies/aether/AetherMavenArtifactPathResolver.java +++ b/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/dependencies/aether/AetherMavenArtifactPathResolver.java @@ -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; @@ -140,6 +141,8 @@ public Collection resolveDependencies( DependencyResolutionDepth defaultDependencyResolutionDepth, boolean includeProjectDependencies ) throws ResolutionException { + DependencyResult dependencyResult; + try { var dependenciesToResolve = Stream .concat( @@ -154,18 +157,35 @@ public Collection 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 getProjectDependencies() {