Example usage for org.apache.maven.artifact.resolver ArtifactResolutionResult getMissingArtifacts

List of usage examples for org.apache.maven.artifact.resolver ArtifactResolutionResult getMissingArtifacts

Introduction

In this page you can find the example usage for org.apache.maven.artifact.resolver ArtifactResolutionResult getMissingArtifacts.

Prototype

public List<Artifact> getMissingArtifacts() 

Source Link

Usage

From source file:com.github.jrh3k5.mojo.flume.AbstractFlumeAgentsMojo.java

License:Apache License

/**
 * Retrieve from the current project all Flume plugins declared as dependencies.
 * <p />/*from  ww w  . jav  a  2 s. c  o  m*/
 * This is intentionally made package-private to expose it for testing purposes.
 * 
 * @param agent
 *            The {@link Agent} whose Flume plugins are to be resolved.
 * @return A {@link Collection} of {@link Artifact} objects representing the given Flume plugins as project dependencies.
 * @throws IOException
 *             If any errors occur during the plugin retrieval.
 */
Collection<Artifact> getFlumePluginDependencies(Agent agent) throws IOException {
    try {
        final DependencyNode rootNode = dependencyGraphBuilder.buildDependencyGraph(project,
                new FlumePluginsArtifactFilter(agent.getFlumePlugins()));
        final List<Artifact> artifacts = new ArrayList<Artifact>(rootNode.getChildren().size());
        for (DependencyNode childNode : rootNode.getChildren()) {
            final Artifact artifact = childNode.getArtifact();
            final ArtifactResolutionResult result = artifactResolver.resolve(toRequest(artifact));
            if (!result.getMissingArtifacts().isEmpty()) {
                throw new IOException(
                        "Unable to resolve one or more artifacts: " + result.getMissingArtifacts());
            }
            artifacts.add(artifact);
        }
        return artifacts;
    } catch (DependencyGraphBuilderException e) {
        throw new IOException("Failed to find plugins as dependencies.", e);
    }
}

From source file:guru.nidi.maven.tools.dependency.DotCreator.java

License:Apache License

private Collection<Artifact> calcDependencies(Artifact artifact) {
    artifact.setScope(null);/*from  ww w.  ja  v a2s .c  o  m*/
    final ArtifactResolutionResult res = mavenContext.resolveArtifact(artifact, parameters);
    for (Iterator<Artifact> it = res.getArtifacts().iterator(); it.hasNext();) {
        final Artifact a = it.next();
        if (a.equals(artifact) || !parameters.include(a) || a.getDependencyTrail().size() != 2) {
            it.remove();
        }
    }
    if (res.getArtifacts().isEmpty() && !res.getMissingArtifacts().isEmpty()) {
        return null;
    }
    return res.getArtifacts();
}

From source file:org.fusesource.mop.MOPRepository.java

License:Open Source License

public Set<Artifact> resolveArtifacts(ArtifactFilter filter, final ArtifactId id)
        throws Exception, InvalidRepositoryException {
    debug("Resolving artifact " + id);

    RepositorySystem repositorySystem = (RepositorySystem) getContainer().lookup(RepositorySystem.class);

    List<ArtifactRepository> remoteRepositories = createRemoteRepositories(repositorySystem);
    ArtifactRepository localRepository = createLocalRepository(repositorySystem, "mop.local",
            getLocalRepo().getAbsolutePath(), false);

    // If group id is not set.. we can still look it up in the db
    // of installed artifacs.
    if (id.getGroupId() == null) {
        database(true, new DBCallback() {
            public void execute(Database database) throws Exception {

                // Makes groupId includeOptional.. we look it up in the database.
                if (id.getGroupId() == null) {
                    Map<String, Set<String>> rc = Database
                            .groupByGroupId(database.findByArtifactId(id.getArtifactId()));
                    if (rc.isEmpty()) {
                        throw new Exception(
                                "Please qualify a group id: No local artifacts match: " + id.getArtifactId());
                    }/*from  ww  w .j a  va  2  s. c  om*/
                    if (rc.size() > 1) {
                        System.out.println("Local artifacts that match:");
                        for (String s : rc.keySet()) {
                            System.out.println("   " + s + ":" + id.getArtifactId());
                        }
                        throw new Exception("Please qualify a group id: Multiple local artifacts match: " + id);
                    }
                    id.setGroupId(rc.keySet().iterator().next());
                    debug("Resolving artifact " + id);
                }

            }
        });
    }

    if (online) {

        // Keep track that we are trying an install..
        // If an install dies midway.. the repo will have partlly installed dependencies...
        // we may want to continue the install??
        // database.beginInstall(id.toString());

    }

    Artifact artifact = repositorySystem.createArtifactWithClassifier(id.getGroupId(), id.getArtifactId(),
            id.getVersion(), id.getType(), id.getClassifier());

    // Setup the filters which will constrain the resulting dependencies..
    List<ArtifactFilter> constraints = new ArrayList<ArtifactFilter>();
    constraints.add(new ScopeArtifactFilter(scope));
    if (!includeOptional) {
        constraints.add(new ArtifactFilter() {
            public boolean include(Artifact artifact) {
                return !artifact.isOptional();
            }
        });
    }
    if (filter != null) {
        constraints.add(filter);
    }

    ArtifactFilter filters = new AndArtifactFilter(constraints);

    ArtifactResolutionRequest request = new ArtifactResolutionRequest().setArtifact(artifact)
            .setResolveRoot(true).setResolveTransitively(isTransitive()).setLocalRepository(localRepository)
            .setRemoteRepositories(remoteRepositories).setOffline(!online).setCollectionFilter(filters);

    ArtifactResolutionResult result = repositorySystem.resolve(request);

    List<Artifact> list = result.getMissingArtifacts();
    if (!list.isEmpty()) {
        throw new Exception("The following artifacts could not be downloaded: " + list);
    }

    if (/* result.getArtifacts().isEmpty() && */!result.getExceptions().isEmpty()) {
        throw new Exception("Error resolving artifact " + artifact, result.getExceptions().get(0));
    }

    Set<Artifact> rc = result.getArtifacts();
    if (online) {
        // Have the DB index the installed the artifacts.
        final LinkedHashSet<String> installed = new LinkedHashSet<String>();
        for (Artifact a : rc) {
            installed.add(a.getId());
        }
        database(false, new DBCallback() {
            public void execute(Database database) throws Exception {
                database.install(installed);
            }
        });
    }

    debug("  Resolved: " + id);
    for (Artifact a : rc) {
        debug("    depends on: " + a.getId() + ", scope: " + a.getScope() + ", optional: " + a.isOptional()
                + ", file: " + a.getFile());
    }

    return rc;

}

From source file:org.sourcepit.osgifier.maven.context.MavenDependencyWalker.java

License:Apache License

private Set<Artifact> resolve(Request walkerRequest, Artifact artifact, MavenProject resolutionContext,
        boolean resolveRoot, List<Dependency> dependencies) {
    final ArtifactResolutionRequest request = new ArtifactResolutionRequest();
    request.setArtifact(artifact);// w ww.  j  a  v  a 2 s  .  co m
    request.setResolveRoot(resolveRoot);
    request.setResolveTransitively(!resolveRoot);

    if (dependencies != null) {
        final Set<Artifact> artifactDependencies = new LinkedHashSet<Artifact>();
        for (Dependency dependency : dependencies) {
            artifactDependencies.add(repositorySystem.createArtifactWithClassifier(dependency.getGroupId(),
                    dependency.getArtifactId(), dependency.getVersion(), dependency.getType(),
                    dependency.getClassifier()));
        }
        request.setArtifactDependencies(artifactDependencies);
    }

    request.setLocalRepository(walkerRequest.getLocalRepository());

    final List<ArtifactRepository> remoteRepositories = new ArrayList<ArtifactRepository>();
    final Set<String> repoIds = new HashSet<String>();
    final List<ArtifactRepository> requestRepos = walkerRequest.getRemoteRepositories();
    if (requestRepos != null) {
        for (ArtifactRepository artifactRepository : requestRepos) {
            repoIds.add(artifactRepository.getId());
            remoteRepositories.add(artifactRepository);
        }
    }

    if (resolutionContext != null) {
        for (ArtifactRepository artifactRepository : resolutionContext.getRemoteArtifactRepositories()) {
            if (repoIds.add(artifactRepository.getId())) {
                remoteRepositories.add(artifactRepository);
            }
        }
        request.setManagedVersionMap(resolutionContext.getManagedVersionMap());
    }

    request.setRemoteRepositories(remoteRepositories);

    final ArtifactFilter artifactFilter = walkerRequest.getArtifactFilter();

    request.setCollectionFilter(artifactFilter);
    request.setResolutionFilter(artifactFilter);

    ArtifactResolutionResult result = repositorySystem.resolve(request);

    final Exception ex;
    if (result.hasExceptions()) {
        ex = result.getExceptions().get(0);
    } else if (result.hasCircularDependencyExceptions()) {
        ex = result.getCircularDependencyException(0);
    } else if (result.hasErrorArtifactExceptions()) {
        ex = result.getErrorArtifactExceptions().get(0);
    } else if (result.hasMetadataResolutionExceptions()) {
        ex = result.getMetadataResolutionException(0);
    } else {
        ex = null;
    }
    if (ex != null) {
        throw new IllegalStateException(ex);
    }

    for (Artifact missingArtifact : result.getMissingArtifacts()) {
        throw Exceptions.pipe(new ArtifactNotFoundException("Unable to resolve artifact", missingArtifact));
    }

    return result.getArtifacts();
}