Example usage for org.apache.maven RepositoryUtils toArtifacts

List of usage examples for org.apache.maven RepositoryUtils toArtifacts

Introduction

In this page you can find the example usage for org.apache.maven RepositoryUtils toArtifacts.

Prototype

public static void toArtifacts(Collection<org.apache.maven.artifact.Artifact> artifacts,
            Collection<? extends DependencyNode> nodes, List<String> trail, DependencyFilter filter) 

Source Link

Usage

From source file:com.github.lucapino.manifest.ManifestMojo.java

License:Apache License

public Set<Artifact> getDependencyArtifacts(MavenProject project, RepositorySystemSession repoSession,
        ProjectDependenciesResolver projectDependenciesResolver) throws MojoExecutionException {

    DefaultDependencyResolutionRequest dependencyResolutionRequest = new DefaultDependencyResolutionRequest(
            project, repoSession);//ww  w .  j a v a2 s.c o m
    DependencyResolutionResult dependencyResolutionResult;

    try {
        dependencyResolutionResult = projectDependenciesResolver.resolve(dependencyResolutionRequest);
    } catch (DependencyResolutionException ex) {
        throw new MojoExecutionException(ex.getMessage(), ex);
    }

    Set artifacts = new LinkedHashSet();
    if (dependencyResolutionResult.getDependencyGraph() != null
            && !dependencyResolutionResult.getDependencyGraph().getChildren().isEmpty()) {
        RepositoryUtils.toArtifacts(artifacts, dependencyResolutionResult.getDependencyGraph().getChildren(),
                Collections.singletonList(project.getArtifact().getId()), null);
    }
    return artifacts;
}

From source file:org.jetbrains.idea.maven.server.Maven30ServerEmbedderImpl.java

License:Apache License

/**
 * copied from {@link DefaultProjectBuilder#resolveDependencies(MavenProject, RepositorySystemSession)}
 *//*from w  w w .j  a  v  a2 s. c o  m*/
private DependencyResolutionResult resolveDependencies(MavenProject project, RepositorySystemSession session) {
    DependencyResolutionResult resolutionResult;

    try {
        ProjectDependenciesResolver dependencyResolver = getComponent(ProjectDependenciesResolver.class);
        DefaultDependencyResolutionRequest resolution = new DefaultDependencyResolutionRequest(project,
                session);
        resolutionResult = dependencyResolver.resolve(resolution);
    } catch (DependencyResolutionException e) {
        resolutionResult = e.getResult();
    }

    Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
    if (resolutionResult.getDependencyGraph() != null) {
        RepositoryUtils.toArtifacts(artifacts, resolutionResult.getDependencyGraph().getChildren(),
                Collections.singletonList(project.getArtifact().getId()), null);

        // Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved or not
        LocalRepositoryManager lrm = session.getLocalRepositoryManager();
        for (Artifact artifact : artifacts) {
            if (!artifact.isResolved()) {
                String path = lrm.getPathForLocalArtifact(RepositoryUtils.toArtifact(artifact));
                artifact.setFile(new File(lrm.getRepository().getBasedir(), path));
            }
        }
    }
    project.setResolvedArtifacts(artifacts);
    project.setArtifacts(artifacts);

    return resolutionResult;
}

From source file:org.springframework.ide.vscode.commons.maven.MavenCore.java

License:Open Source License

/**
 * Calculates dependency graph for a Maven project provided the scope.
 *
 * @param project Maven Project descriptor
 * @param scope Dependency scope//from w w w. j  a  v a2 s .  c om
 * @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() {
            @Override
            public boolean visitEnter(DependencyNode node) {
                if (node.getDependency() != null) {
                    dependencyNodes.add(node);
                }
                return true;
            }

            @Override
            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.error("", 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();
}