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);
}
}
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);
}
}
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());
}
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());
}
}
}
}
}
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();
}
Aggregations