Search in sources :

Example 1 with DependencyGraphTransformer

use of org.sonatype.aether.collection.DependencyGraphTransformer in project karaf by apache.

the class Dependency30Helper method getDependencyTree.

private DependencyNode getDependencyTree(Artifact artifact) throws MojoExecutionException {
    try {
        CollectRequest collectRequest = new CollectRequest(new Dependency(artifact, "compile"), null, projectRepositories);
        DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(repositorySystemSession);
        session.setDependencySelector(new AndDependencySelector(new OptionalDependencySelector(), new ScopeDependencySelector1(), new ExclusionDependencySelector()));
        DependencyGraphTransformer transformer = new ChainedDependencyGraphTransformer(new ConflictMarker(), new JavaEffectiveScopeCalculator(), new JavaDependencyContextRefiner());
        session.setDependencyGraphTransformer(transformer);
        CollectResult result = repositorySystem.collectDependencies(session, collectRequest);
        return result.getRoot();
    } catch (DependencyCollectionException e) {
        throw new MojoExecutionException("Cannot build project dependency tree", e);
    }
}
Also used : JavaDependencyContextRefiner(org.sonatype.aether.util.graph.transformer.JavaDependencyContextRefiner) DependencyCollectionException(org.sonatype.aether.collection.DependencyCollectionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CollectResult(org.sonatype.aether.collection.CollectResult) AndDependencySelector(org.sonatype.aether.util.graph.selector.AndDependencySelector) Dependency(org.sonatype.aether.graph.Dependency) CollectRequest(org.sonatype.aether.collection.CollectRequest) ChainedDependencyGraphTransformer(org.sonatype.aether.util.graph.transformer.ChainedDependencyGraphTransformer) OptionalDependencySelector(org.sonatype.aether.util.graph.selector.OptionalDependencySelector) DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession) JavaEffectiveScopeCalculator(org.sonatype.aether.util.graph.transformer.JavaEffectiveScopeCalculator) DependencyGraphTransformer(org.sonatype.aether.collection.DependencyGraphTransformer) ChainedDependencyGraphTransformer(org.sonatype.aether.util.graph.transformer.ChainedDependencyGraphTransformer) ConflictMarker(org.sonatype.aether.util.graph.transformer.ConflictMarker) ExclusionDependencySelector(org.sonatype.aether.util.graph.selector.ExclusionDependencySelector)

Example 2 with DependencyGraphTransformer

use of org.sonatype.aether.collection.DependencyGraphTransformer in project sonatype-aether by sonatype.

the class DefaultDependencyCollector method collectDependencies.

public CollectResult collectDependencies(RepositorySystemSession session, CollectRequest request) throws DependencyCollectionException {
    session = optimizeSession(session);
    RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
    CollectResult result = new CollectResult(request);
    DependencySelector depSelector = session.getDependencySelector();
    DependencyManager depManager = session.getDependencyManager();
    DependencyTraverser depTraverser = session.getDependencyTraverser();
    Dependency root = request.getRoot();
    List<RemoteRepository> repositories = request.getRepositories();
    List<Dependency> dependencies = request.getDependencies();
    List<Dependency> managedDependencies = request.getManagedDependencies();
    GraphEdge edge = null;
    if (root != null) {
        VersionRangeResult rangeResult;
        try {
            VersionRangeRequest rangeRequest = new VersionRangeRequest(root.getArtifact(), request.getRepositories(), request.getRequestContext());
            rangeRequest.setTrace(trace);
            rangeResult = versionRangeResolver.resolveVersionRange(session, rangeRequest);
            if (rangeResult.getVersions().isEmpty()) {
                throw new VersionRangeResolutionException(rangeResult, "No versions available for " + root.getArtifact() + " within specified range");
            }
        } catch (VersionRangeResolutionException e) {
            result.addException(e);
            throw new DependencyCollectionException(result);
        }
        Version version = rangeResult.getVersions().get(rangeResult.getVersions().size() - 1);
        root = root.setArtifact(root.getArtifact().setVersion(version.toString()));
        ArtifactDescriptorResult descriptorResult;
        try {
            ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
            descriptorRequest.setArtifact(root.getArtifact());
            descriptorRequest.setRepositories(request.getRepositories());
            descriptorRequest.setRequestContext(request.getRequestContext());
            descriptorRequest.setTrace(trace);
            if (isLackingDescriptor(root.getArtifact())) {
                descriptorResult = new ArtifactDescriptorResult(descriptorRequest);
            } else {
                descriptorResult = descriptorReader.readArtifactDescriptor(session, descriptorRequest);
            }
        } catch (ArtifactDescriptorException e) {
            result.addException(e);
            throw new DependencyCollectionException(result);
        }
        root = root.setArtifact(descriptorResult.getArtifact());
        repositories = remoteRepositoryManager.aggregateRepositories(session, repositories, descriptorResult.getRepositories(), true);
        dependencies = mergeDeps(dependencies, descriptorResult.getDependencies());
        managedDependencies = mergeDeps(managedDependencies, descriptorResult.getManagedDependencies());
        GraphNode node = new GraphNode();
        node.setAliases(descriptorResult.getAliases());
        node.setRepositories(request.getRepositories());
        edge = new GraphEdge(node);
        edge.setDependency(root);
        edge.setRequestContext(request.getRequestContext());
        edge.setRelocations(descriptorResult.getRelocations());
        edge.setVersionConstraint(rangeResult.getVersionConstraint());
        edge.setVersion(version);
    } else {
        edge = new GraphEdge(new GraphNode());
    }
    result.setRoot(edge);
    boolean traverse = (root == null) || depTraverser.traverseDependency(root);
    if (traverse && !dependencies.isEmpty()) {
        DataPool pool = new DataPool(session);
        EdgeStack edges = new EdgeStack();
        edges.push(edge);
        DefaultDependencyCollectionContext context = new DefaultDependencyCollectionContext(session, root, managedDependencies);
        Args args = new Args(result, session, trace, pool, edges, context);
        process(args, dependencies, repositories, depSelector.deriveChildSelector(context), depManager.deriveChildManager(context), depTraverser.deriveChildTraverser(context));
    }
    DependencyGraphTransformer transformer = session.getDependencyGraphTransformer();
    try {
        DefaultDependencyGraphTransformationContext context = new DefaultDependencyGraphTransformationContext(session);
        result.setRoot(transformer.transformGraph(edge, context));
    } catch (RepositoryException e) {
        result.addException(e);
    }
    if (!result.getExceptions().isEmpty()) {
        throw new DependencyCollectionException(result);
    }
    return result;
}
Also used : DependencyManager(org.sonatype.aether.collection.DependencyManager) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) RequestTrace(org.sonatype.aether.RequestTrace) DefaultRequestTrace(org.sonatype.aether.util.DefaultRequestTrace) DependencyTraverser(org.sonatype.aether.collection.DependencyTraverser) Version(org.sonatype.aether.version.Version) DependencyGraphTransformer(org.sonatype.aether.collection.DependencyGraphTransformer) VersionRangeRequest(org.sonatype.aether.resolution.VersionRangeRequest) ArtifactDescriptorRequest(org.sonatype.aether.resolution.ArtifactDescriptorRequest) VersionRangeResolutionException(org.sonatype.aether.resolution.VersionRangeResolutionException) DependencyCollectionException(org.sonatype.aether.collection.DependencyCollectionException) VersionRangeResult(org.sonatype.aether.resolution.VersionRangeResult) CollectResult(org.sonatype.aether.collection.CollectResult) RepositoryException(org.sonatype.aether.RepositoryException) Dependency(org.sonatype.aether.graph.Dependency) DependencySelector(org.sonatype.aether.collection.DependencySelector) ArtifactDescriptorResult(org.sonatype.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorException(org.sonatype.aether.resolution.ArtifactDescriptorException)

Aggregations

CollectResult (org.sonatype.aether.collection.CollectResult)2 DependencyCollectionException (org.sonatype.aether.collection.DependencyCollectionException)2 DependencyGraphTransformer (org.sonatype.aether.collection.DependencyGraphTransformer)2 Dependency (org.sonatype.aether.graph.Dependency)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 RepositoryException (org.sonatype.aether.RepositoryException)1 RequestTrace (org.sonatype.aether.RequestTrace)1 CollectRequest (org.sonatype.aether.collection.CollectRequest)1 DependencyManager (org.sonatype.aether.collection.DependencyManager)1 DependencySelector (org.sonatype.aether.collection.DependencySelector)1 DependencyTraverser (org.sonatype.aether.collection.DependencyTraverser)1 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)1 ArtifactDescriptorException (org.sonatype.aether.resolution.ArtifactDescriptorException)1 ArtifactDescriptorRequest (org.sonatype.aether.resolution.ArtifactDescriptorRequest)1 ArtifactDescriptorResult (org.sonatype.aether.resolution.ArtifactDescriptorResult)1 VersionRangeRequest (org.sonatype.aether.resolution.VersionRangeRequest)1 VersionRangeResolutionException (org.sonatype.aether.resolution.VersionRangeResolutionException)1 VersionRangeResult (org.sonatype.aether.resolution.VersionRangeResult)1 DefaultRepositorySystemSession (org.sonatype.aether.util.DefaultRepositorySystemSession)1 DefaultRequestTrace (org.sonatype.aether.util.DefaultRequestTrace)1