Skip to content

Commit

Permalink
Fix resolution and add debug graph output (use -X)
Browse files Browse the repository at this point in the history
Changes:
* both spots doing collection now can dump the collected graph in DEBUG
* "runtime" resolution was wrong, as same filter was used to add root direct
  dependencies and later to flatten. This is wrong, basically all scopes
  sans test should be added, to supress any occurence of same dependency
  deeper in the graph, that will be eliminated by conflict resultion
  and finally the root added one will be eliminated by dependency
  filter itself.
  • Loading branch information
cstamas committed Feb 9, 2024
1 parent 03453ad commit dad73aa
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@
import org.eclipse.aether.util.filter.AndDependencyFilter;
import org.eclipse.aether.util.filter.ExclusionsDependencyFilter;
import org.eclipse.aether.util.filter.ScopeDependencyFilter;
import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.stream.Collectors.toSet;

public class MavenProvisioner {
private static final Logger logger = LoggerFactory.getLogger(MavenProvisioner.class);

private final RepositorySystem repositorySystem;
private final RepositorySystemSession repositorySystemSession;
Expand Down Expand Up @@ -443,6 +447,12 @@ private List<Artifact> resolveArtifacts(DependencyRequest request) throws Depend
}
}
DependencyResult result = repositorySystem.resolveDependencies(repositorySystemSession, request);

if (logger.isDebugEnabled() && result.getRoot() != null) {
logger.debug("MavenProvisioner -- Collection result for {}", request.getCollectRequest());
result.getRoot().accept(new DependencyGraphDumper(logger::debug));
}

List<Artifact> artifacts = new ArrayList<Artifact>();
for (ArtifactResult ar : result.getArtifactResults()) {
artifacts.add(ar.getArtifact());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
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.eclipse.aether.util.artifact.JavaScopes;
import org.eclipse.aether.util.filter.ScopeDependencyFilter;
import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -128,7 +130,7 @@ private List<Artifact> resolveRuntimeScopeTransitively() {
DependencyFilter runtimeFilter = new ScopeDependencyFilter(JavaScopes.SYSTEM, JavaScopes.PROVIDED, JavaScopes.TEST);
List<org.eclipse.aether.graph.Dependency> dependencies = project.getDependencies().stream()
.map(d -> RepositoryUtils.toDependency(d, repositorySystemSession.getArtifactTypeRegistry()))
.filter(d -> runtimeFilter.accept(new DefaultDependencyNode(d), Collections.emptyList()))
.filter(d -> !JavaScopes.TEST.equals(d.getScope()))
.collect(Collectors.toList());
List<org.eclipse.aether.graph.Dependency> managedDependencies = Collections.emptyList();
if (project.getDependencyManagement() != null) {
Expand All @@ -144,7 +146,14 @@ private List<Artifact> resolveRuntimeScopeTransitively() {
collectRequest.setManagedDependencies(managedDependencies);
DependencyRequest request = new DependencyRequest(collectRequest, runtimeFilter);
try {
return repositorySystem.resolveDependencies(repositorySystemSession, request).getArtifactResults().stream()
DependencyResult result = repositorySystem.resolveDependencies(repositorySystemSession, request);

if (logger.isDebugEnabled() && result.getRoot() != null) {
logger.debug("BaseMojo -- Collection result for {}", request.getCollectRequest());
result.getRoot().accept(new DependencyGraphDumper(logger::debug));
}

return result.getArtifactResults().stream()
.map(ArtifactResult::getArtifact)
.collect(Collectors.toList());
} catch (DependencyResolutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.codehaus.plexus.util.WriterFactory;

import java.io.File;
Expand All @@ -37,7 +36,7 @@
import java.util.ArrayList;
import java.util.List;

@Mojo(name = "generateDependencies", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
@Mojo(name = "generateDependencies", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class GeneratorMojo
extends BaseMojo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import ca.vanzyl.provisio.MavenProvisioner;

@Mojo(name = "provision", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
@Mojo(name = "provision", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
public class ProvisioningMojo extends BaseMojo {

@Parameter(defaultValue = "false", property = "skipProvision")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import java.io.File;
import java.util.ArrayList;
Expand All @@ -34,7 +33,7 @@
import java.util.Set;
import java.util.stream.Collectors;

@Mojo(name = "validateDependencies", requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
@Mojo(name = "validateDependencies", threadSafe = true)
public class ValidatorMojo
extends BaseMojo
{
Expand Down

0 comments on commit dad73aa

Please sign in to comment.