Search in sources :

Example 6 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project bnd by bndtools.

the class DependencyResolver method discoverArtifacts.

private void discoverArtifacts(Map<File, ArtifactResult> files, List<DependencyNode> nodes, String parent, List<RemoteRepository> remoteRepositories) throws MojoExecutionException {
    for (DependencyNode node : nodes) {
        // Ensure that the file is downloaded so we can index it
        try {
            ArtifactResult resolvedArtifact = postProcessor.postProcessResult(system.resolveArtifact(session, new ArtifactRequest(node.getArtifact(), remoteRepositories, parent)));
            logger.debug("Located file: {} for artifact {}", resolvedArtifact.getArtifact().getFile(), resolvedArtifact);
            files.put(resolvedArtifact.getArtifact().getFile(), resolvedArtifact);
        } catch (ArtifactResolutionException e) {
            throw new MojoExecutionException("Failed to resolve the dependency " + node.getArtifact().toString(), e);
        }
        if (includeTransitive) {
            discoverArtifacts(files, node.getChildren(), node.getRequestContext(), remoteRepositories);
        } else {
            logger.debug("Ignoring transitive dependencies of {}", node.getDependency());
        }
    }
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DependencyNode(org.eclipse.aether.graph.DependencyNode) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 7 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project atlasmap by atlasmap.

the class GenerateInspectionsMojo method resolveClasspath.

private List<URL> resolveClasspath(List<String> artifacts) throws MojoFailureException {
    final List<URL> urls = new ArrayList<>();
    try {
        for (String gav : artifacts) {
            Artifact artifact = new DefaultArtifact(gav);
            getLog().debug("Resolving dependencies for artifact: " + artifact);
            CollectRequest collectRequest = new CollectRequest();
            collectRequest.setRoot(new Dependency(artifact, ""));
            collectRequest.setRepositories(remoteRepos);
            DependencyRequest dependencyRequest = new DependencyRequest();
            dependencyRequest.setCollectRequest(collectRequest);
            DependencyResult dependencyResult = system.resolveDependencies(repoSession, dependencyRequest);
            PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
            dependencyResult.getRoot().accept(nlg);
            Iterator<DependencyNode> it = nlg.getNodes().iterator();
            while (it.hasNext()) {
                DependencyNode node = it.next();
                if (node.getDependency() != null) {
                    Artifact x = node.getDependency().getArtifact();
                    if (x.getFile() != null) {
                        getLog().debug("Found dependency: " + x + " for artifact: " + artifact);
                        urls.add(x.getFile().toURI().toURL());
                    }
                }
            }
        }
    } catch (IllegalArgumentException e) {
        throw new MojoFailureException(e.getMessage(), e);
    } catch (DependencyResolutionException e) {
        throw new MojoFailureException(e.getMessage(), e);
    } catch (MalformedURLException e) {
        throw new MojoFailureException(e.getMessage(), e);
    }
    return urls;
}
Also used : MalformedURLException(java.net.MalformedURLException) DependencyResult(org.eclipse.aether.resolution.DependencyResult) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) URL(java.net.URL) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 8 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project fabric8 by jboss-fuse.

the class AetherBasedResolver method collectDependencies.

protected DependencyNode collectDependencies(Artifact root, String pomVersion, final Filter<Dependency> excludeDependencyFilter) throws RepositoryException, IOException {
    final DefaultRepositorySystemSession session = newSession();
    try {
        List<RemoteRepository> repos = selectRepositories();
        assignProxyAndMirrors(repos);
        ArtifactDescriptorResult artifactDescriptorResult = m_repoSystem.readArtifactDescriptor(session, new ArtifactDescriptorRequest(root, repos, null));
        repos.addAll(artifactDescriptorResult.getRepositories());
        Dependency rootDependency = new Dependency(root, null);
        List<Dependency> dependencies = artifactDescriptorResult.getDependencies();
        final DefaultDependencyNode rootNode = new DefaultDependencyNode(rootDependency);
        GenericVersionScheme versionScheme = new GenericVersionScheme();
        rootNode.setVersion(versionScheme.parseVersion(pomVersion));
        rootNode.setVersionConstraint(versionScheme.parseVersionConstraint(pomVersion));
        DependencyNode pomNode = rootNode;
        // final Filter<Dependency> shouldExclude = Filters.or(DependencyFilters.testScopeFilter, excludeDependencyFilter, new NewerVersionExistsFilter(rootNode));
        final Filter<Dependency> shouldExclude = Filters.or(Arrays.asList(DependencyFilters.testScopeFilter, excludeDependencyFilter));
        DependencySelector dependencySelector = new AndDependencySelector(new ScopeDependencySelector("test"), new ExclusionDependencySelector(), new DependencySelector() {

            @Override
            public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
                return this;
            }

            @Override
            public boolean selectDependency(Dependency dependency) {
                try {
                    return !DependencyFilters.matches(dependency, shouldExclude);
                } catch (Exception e) {
                    failedToMakeDependencyTree(dependency, e);
                    return false;
                }
            }
        });
        session.setDependencySelector(dependencySelector);
        // work on the root dependency directly?
        if (true) {
            for (Dependency dependency : dependencies) {
                DependencyNode node = resolveDependencies(session, repos, pomNode, dependency, shouldExclude);
                if (node != null) {
                    pomNode.getChildren().add(node);
                }
            }
        } else {
            DependencyNode node = resolveDependencies(session, repos, pomNode, rootDependency, shouldExclude);
            if (node != null) {
                pomNode = node;
            }
        }
        // now lets transform the dependency tree to remove different versions for the same artifact
        final DependencyGraphTransformationContext tranformContext = new DependencyGraphTransformationContext() {

            Map<Object, Object> map = new HashMap<>();

            public RepositorySystemSession getSession() {
                return session;
            }

            public Object get(Object key) {
                return map.get(key);
            }

            public Object put(Object key, Object value) {
                return map.put(key, value);
            }
        };
        DependencyGraphTransformer transformer = new ReplaceConflictingVersionResolver();
        pomNode = transformer.transformGraph(pomNode, tranformContext);
        transformer = new DuplicateTransformer();
        pomNode = transformer.transformGraph(pomNode, tranformContext);
        return pomNode;
    } finally {
        releaseSession(session);
    }
}
Also used : DependencyGraphTransformationContext(org.eclipse.aether.collection.DependencyGraphTransformationContext) ReplaceConflictingVersionResolver(io.fabric8.maven.ReplaceConflictingVersionResolver) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DuplicateTransformer(io.fabric8.maven.DuplicateTransformer) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DependencyNode(org.eclipse.aether.graph.DependencyNode) ChainedDependencyGraphTransformer(org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer) DependencyGraphTransformer(org.eclipse.aether.collection.DependencyGraphTransformer) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) ScopeDependencySelector(org.eclipse.aether.util.graph.selector.ScopeDependencySelector) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector) DependencyCollectionContext(org.eclipse.aether.collection.DependencyCollectionContext) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) Dependency(org.eclipse.aether.graph.Dependency) FailedToResolveDependency(io.fabric8.maven.FailedToResolveDependency) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ArtifactNotFoundException(org.eclipse.aether.transfer.ArtifactNotFoundException) RepositoryException(org.eclipse.aether.RepositoryException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) MetadataTransferException(org.eclipse.aether.transfer.MetadataTransferException) NoRouteToHostException(java.net.NoRouteToHostException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) MalformedURLException(java.net.MalformedURLException) MetadataNotFoundException(org.eclipse.aether.transfer.MetadataNotFoundException) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) ScopeDependencySelector(org.eclipse.aether.util.graph.selector.ScopeDependencySelector) OptionalDependencySelector(org.eclipse.aether.util.graph.selector.OptionalDependencySelector) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector) DependencySelector(org.eclipse.aether.collection.DependencySelector) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 9 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project fabric8 by jboss-fuse.

the class DuplicateTransformer method visit.

private void visit(DependencyNode node, Set<DependencyNode> visited) {
    List<DependencyNode> newChildren = new ArrayList<>();
    for (DependencyNode childNode : node.getChildren()) {
        if (visited.add(childNode)) {
            newChildren.add(childNode);
            visit(childNode, visited);
        }
    }
    node.setChildren(newChildren);
}
Also used : DependencyNode(org.eclipse.aether.graph.DependencyNode) ArrayList(java.util.ArrayList)

Example 10 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project fabric8 by jboss-fuse.

the class ReplaceConflictingVersionResolver method selectVersion.

private void selectVersion(DependencyNode node, DependencyNode parent, int depth, Map<DependencyNode, Integer> depths, ConflictGroup group, Map<?, ?> conflictIds) throws RepositoryException {
    Integer smallestDepth = depths.get(node);
    if (smallestDepth == null || smallestDepth > depth) {
        depths.put(node, depth);
    } else {
        return;
    }
    Object key = conflictIds.get(node);
    if (group.key.equals(key)) {
        Position pos = new Position(parent, depth);
        if (parent != null) {
            group.positions.add(pos);
        }
        if (!group.isAcceptable(node.getVersion())) {
            return;
        }
        group.candidates.put(node, pos);
        VersionConstraint versionConstraint = node.getVersionConstraint();
        if (versionConstraint != null && versionConstraint.getRange() != null && isNotEmpty(versionConstraint.getRange())) {
            group.constraints.add(versionConstraint);
        }
        if (group.version == null || isNearer(pos, node.getVersion(), group.position, group.version)) {
            group.version = node.getVersion();
            group.versionDependency = node;
            group.position = pos;
        }
        if (!group.isAcceptable(group.version)) {
            group.version = null;
            group.versionDependency = null;
            for (Iterator<Map.Entry<DependencyNode, Position>> it = group.candidates.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry<DependencyNode, Position> entry = it.next();
                Version version = entry.getKey().getVersion();
                pos = entry.getValue();
                if (!group.isAcceptable(version)) {
                    it.remove();
                } else if (group.version == null || isNearer(pos, version, group.position, group.version)) {
                    group.version = version;
                    group.versionDependency = entry.getKey();
                    group.position = pos;
                }
            }
            if (group.version == null) {
                Collection<String> versions = new LinkedHashSet<String>();
                for (VersionConstraint constraint : group.constraints) {
                    versions.add(constraint.toString());
                }
                // TODO, FIXME
                throw new UnsolvableVersionConflictException(Collections.<List<DependencyNode>>emptyList());
            }
        }
    }
    for (DependencyNode child : node.getChildren()) {
        selectVersion(child, node, depth + 1, depths, group, conflictIds);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VersionConstraint(org.eclipse.aether.version.VersionConstraint) Version(org.eclipse.aether.version.Version) DependencyNode(org.eclipse.aether.graph.DependencyNode) TreeMap(java.util.TreeMap) Map(java.util.Map) UnsolvableVersionConflictException(org.eclipse.aether.collection.UnsolvableVersionConflictException)

Aggregations

DependencyNode (org.eclipse.aether.graph.DependencyNode)28 Artifact (org.eclipse.aether.artifact.Artifact)14 CollectRequest (org.eclipse.aether.collection.CollectRequest)14 Dependency (org.eclipse.aether.graph.Dependency)13 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)12 ArrayList (java.util.ArrayList)11 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)10 File (java.io.File)8 List (java.util.List)8 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)8 DependencyResolutionException (org.eclipse.aether.resolution.DependencyResolutionException)8 IOException (java.io.IOException)7 DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)7 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)7 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)6 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)6 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)5 PreorderNodeListGenerator (org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator)5 MalformedURLException (java.net.MalformedURLException)4 RepositorySystem (org.eclipse.aether.RepositorySystem)4