Example usage for org.apache.maven.repository RepositorySystem createArtifactWithClassifier

List of usage examples for org.apache.maven.repository RepositorySystem createArtifactWithClassifier

Introduction

In this page you can find the example usage for org.apache.maven.repository RepositorySystem createArtifactWithClassifier.

Prototype

Artifact createArtifactWithClassifier(String groupId, String artifactId, String version, String type,
            String classifier);

Source Link

Usage

From source file:com.yelbota.plugins.nd.DependencyHelper.java

License:Apache License

/**
 * Resolves native dependency artifact. First it checks plugin dependencies
 * compares they with getDefaultArtifactId() and getDefaultGroupId(), then method
 * tries to resolve from repositories using also getDefaultVersion(), getDefaultPackaging()
 * and getDefaultClassifier()./*w w w.  j  ava  2  s .co m*/
 *
 * @param pluginArtifacts    list of plugin dependencies (inject this with @parameter)
 * @param repositorySystem   inject this with @parameter
 * @param localRepository    inject this with @parameter
 * @param remoteRepositories inject this with @parameter
 * @return Native dependency artifact
 * @throws MojoFailureException
 */
public Artifact resolve(List<Artifact> pluginArtifacts, RepositorySystem repositorySystem,
        ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
        throws MojoFailureException {

    Artifact artifact = null;

    if (pluginArtifacts != null) {

        // Lookup plugin artifacts.
        for (Artifact pluginArtifact : pluginArtifacts) {

            boolean eqGroupId = pluginArtifact.getGroupId().equals(getDefaultGroupId());
            boolean eqArtifactId = pluginArtifact.getArtifactId().equals(getDefaultArtifactId());

            if (eqGroupId && eqArtifactId) {

                artifact = pluginArtifact;
                break;
            }
        }
    }

    if (artifact != null) {
        return artifact;
    } else {
        // Okay. Lets download sdk
        if (repositorySystem != null) {
            artifact = repositorySystem.createArtifactWithClassifier(

                    getDefaultGroupId(), getDefaultArtifactId(), getDefaultVersion(), getDefaultPackaging(),
                    getDefaultClassifier());

            ArtifactResolutionRequest request = new ArtifactResolutionRequest();

            request.setArtifact(artifact);
            request.setLocalRepository(localRepository);
            request.setRemoteRepositories(remoteRepositories);

            ArtifactResolutionResult resolutionResult = repositorySystem.resolve(request);

            if (!resolutionResult.isSuccess()) {

                String message = "Failed to resolve artifact " + artifact;
                throw new ArtifactResolutionException(message, resolutionResult);
            }
        }

        return artifact;
    }
}

From source file:hudson.maven.MavenEmbedder.java

License:Apache License

public Artifact createArtifactWithClassifier(String groupId, String artifactId, String version, String type,
        String classifier) throws MavenEmbedderException {
    try {/*w  ww . jav  a 2 s.  c  o  m*/
        RepositorySystem repositorySystem = lookup(RepositorySystem.class);
        return repositorySystem.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
    } catch (ComponentLookupException e) {
        throw new MavenEmbedderException(e.getMessage(), e);
    }
}

From source file:org.ebayopensource.turmeric.eclipse.mavenapi.impl.AbstractMavenApi.java

License:Open Source License

/**
 * Resolve an artifact using a particular maven implementation and repository.
 *
 * @param embedder the embedded version of maven to use
 * @param repoSystem the repository to resolve against
 * @param md the artifact metadata to search
 * @return the resolved artifact//w  ww . j av  a2  s  . c om
 * @throws MavenEclipseApiException the maven eclipse api exception
 */
public Artifact resolveArtifact(MavenImpl embedder, RepositorySystem repoSystem, ArtifactMetadata md)
        throws MavenEclipseApiException {
    if (embedder == null)
        throw new MavenEclipseApiException();

    try {
        List<ArtifactRepository> repos = _getKnownRepositories(embedder, md.getType());
        if (repos == null || repos.size() < 1) {
            throw new MavenEclipseApiException(Messages.ERROR_NO_REPOSITORIES);
        }

        Artifact artifact = null;

        if (md.getClassifier() == null)
            artifact = repoSystem.createArtifact(md.getGroupId(), md.getArtifactId(), md.getVersion(),
                    md.getScope(), md.getType());
        else
            artifact = repoSystem.createArtifactWithClassifier(md.getGroupId(), md.getArtifactId(),
                    md.getVersion(), md.getType(), md.getClassifier());

        if (artifact == null) {
            throw new MavenEclipseApiException(Messages.ERROR_NULL_ARTIFACT);
        }

        // embedder.resolve(artifact, repos, embedder.getLocalRepository());
        MavenApiUtil.resolveArtifact(repoSystem, artifact, embedder.getLocalRepository(), repos);

        return artifact;
    } catch (Exception e) {
        e.printStackTrace();
        throw new MavenEclipseApiException(e);
    }
}

From source file:org.ebayopensource.turmeric.eclipse.mavenapi.impl.AbstractMavenEclipseApi.java

License:Open Source License

/**
 * Create an artifact given the metadata.  This populates it into the repository.
 *
 * @param metadata the artifact metadata to use.
 * @return the created artifact.//  ww w .  j av a  2  s.c om
 * @throws MavenEclipseApiException the maven eclipse api exception
 */
public Artifact createArtifact(final ArtifactMetadata metadata) throws MavenEclipseApiException {
    RepositorySystem res = MavenApiHelper.getRepositorySystem();
    if (metadata.getClassifier() == null)
        return res.createArtifact(metadata.getGroupId(), metadata.getArtifactId(), metadata.getVersion(),
                metadata.getScope(), metadata.getType());
    return res.createArtifactWithClassifier(metadata.getGroupId(), metadata.getArtifactId(),
            metadata.getVersion(), metadata.getType(), metadata.getClassifier());
}

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());
                    }/* w  w w.ja  v a 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.phpmaven.project.impl.DependencyHelper.java

License:Apache License

/**
  * Resolves the artifact./* w ww . j a  va  2s . co  m*/
  * @param groupId group id
  * @param artifactId artifact id
  * @param version version
  * @param type type
  * @param classifier classifier
  * @return the resolved artifact
  * @throws PhpException thrown on resolve errors
  */
private static Artifact resolveArtifact(String groupId, String artifactId, String version, String type,
        String classifier, RepositorySystem reposSystem, MavenSession session) throws MojoExecutionException {
    final Artifact artifact = reposSystem.createArtifactWithClassifier(groupId, artifactId, version, type,
            classifier);
    final ArtifactResolutionRequest request = new ArtifactResolutionRequest();
    request.setArtifact(artifact);
    request.setLocalRepository(session.getLocalRepository());
    request.setOffline(session.isOffline());
    final Set<ArtifactRepository> setRepos = new HashSet<ArtifactRepository>(
            session.getRequest().getRemoteRepositories());
    setRepos.addAll(session.getCurrentProject().getRemoteArtifactRepositories());
    request.setRemoteRepositories(new ArrayList<ArtifactRepository>(setRepos));
    final ArtifactResolutionResult result = reposSystem.resolve(request);
    if (!result.isSuccess()) {
        throw new MojoExecutionException(
                "dependency resolution failed for " + groupId + ":" + artifactId + ":" + version);
    }

    final Artifact resultArtifact = result.getArtifacts().iterator().next();
    return resultArtifact;
}

From source file:org.sonatype.gshell.commands.artifact.ResolveCommand.java

License:Apache License

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();//  w w  w  . ja  va2s .  c  o  m

    RepositorySystem rsys = plexus.lookup(RepositorySystem.class);

    ArtifactResolutionRequest request = new ArtifactResolutionRequest();

    String[] items = resolveId.split(":", 3);
    if (items.length != 3) {
        io.error("Invalid artifact resolution id: {}", resolveId); // TODO: i18n
        return Result.FAILURE;
    }

    String groupId = items[0];
    String artifactId = items[1];
    String version = items[2];

    Artifact artifact;
    if (classifier != null) {
        artifact = rsys.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
    } else {
        artifact = rsys.createArtifact(groupId, artifactId, version, type);
    }
    io.println("Resolving artifact: {}", artifact); // TODO: i18n

    //
    // TODO: Bring the ArtifactManager/ArtifactRepsitoryManager back to manage these components

    request.setLocalRepository(rsys.createDefaultLocalRepository());
    request.setRemoteRepositories(Collections.singletonList(rsys.createDefaultRemoteRepository()));

    request.setResolveRoot(true);
    request.setResolveTransitively(transitive);
    request.setArtifact(artifact);

    if (scope != null) {
        io.debug("Using scope: {}", scope);
        request.setCollectionFilter(new ScopeArtifactFilter(scope));
    }

    request.setOffline(offline);
    request.setTransferListener(new ProgressSpinnerMonitor(io));
    ArtifactResolutionResult result = rsys.resolve(request);

    Set<Artifact> artifacts = result.getArtifacts();
    io.println("Resolved artifacts:"); // TODO: i18n
    for (Artifact a : artifacts) {
        io.println("    {}", a);
    }

    return Result.SUCCESS;
}