Search in sources :

Example 41 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project kie-wb-common by kiegroup.

the class MavenArtifactResolverTest method resolveArtifactOffline.

@Test
public void resolveArtifactOffline() throws Exception {
    final boolean[] executedOffline = { true };
    RepositorySystemSession session = Aether.getAether().getSession();
    assertThat(checksIfArtifactIsPresent(session)).isFalse();
    File file = new File("target/test-classes/fake-uberfire-m2repo-editor-backend-100-SNAPSHOT.jar");
    assertThat(file).exists();
    Artifact artifact = getArtifact();
    artifact = artifact.setFile(file);
    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(artifact);
    ArtifactResult result;
    try {
        Aether.getAether().getSystem().resolveArtifact(session, artifactRequest);
    } catch (ArtifactResolutionException ex) {
        assertThat(ex).isNotNull();
    }
    deployTestJar(artifact, session);
    MavenArtifactResolver resolver = new MavenArtifactResolver() {

        public URI resolve(final String groupId, final String artifactId, final String version) throws Exception {
            return internalResolver(true, groupId, artifactId, version);
        }

        URI resolveEmbedded(final String groupId, final String artifactId, final String version) throws IOException {
            executedOffline[0] = false;
            return super.resolveEmbedded(groupId, artifactId, version);
        }
    };
    URI uri = resolver.resolve(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
    assertThat(uri).isNotNull();
    assertThat(uri.getPath()).endsWith(File.separator + "fake-uberfire-m2repo-editor-backend" + File.separator + "100-SNAPSHOT" + File.separator + "fake-uberfire-m2repo-editor-backend-100-SNAPSHOT.jar");
    result = Aether.getAether().getSystem().resolveArtifact(session, artifactRequest);
    assertThat(result.isMissing()).isFalse();
    assertThat(result.isResolved()).isTrue();
    assertThat(executedOffline[0]).isTrue();
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) File(java.io.File) URI(java.net.URI) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) Test(org.junit.Test)

Example 42 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project kie-wb-common by kiegroup.

the class MavenArtifactResolver method internalResolver.

URI internalResolver(final boolean isOffline, final String groupId, final String artifactId, final String version) throws Exception {
    try {
        if (!isOffline) {
            return resolveEmbedded(groupId, artifactId, version);
        }
        GAV gav = new GAV(groupId, artifactId, version);
        org.eclipse.aether.artifact.Artifact jarArtifact = new DefaultArtifact(gav.getGroupId(), gav.getArtifactId(), JAR_ARTIFACT, gav.getVersion());
        RepositorySystemSession session = Aether.getAether().getSession();
        ArtifactRequest artifactReq = new ArtifactRequest();
        artifactReq.setArtifact(jarArtifact);
        ArtifactResult result = Aether.getAether().getSystem().resolveArtifact(session, artifactReq);
        return result.getArtifact().getFile().toURI();
    } catch (Exception e) {
        logger.error("Unable to get artifact: " + groupId + ":" + artifactId + ":" + version + " from maven repository", e);
        throw new Exception("Unable to get artifact: " + groupId + ":" + artifactId + ":" + version + " from maven repository", e);
    }
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) GAV(org.guvnor.common.services.project.model.GAV) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) IOException(java.io.IOException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 43 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project karaf by apache.

the class Dependency31Helper method resolveById.

@Override
public File resolveById(String id, Log log) throws MojoFailureException {
    if (id.startsWith("mvn:")) {
        if (id.contains("!")) {
            id = id.substring(0, "mvn:".length()) + id.substring(id.indexOf("!") + 1);
        }
        if (id.endsWith("/")) {
            id = id.substring(0, id.length() - 1);
        }
    }
    id = MavenUtil.mvnToAether(id);
    if (log.isDebugEnabled()) {
        log.debug("Resolving artifact " + id + " from " + projectRepositories);
    }
    ArtifactResult result;
    try {
        result = resolveArtifact(MavenUtil.aetherToArtifact(id));
    } catch (ArtifactResolutionException e) {
        log.warn("Could not resolve " + id, e);
        throw new MojoFailureException(format("Couldn't resolve artifact %s", id), e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Resolved artifact " + id + " to " + result.getArtifact().getFile() + " from " + result.getRepository());
    }
    return result.getArtifact().getFile();
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 44 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project spf4j by zolyfarkas.

the class MavenRepositoryUtils method resolveArtifact.

public static File resolveArtifact(final String groupId, final String artifactId, final String classifier, final String extension, final String versionExpr, final List<RemoteRepository> repos, final RepositorySystem repositorySystem, final RepositorySystemSession session) throws ArtifactResolutionException {
    Artifact artifact = new DefaultArtifact(groupId, artifactId, classifier, extension, versionExpr);
    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(artifact);
    artifactRequest.setRepositories(repos);
    ArtifactResult artifactResult = repositorySystem.resolveArtifact(session, artifactRequest);
    artifact = artifactResult.getArtifact();
    return artifact.getFile();
}
Also used : ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 45 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project che by eclipse.

the class CheArtifactResolver method resolveOrig.

private void resolveOrig(Artifact artifact, List<ArtifactRepository> remoteRepositories, RepositorySystemSession session) throws ArtifactResolutionException, ArtifactNotFoundException {
    if (artifact == null) {
        return;
    }
    if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
        File systemFile = artifact.getFile();
        if (systemFile == null) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " has no file attached", artifact);
        }
        if (!systemFile.exists()) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " not found in path: " + systemFile, artifact);
        }
        if (!systemFile.isFile()) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " is not a file: " + systemFile, artifact);
        }
        artifact.setResolved(true);
        return;
    }
    if (!artifact.isResolved()) {
        ArtifactResult result;
        try {
            ArtifactRequest artifactRequest = new ArtifactRequest();
            artifactRequest.setArtifact(RepositoryUtils.toArtifact(artifact));
            artifactRequest.setRepositories(RepositoryUtils.toRepos(remoteRepositories));
            // 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(artifactRequest.getArtifact());
            artifact.setFile(new File(lrm.getRepository().getBasedir(), path));
            result = repoSystem.resolveArtifact(session, artifactRequest);
        } catch (org.eclipse.aether.resolution.ArtifactResolutionException e) {
            if (e.getCause() instanceof org.eclipse.aether.transfer.ArtifactNotFoundException) {
                throw new ArtifactNotFoundException(e.getMessage(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), artifact.getClassifier(), remoteRepositories, artifact.getDownloadUrl(), artifact.getDependencyTrail(), e);
            } else {
                throw new ArtifactResolutionException(e.getMessage(), artifact, remoteRepositories, e);
            }
        }
        artifact.selectVersion(result.getArtifact().getVersion());
        artifact.setFile(result.getArtifact().getFile());
        artifact.setResolved(true);
        if (artifact.isSnapshot()) {
            Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion());
            if (matcher.matches()) {
                Snapshot snapshot = new Snapshot();
                snapshot.setTimestamp(matcher.group(2));
                try {
                    snapshot.setBuildNumber(Integer.parseInt(matcher.group(3)));
                    artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot));
                } catch (NumberFormatException e) {
                    logger.warn("Invalid artifact version " + artifact.getVersion() + ": " + e.getMessage());
                }
            }
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) Snapshot(org.apache.maven.artifact.repository.metadata.Snapshot) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) LocalRepositoryManager(org.eclipse.aether.repository.LocalRepositoryManager) LegacyLocalRepositoryManager(org.apache.maven.artifact.repository.LegacyLocalRepositoryManager) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) File(java.io.File) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException)

Aggregations

ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)83 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)53 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)51 Artifact (org.eclipse.aether.artifact.Artifact)41 File (java.io.File)37 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)37 IOException (java.io.IOException)16 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)16 ArrayList (java.util.ArrayList)15 Dependency (org.eclipse.aether.graph.Dependency)15 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)13 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)13 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)11 CollectRequest (org.eclipse.aether.collection.CollectRequest)11 ArtifactDescriptorResult (org.eclipse.aether.resolution.ArtifactDescriptorResult)10 FileNotFoundException (java.io.FileNotFoundException)9 RepositorySystem (org.eclipse.aether.RepositorySystem)8 Test (org.junit.Test)8 DependencyFilter (org.eclipse.aether.graph.DependencyFilter)7