Search in sources :

Example 6 with DependencyVisitor

use of org.eclipse.aether.graph.DependencyVisitor in project sts4 by spring-projects.

the class MavenCore method resolveDependencies.

/**
 * Calculates dependency graph for a Maven project provided the scope.
 *
 * @param project Maven Project descriptor
 * @param scope Dependency scope
 * @return Set of all dependencies including transient ones
 * @throws MavenException
 */
public Set<Artifact> resolveDependencies(MavenProject project, String scope) throws MavenException {
    MavenExecutionRequest request = maven.createExecutionRequest();
    DefaultRepositorySystemSession session = maven.createRepositorySession(request);
    DependencyNode graph = readDependencyTree(maven.lookupComponent(org.eclipse.aether.RepositorySystem.class), session, project, scope);
    if (graph != null) {
        ArrayList<DependencyNode> dependencyNodes = new ArrayList<>();
        graph.accept(new DependencyVisitor() {

            public boolean visitEnter(DependencyNode node) {
                if (node.getDependency() != null) {
                    dependencyNodes.add(node);
                }
                return true;
            }

            public boolean visitLeave(DependencyNode dependencynode) {
                return true;
            }
        });
        LinkedHashSet<Artifact> artifacts = new LinkedHashSet<>();
        RepositoryUtils.toArtifacts(artifacts, dependencyNodes, Collections.singletonList(project.getArtifact().getId()), null);
        return artifacts.parallelStream().map(artifact -> {
            if (!artifact.isResolved()) {
                try {
                    artifact = maven.resolve(artifact, project.getRemoteArtifactRepositories(), request);
                } catch (MavenException e) {
                    Log.log(e);
                    // Maven 2.x quirk: an artifact always points at the local repo,
                    // regardless whether resolved or not
                    LocalRepositoryManager lrm = session.getLocalRepositoryManager();
                    String path = lrm.getPathForLocalArtifact(RepositoryUtils.toArtifact(artifact));
                    artifact.setFile(new File(lrm.getRepository().getBasedir(), path));
                }
            }
            return artifact;
        }).collect(Collectors.toSet());
    }
    return Collections.emptySet();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Arrays(java.util.Arrays) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) URL(java.net.URL) DependencyManagement(org.apache.maven.model.DependencyManagement) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) MavenProject(org.apache.maven.project.MavenProject) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) DependencyVisitor(org.eclipse.aether.graph.DependencyVisitor) Artifact(org.apache.maven.artifact.Artifact) JandexIndex(org.springframework.ide.vscode.commons.jandex.JandexIndex) Path(java.nio.file.Path) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) FilteringDependencyVisitor(org.eclipse.aether.util.graph.visitor.FilteringDependencyVisitor) Collection(java.util.Collection) Set(java.util.Set) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) SourceUrlProviderFromSourceContainer(org.springframework.ide.vscode.commons.javadoc.SourceUrlProviderFromSourceContainer) Collectors(java.util.stream.Collectors) ScopeDependencyFilter(org.eclipse.aether.util.filter.ScopeDependencyFilter) List(java.util.List) Stream(java.util.stream.Stream) RepositoryUtils(org.apache.maven.RepositoryUtils) LocalRepositoryManager(org.eclipse.aether.repository.LocalRepositoryManager) DependencyManagerUtils(org.eclipse.aether.util.graph.manager.DependencyManagerUtils) CloningDependencyVisitor(org.eclipse.aether.util.graph.visitor.CloningDependencyVisitor) HtmlJavadocProvider(org.springframework.ide.vscode.commons.javadoc.HtmlJavadocProvider) Supplier(com.google.common.base.Supplier) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) Suppliers(com.google.common.base.Suppliers) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) LinkedHashSet(java.util.LinkedHashSet) CollectRequest(org.eclipse.aether.collection.CollectRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) Log(org.springframework.ide.vscode.commons.util.Log) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) IOException(java.io.IOException) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) InputStreamReader(java.io.InputStreamReader) File(java.io.File) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) Paths(java.nio.file.Paths) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) InputStream(java.io.InputStream) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) ArrayList(java.util.ArrayList) Artifact(org.apache.maven.artifact.Artifact) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyVisitor(org.eclipse.aether.graph.DependencyVisitor) FilteringDependencyVisitor(org.eclipse.aether.util.graph.visitor.FilteringDependencyVisitor) CloningDependencyVisitor(org.eclipse.aether.util.graph.visitor.CloningDependencyVisitor) LocalRepositoryManager(org.eclipse.aether.repository.LocalRepositoryManager) File(java.io.File)

Aggregations

DependencyVisitor (org.eclipse.aether.graph.DependencyVisitor)6 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)4 File (java.io.File)3 Artifact (org.apache.maven.artifact.Artifact)3 CollectRequest (org.eclipse.aether.collection.CollectRequest)3 Dependency (org.eclipse.aether.graph.Dependency)3 DependencyNode (org.eclipse.aether.graph.DependencyNode)3 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)2 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)2 Artifact (org.eclipse.aether.artifact.Artifact)2 CollectResult (org.eclipse.aether.collection.CollectResult)2 Supplier (com.google.common.base.Supplier)1 Suppliers (com.google.common.base.Suppliers)1 THashMap (gnu.trove.THashMap)1 THashSet (gnu.trove.THashSet)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1