package scala_maven;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.repository.RepositorySystem;

import java.util.Set;

public class MavenArtifactResolver {

    private final RepositorySystem repositorySystem;
    private final MavenSession session;

    public MavenArtifactResolver(RepositorySystem repositorySystem, MavenSession session) {
        this.repositorySystem = repositorySystem;
        this.session = session;
    }

    public Artifact getJar(String groupId, String artifactId, String version, String classifier) {
        Artifact artifact = createJarArtifact(groupId, artifactId, version, classifier);
        return resolve(artifact, false).iterator().next();
    }

    public Set<Artifact> getJarAndDependencies(String groupId, String artifactId, String version, String classifier) {
        Artifact artifact = createJarArtifact(groupId, artifactId, version, classifier);
        return resolve(artifact, true);
    }

    private Artifact createJarArtifact(String groupId, String artifactId, String version, String classifier) {
        return classifier == null ? repositorySystem.createArtifact(groupId, artifactId, version, ScalaMojoSupport.JAR)
            : repositorySystem.createArtifactWithClassifier(groupId, artifactId, version, ScalaMojoSupport.JAR,
                classifier);
    }

    private Set<Artifact> resolve(Artifact artifact, boolean transitively) {
        ArtifactResolutionRequest request = new ArtifactResolutionRequest() //
            .setArtifact(artifact) //
            .setResolveRoot(true) //
            .setResolveTransitively(transitively) //
            .setServers(session.getRequest().getServers()) //
            .setMirrors(session.getRequest().getMirrors()) //
            .setProxies(session.getRequest().getProxies()) //
            .setLocalRepository(session.getLocalRepository()) //
            .setRemoteRepositories(session.getCurrentProject().getRemoteArtifactRepositories());
        return repositorySystem.resolve(request).getArtifacts();
    }
}

@@ -229,16 +229,57 @@
 ystem;%0A%0A
+import java.util.NoSuchElementException;%0A
 import j
@@ -798,38 +798,204 @@
 r);%0A        
-return
+Set%3CArtifact%3E resolvedArtifacts = resolve(artifact, false);%0A        if (resolvedArtifacts.isEmpty()) %7B%0A            throw new NoSuchElementException(String.format(%22Could not
  resolve
 (artifact, f
@@ -978,33 +978,33 @@
  not resolve
-(
+ 
 artifact
 , false).ite
@@ -991,24 +991,117 @@
 artifact
-, false)
+ %25s:%25s:%25s:%25s%22, groupId, artifactId, version, classifier));%0A        %7D%0A        return resolvedArtifacts
 .iterato

Improve error message when artifact can't be resolved.