Search in sources :

Example 21 with ArtifactRequest

use of org.sonatype.aether.resolution.ArtifactRequest in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testVersionResolverFails.

@Test
public void testVersionResolverFails() {
    resolver.setVersionResolver(new VersionResolver() {

        public VersionResult resolveVersion(RepositorySystemSession session, VersionRequest request) throws VersionResolutionException {
            throw new VersionResolutionException(new VersionResult(request));
        }
    });
    ArtifactRequest request = new ArtifactRequest(artifact, null, "");
    try {
        resolver.resolveArtifact(session, request);
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
        assertNotNull(e.getResults());
        assertEquals(1, e.getResults().size());
        ArtifactResult result = e.getResults().get(0);
        assertSame(request, result.getRequest());
        assertFalse(result.getExceptions().isEmpty());
        assertTrue(result.getExceptions().get(0) instanceof VersionResolutionException);
        Artifact resolved = result.getArtifact();
        assertNull(resolved);
    }
}
Also used : RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) TestRepositorySystemSession(org.sonatype.aether.test.impl.TestRepositorySystemSession) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) VersionResolver(org.sonatype.aether.impl.VersionResolver) VersionResult(org.sonatype.aether.resolution.VersionResult) VersionRequest(org.sonatype.aether.resolution.VersionRequest) VersionResolutionException(org.sonatype.aether.resolution.VersionResolutionException) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) Test(org.junit.Test)

Example 22 with ArtifactRequest

use of org.sonatype.aether.resolution.ArtifactRequest in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testResolveRemoteArtifactUnsuccessful.

@Test
public void testResolveRemoteArtifactUnsuccessful() throws IOException, ArtifactResolutionException {
    RecordingRepositoryConnector connector = new RecordingRepositoryConnector() {

        @Override
        public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
            super.get(artifactDownloads, metadataDownloads);
            ArtifactDownload download = artifactDownloads.iterator().next();
            ArtifactTransferException exception = new ArtifactNotFoundException(download.getArtifact(), null, "not found");
            download.setException(exception);
        }
    };
    connector.setExpectGet(artifact);
    remoteRepositoryManager.setConnector(connector);
    ArtifactRequest request = new ArtifactRequest(artifact, null, "");
    request.addRepository(new RemoteRepository("id", "default", "file:///"));
    try {
        resolver.resolveArtifact(session, request);
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
        assertNotNull(e.getResults());
        assertEquals(1, e.getResults().size());
        ArtifactResult result = e.getResults().get(0);
        assertSame(request, result.getRequest());
        assertFalse(result.getExceptions().isEmpty());
        assertTrue(result.getExceptions().get(0) instanceof ArtifactNotFoundException);
        Artifact resolved = result.getArtifact();
        assertNull(resolved);
    }
}
Also used : ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) Collection(java.util.Collection) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) Test(org.junit.Test)

Example 23 with ArtifactRequest

use of org.sonatype.aether.resolution.ArtifactRequest in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testRepositoryEventsOnVersionResolverFail.

@Test
public void testRepositoryEventsOnVersionResolverFail() {
    resolver.setVersionResolver(new VersionResolver() {

        public VersionResult resolveVersion(RepositorySystemSession session, VersionRequest request) throws VersionResolutionException {
            throw new VersionResolutionException(new VersionResult(request));
        }
    });
    RecordingRepositoryListener listener = new RecordingRepositoryListener();
    session.setRepositoryListener(listener);
    ArtifactRequest request = new ArtifactRequest(artifact, null, "");
    try {
        resolver.resolveArtifact(session, request);
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
    }
    List<EventWrapper> events = listener.getEvents();
    assertEquals(2, events.size());
    EventWrapper event = events.get(0);
    assertEquals(artifact, event.getEvent().getArtifact());
    assertEquals(Type.ARTIFACT_RESOLVING, event.getType());
    event = events.get(1);
    assertEquals(artifact, event.getEvent().getArtifact());
    assertEquals(Type.ARTIFACT_RESOLVED, event.getType());
    assertNotNull(event.getEvent().getException());
    assertEquals(1, event.getEvent().getExceptions().size());
}
Also used : RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) TestRepositorySystemSession(org.sonatype.aether.test.impl.TestRepositorySystemSession) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) VersionResolver(org.sonatype.aether.impl.VersionResolver) VersionResult(org.sonatype.aether.resolution.VersionResult) VersionRequest(org.sonatype.aether.resolution.VersionRequest) VersionResolutionException(org.sonatype.aether.resolution.VersionResolutionException) RecordingRepositoryListener(org.sonatype.aether.test.impl.RecordingRepositoryListener) EventWrapper(org.sonatype.aether.test.impl.RecordingRepositoryListener.EventWrapper) Test(org.junit.Test)

Example 24 with ArtifactRequest

use of org.sonatype.aether.resolution.ArtifactRequest in project intellij-community by JetBrains.

the class CustomMaven30ArtifactResolver method resolveOld.

private void resolveOld(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.sonatype.aether.resolution.ArtifactResolutionException e) {
            if (e.getCause() instanceof org.sonatype.aether.transfer.ArtifactNotFoundException) {
                throw new ArtifactNotFoundException(e.getMessage(), artifact, remoteRepositories, 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.sonatype.aether.resolution.ArtifactResult) Snapshot(org.apache.maven.artifact.repository.metadata.Snapshot) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) LegacyLocalRepositoryManager(org.apache.maven.artifact.repository.LegacyLocalRepositoryManager) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) File(java.io.File)

Example 25 with ArtifactRequest

use of org.sonatype.aether.resolution.ArtifactRequest in project karaf by apache.

the class Dependency30Helper 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);
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(new DefaultArtifact(id));
    request.setRepositories(projectRepositories);
    log.debug("Resolving artifact " + id + " from " + projectRepositories);
    ArtifactResult result;
    try {
        result = repositorySystem.resolveArtifact(repositorySystemSession, request);
    } catch (ArtifactResolutionException e) {
        log.warn("Could not resolve " + id, e);
        throw new MojoFailureException(format("Couldn't resolve artifact %s", id), e);
    }
    log.debug("Resolved artifact " + id + " to " + result.getArtifact().getFile() + " from " + result.getRepository());
    return result.getArtifact().getFile();
}
Also used : ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DefaultArtifact(org.sonatype.aether.util.artifact.DefaultArtifact) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult)

Aggregations

ArtifactRequest (org.sonatype.aether.resolution.ArtifactRequest)25 LocalArtifactRequest (org.sonatype.aether.repository.LocalArtifactRequest)18 ArtifactResult (org.sonatype.aether.resolution.ArtifactResult)18 Test (org.junit.Test)16 Artifact (org.sonatype.aether.artifact.Artifact)14 LocalArtifactResult (org.sonatype.aether.repository.LocalArtifactResult)12 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)12 ArtifactResolutionException (org.sonatype.aether.resolution.ArtifactResolutionException)12 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)11 File (java.io.File)8 TestRepositorySystemSession (org.sonatype.aether.test.impl.TestRepositorySystemSession)7 RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)6 IOException (java.io.IOException)5 LocalRepositoryManager (org.sonatype.aether.repository.LocalRepositoryManager)5 VersionRequest (org.sonatype.aether.resolution.VersionRequest)5 VersionResolutionException (org.sonatype.aether.resolution.VersionResolutionException)5 VersionResult (org.sonatype.aether.resolution.VersionResult)5 RecordingRepositoryListener (org.sonatype.aether.test.impl.RecordingRepositoryListener)5 EventWrapper (org.sonatype.aether.test.impl.RecordingRepositoryListener.EventWrapper)5 ArtifactNotFoundException (org.sonatype.aether.transfer.ArtifactNotFoundException)5