Search in sources :

Example 1 with DuplicateTransformer

use of io.fabric8.maven.DuplicateTransformer 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)

Aggregations

DuplicateTransformer (io.fabric8.maven.DuplicateTransformer)1 FailedToResolveDependency (io.fabric8.maven.FailedToResolveDependency)1 ReplaceConflictingVersionResolver (io.fabric8.maven.ReplaceConflictingVersionResolver)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 NoRouteToHostException (java.net.NoRouteToHostException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)1 RepositoryException (org.eclipse.aether.RepositoryException)1 DependencyCollectionContext (org.eclipse.aether.collection.DependencyCollectionContext)1 DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)1 DependencyGraphTransformationContext (org.eclipse.aether.collection.DependencyGraphTransformationContext)1 DependencyGraphTransformer (org.eclipse.aether.collection.DependencyGraphTransformer)1 DependencySelector (org.eclipse.aether.collection.DependencySelector)1 DefaultDependencyNode (org.eclipse.aether.graph.DefaultDependencyNode)1