Search in sources :

Example 1 with DependencyManagement

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

the class DefaultDependencyCollector method process.

private void process(final Args args, List<Dependency> dependencies, List<RemoteRepository> repositories, DependencySelector depSelector, DependencyManager depManager, DependencyTraverser depTraverser) throws DependencyCollectionException {
    nextDependency: for (Dependency dependency : dependencies) {
        boolean disableVersionManagement = false;
        List<Artifact> relocations = Collections.emptyList();
        thisDependency: while (true) {
            if (!depSelector.selectDependency(dependency)) {
                continue nextDependency;
            }
            DependencyManagement depMngt = depManager.manageDependency(dependency);
            String premanagedVersion = null;
            String premanagedScope = null;
            if (depMngt != null) {
                if (depMngt.getVersion() != null && !disableVersionManagement) {
                    Artifact artifact = dependency.getArtifact();
                    premanagedVersion = artifact.getVersion();
                    dependency = dependency.setArtifact(artifact.setVersion(depMngt.getVersion()));
                }
                if (depMngt.getProperties() != null) {
                    Artifact artifact = dependency.getArtifact();
                    dependency = dependency.setArtifact(artifact.setProperties(depMngt.getProperties()));
                }
                if (depMngt.getScope() != null) {
                    premanagedScope = dependency.getScope();
                    dependency = dependency.setScope(depMngt.getScope());
                }
                if (depMngt.getExclusions() != null) {
                    dependency = dependency.setExclusions(depMngt.getExclusions());
                }
            }
            disableVersionManagement = false;
            boolean noDescriptor = isLackingDescriptor(dependency.getArtifact());
            boolean traverse = !noDescriptor && depTraverser.traverseDependency(dependency);
            VersionRangeResult rangeResult;
            try {
                VersionRangeRequest rangeRequest = new VersionRangeRequest();
                rangeRequest.setArtifact(dependency.getArtifact());
                rangeRequest.setRepositories(repositories);
                rangeRequest.setRequestContext(args.result.getRequest().getRequestContext());
                rangeRequest.setTrace(args.trace);
                Object key = args.pool.toKey(rangeRequest);
                rangeResult = args.pool.getConstraint(key, rangeRequest);
                if (rangeResult == null) {
                    rangeResult = versionRangeResolver.resolveVersionRange(args.session, rangeRequest);
                    args.pool.putConstraint(key, rangeResult);
                }
                if (rangeResult.getVersions().isEmpty()) {
                    throw new VersionRangeResolutionException(rangeResult, "No versions available for " + dependency.getArtifact() + " within specified range");
                }
            } catch (VersionRangeResolutionException e) {
                addException(args.result, e);
                continue nextDependency;
            }
            List<Version> versions = rangeResult.getVersions();
            for (Version version : versions) {
                Artifact originalArtifact = dependency.getArtifact().setVersion(version.toString());
                Dependency d = dependency.setArtifact(originalArtifact);
                ArtifactDescriptorResult descriptorResult;
                {
                    ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
                    descriptorRequest.setArtifact(d.getArtifact());
                    descriptorRequest.setRepositories(repositories);
                    descriptorRequest.setRequestContext(args.result.getRequest().getRequestContext());
                    descriptorRequest.setTrace(args.trace);
                    if (noDescriptor) {
                        descriptorResult = new ArtifactDescriptorResult(descriptorRequest);
                    } else {
                        Object key = args.pool.toKey(descriptorRequest);
                        descriptorResult = args.pool.getDescriptor(key, descriptorRequest);
                        if (descriptorResult == null) {
                            try {
                                descriptorResult = descriptorReader.readArtifactDescriptor(args.session, descriptorRequest);
                                args.pool.putDescriptor(key, descriptorResult);
                            } catch (ArtifactDescriptorException e) {
                                addException(args.result, e);
                                args.pool.putDescriptor(key, e);
                                continue;
                            }
                        } else if (descriptorResult == DataPool.NO_DESCRIPTOR) {
                            continue;
                        }
                    }
                }
                d = d.setArtifact(descriptorResult.getArtifact());
                GraphNode node = args.edges.top().getTarget();
                GraphEdge cycleEdge = args.edges.find(d.getArtifact());
                if (cycleEdge != null) {
                    GraphEdge edge = new GraphEdge(cycleEdge.getTarget());
                    edge.setDependency(d);
                    edge.setScope(d.getScope());
                    edge.setPremanagedScope(premanagedScope);
                    edge.setPremanagedVersion(premanagedVersion);
                    edge.setRelocations(relocations);
                    edge.setVersionConstraint(rangeResult.getVersionConstraint());
                    edge.setVersion(version);
                    edge.setRequestContext(args.result.getRequest().getRequestContext());
                    node.getOutgoingEdges().add(edge);
                    continue;
                }
                if (!descriptorResult.getRelocations().isEmpty()) {
                    relocations = descriptorResult.getRelocations();
                    disableVersionManagement = originalArtifact.getGroupId().equals(d.getArtifact().getGroupId()) && originalArtifact.getArtifactId().equals(d.getArtifact().getArtifactId());
                    dependency = d;
                    continue thisDependency;
                }
                d = args.pool.intern(d.setArtifact(args.pool.intern(d.getArtifact())));
                DependencySelector childSelector = null;
                DependencyManager childManager = null;
                DependencyTraverser childTraverser = null;
                List<RemoteRepository> childRepos = null;
                Object key = null;
                boolean recurse = traverse && !descriptorResult.getDependencies().isEmpty();
                if (recurse) {
                    DefaultDependencyCollectionContext context = args.collectionContext;
                    context.set(d, descriptorResult.getManagedDependencies());
                    childSelector = depSelector.deriveChildSelector(context);
                    childManager = depManager.deriveChildManager(context);
                    childTraverser = depTraverser.deriveChildTraverser(context);
                    childRepos = remoteRepositoryManager.aggregateRepositories(args.session, repositories, descriptorResult.getRepositories(), true);
                    key = args.pool.toKey(d.getArtifact(), childRepos, childSelector, childManager, childTraverser);
                } else {
                    key = args.pool.toKey(d.getArtifact(), repositories);
                }
                List<RemoteRepository> repos;
                ArtifactRepository repo = rangeResult.getRepository(version);
                if (repo instanceof RemoteRepository) {
                    repos = Collections.singletonList((RemoteRepository) repo);
                } else if (repo == null) {
                    repos = repositories;
                } else {
                    repos = Collections.emptyList();
                }
                GraphNode child = args.pool.getNode(key);
                if (child == null) {
                    child = new GraphNode();
                    child.setAliases(descriptorResult.getAliases());
                    child.setRepositories(repos);
                    args.pool.putNode(key, child);
                } else {
                    recurse = false;
                    if (repos.size() < child.getRepositories().size()) {
                        child.setRepositories(repos);
                    }
                }
                GraphEdge edge = new GraphEdge(child);
                edge.setDependency(d);
                edge.setScope(d.getScope());
                edge.setPremanagedScope(premanagedScope);
                edge.setPremanagedVersion(premanagedVersion);
                edge.setRelocations(relocations);
                edge.setVersionConstraint(rangeResult.getVersionConstraint());
                edge.setVersion(version);
                edge.setRequestContext(args.result.getRequest().getRequestContext());
                node.getOutgoingEdges().add(edge);
                if (recurse) {
                    args.edges.push(edge);
                    process(args, descriptorResult.getDependencies(), childRepos, childSelector, childManager, childTraverser);
                    args.edges.pop();
                }
            }
            break;
        }
    }
}
Also used : VersionRangeResult(org.sonatype.aether.resolution.VersionRangeResult) DependencyManager(org.sonatype.aether.collection.DependencyManager) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) ArtifactRepository(org.sonatype.aether.repository.ArtifactRepository) Dependency(org.sonatype.aether.graph.Dependency) DependencyTraverser(org.sonatype.aether.collection.DependencyTraverser) Artifact(org.sonatype.aether.artifact.Artifact) Version(org.sonatype.aether.version.Version) DependencySelector(org.sonatype.aether.collection.DependencySelector) ArrayList(java.util.ArrayList) List(java.util.List) VersionRangeRequest(org.sonatype.aether.resolution.VersionRangeRequest) ArtifactDescriptorResult(org.sonatype.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorException(org.sonatype.aether.resolution.ArtifactDescriptorException) DependencyManagement(org.sonatype.aether.collection.DependencyManagement) ArtifactDescriptorRequest(org.sonatype.aether.resolution.ArtifactDescriptorRequest) VersionRangeResolutionException(org.sonatype.aether.resolution.VersionRangeResolutionException)

Example 2 with DependencyManagement

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

the class ClassicDependencyManager method manageDependency.

public DependencyManagement manageDependency(Dependency dependency) {
    DependencyManagement management = null;
    Object key = getKey(dependency.getArtifact());
    if (depth >= 2) {
        String version = managedVersions.get(key);
        if (version != null) {
            if (management == null) {
                management = new DependencyManagement();
            }
            management.setVersion(version);
        }
        String scope = managedScopes.get(key);
        if (scope != null) {
            if (management == null) {
                management = new DependencyManagement();
            }
            management.setScope(scope);
            if (!JavaScopes.SYSTEM.equals(scope) && dependency.getArtifact().getProperty(ArtifactProperties.LOCAL_PATH, null) != null) {
                Map<String, String> properties = new HashMap<String, String>(dependency.getArtifact().getProperties());
                properties.remove(ArtifactProperties.LOCAL_PATH);
                management.setProperties(properties);
            }
        }
        if ((scope != null && JavaScopes.SYSTEM.equals(scope)) || (scope == null && JavaScopes.SYSTEM.equals(dependency.getScope()))) {
            String localPath = managedLocalPaths.get(key);
            if (localPath != null) {
                if (management == null) {
                    management = new DependencyManagement();
                }
                Map<String, String> properties = new HashMap<String, String>(dependency.getArtifact().getProperties());
                properties.put(ArtifactProperties.LOCAL_PATH, localPath);
                management.setProperties(properties);
            }
        }
    }
    Collection<Exclusion> exclusions = managedExclusions.get(key);
    if (exclusions != null) {
        if (management == null) {
            management = new DependencyManagement();
        }
        Collection<Exclusion> result = new LinkedHashSet<Exclusion>(dependency.getExclusions());
        result.addAll(exclusions);
        management.setExclusions(result);
    }
    return management;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) Exclusion(org.sonatype.aether.graph.Exclusion) DependencyManagement(org.sonatype.aether.collection.DependencyManagement)

Aggregations

DependencyManagement (org.sonatype.aether.collection.DependencyManagement)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Artifact (org.sonatype.aether.artifact.Artifact)1 DependencyManager (org.sonatype.aether.collection.DependencyManager)1 DependencySelector (org.sonatype.aether.collection.DependencySelector)1 DependencyTraverser (org.sonatype.aether.collection.DependencyTraverser)1 Dependency (org.sonatype.aether.graph.Dependency)1 Exclusion (org.sonatype.aether.graph.Exclusion)1 ArtifactRepository (org.sonatype.aether.repository.ArtifactRepository)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 Version (org.sonatype.aether.version.Version)1