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

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

Introduction

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

Prototype

Artifact createArtifact(String groupId, String artifactId, String version, String packaging);

Source Link

Usage

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  .  j a v a  2  s.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;
}