use of org.eclipse.aether.resolution.ArtifactRequest in project bnd by bndtools.
the class AetherRepository method get.
@Override
public File get(String bsn, Version version, Map<String, String> properties, DownloadListener... listeners) throws Exception {
init();
// Use the index by preference
if (indexedRepo != null)
return indexedRepo.get(ConversionUtils.maybeMavenCoordsToBsn(bsn), version, properties, listeners);
File file = null;
boolean getSource = false;
try {
// Setup the Aether repo session and request
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, localRepo));
if (bsn.endsWith(".source")) {
String originalBsn = properties.get("bsn");
if (originalBsn != null) {
bsn = originalBsn;
getSource = true;
}
}
String[] coords = ConversionUtils.getGroupAndArtifactForBsn(bsn);
MavenVersion mvnVersion = new MavenVersion(version);
String versionStr = null;
if ("exact".equals(properties.get("strategy")) || getSource) {
versionStr = properties.get("version");
} else {
versionStr = mvnVersion.toString();
}
Artifact artifact = null;
if (getSource) {
artifact = new DefaultArtifact(coords[0], coords[1], "sources", "jar", versionStr);
} else {
artifact = new DefaultArtifact(coords[0], coords[1], "jar", versionStr);
}
ArtifactRequest request = new ArtifactRequest();
request.setArtifact(artifact);
request.setRepositories(Collections.singletonList(remoteRepo));
// Log the transfer
session.setTransferListener(new AbstractTransferListener() {
@Override
public void transferStarted(TransferEvent event) throws TransferCancelledException {
System.err.println(event);
}
@Override
public void transferSucceeded(TransferEvent event) {
System.err.println(event);
}
@Override
public void transferFailed(TransferEvent event) {
System.err.println(event);
}
});
try {
// Resolve the version
ArtifactResult artifactResult = repoSystem.resolveArtifact(session, request);
artifact = artifactResult.getArtifact();
file = artifact.getFile();
} catch (ArtifactResolutionException ex) {
// could not download artifact, simply return null
}
return file;
} finally {
for (DownloadListener dl : listeners) {
if (file != null)
dl.success(file);
else
dl.failure(null, "Download failed");
}
}
}
use of org.eclipse.aether.resolution.ArtifactRequest in project bnd by bndtools.
the class DependencyResolver method discoverArtifacts.
private void discoverArtifacts(Map<File, ArtifactResult> files, List<DependencyNode> nodes, String parent, List<RemoteRepository> remoteRepositories) throws MojoExecutionException {
for (DependencyNode node : nodes) {
// Ensure that the file is downloaded so we can index it
try {
ArtifactResult resolvedArtifact = postProcessor.postProcessResult(system.resolveArtifact(session, new ArtifactRequest(node.getArtifact(), remoteRepositories, parent)));
logger.debug("Located file: {} for artifact {}", resolvedArtifact.getArtifact().getFile(), resolvedArtifact);
files.put(resolvedArtifact.getArtifact().getFile(), resolvedArtifact);
} catch (ArtifactResolutionException e) {
throw new MojoExecutionException("Failed to resolve the dependency " + node.getArtifact().toString(), e);
}
if (includeTransitive) {
discoverArtifacts(files, node.getChildren(), node.getRequestContext(), remoteRepositories);
} else {
logger.debug("Ignoring transitive dependencies of {}", node.getDependency());
}
}
}
use of org.eclipse.aether.resolution.ArtifactRequest in project bnd by bndtools.
the class RemotePostProcessor method postProcessSnapshot.
private ArtifactResult postProcessSnapshot(ArtifactRequest request, Artifact artifact) throws MojoExecutionException {
for (RemoteRepository repository : request.getRepositories()) {
if (!repository.getPolicy(true).isEnabled()) {
// Skip the repo if it isn't enabled for snapshots
continue;
}
// Remove the workspace from the session so that we don't use it
DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession(session);
newSession.setWorkspaceReader(null);
// Find the snapshot metadata for the module
MetadataRequest mr = new MetadataRequest().setRepository(repository).setMetadata(new DefaultMetadata(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "maven-metadata.xml", SNAPSHOT));
for (MetadataResult metadataResult : system.resolveMetadata(newSession, singletonList(mr))) {
if (metadataResult.isResolved()) {
String version;
try {
Metadata read = metadataReader.read(metadataResult.getMetadata().getFile(), null);
Versioning versioning = read.getVersioning();
if (versioning == null || versioning.getSnapshotVersions() == null || versioning.getSnapshotVersions().isEmpty()) {
continue;
} else {
version = versioning.getSnapshotVersions().get(0).getVersion();
}
} catch (Exception e) {
throw new MojoExecutionException("Unable to read project metadata for " + artifact, e);
}
Artifact fullVersionArtifact = new org.eclipse.aether.artifact.DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), artifact.getExtension(), version);
try {
ArtifactResult result = system.resolveArtifact(newSession, new ArtifactRequest().setArtifact(fullVersionArtifact).addRepository(repository));
if (result.isResolved()) {
File toUse = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForRemoteArtifact(fullVersionArtifact, repository, artifact.toString()));
if (!toUse.exists()) {
logger.warn("The resolved artifact {} does not exist at {}", fullVersionArtifact, toUse);
continue;
} else {
logger.debug("Located snapshot file {} for artifact {}", toUse, artifact);
}
result.getArtifact().setFile(toUse);
return result;
}
} catch (ArtifactResolutionException e) {
logger.debug("Unable to locate the artifact {}", fullVersionArtifact, e);
}
}
}
}
logger.debug("Unable to resolve a remote repository containing {}", artifact);
return null;
}
use of org.eclipse.aether.resolution.ArtifactRequest in project fabric8 by jboss-fuse.
the class AetherBasedResolver method resolve.
private File resolve(List<LocalRepository> defaultRepos, List<RemoteRepository> remoteRepos, Artifact artifact) throws IOException {
if (artifact.getExtension().isEmpty()) {
artifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), "jar", artifact.getVersion());
}
// Try with default repositories
try {
VersionConstraint vc = new GenericVersionScheme().parseVersionConstraint(artifact.getVersion());
if (vc.getVersion() != null) {
for (LocalRepository repo : defaultRepos) {
if (vc.getVersion().toString().endsWith("SNAPSHOT") && !handlesSnapshot(repo)) {
continue;
}
DefaultRepositorySystemSession session = newSession(repo);
try {
return m_repoSystem.resolveArtifact(session, new ArtifactRequest(artifact, null, null)).getArtifact().getFile();
} catch (ArtifactResolutionException e) {
// Ignore
} finally {
releaseSession(session);
}
}
}
} catch (InvalidVersionSpecificationException e) {
// Should not happen
}
DefaultRepositorySystemSession session = newSession(null);
try {
artifact = resolveLatestVersionRange(session, remoteRepos, artifact);
ArtifactResult result = m_repoSystem.resolveArtifact(session, new ArtifactRequest(artifact, remoteRepos, null));
File resolved = result.getArtifact().getFile();
LOG.debug("Resolved ({}) as {}", artifact.toString(), resolved.getAbsolutePath());
return resolved;
} catch (ArtifactResolutionException e) {
// we know there's one ArtifactResult, because there was one ArtifactRequest
ArtifactResolutionException original = new ArtifactResolutionException(e.getResults(), "Error resolving artifact " + artifact.toString(), null);
original.setStackTrace(e.getStackTrace());
List<String> messages = new ArrayList<>(e.getResult().getExceptions().size());
List<Exception> suppressed = new ArrayList<>();
for (Exception ex : e.getResult().getExceptions()) {
messages.add(ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());
suppressed.add(ex);
}
IOException exception = new IOException(original.getMessage() + ": " + messages, original);
for (Exception ex : suppressed) {
exception.addSuppressed(ex);
}
LOG.warn(exception.getMessage(), exception);
for (Exception ex : suppressed) {
LOG.warn(" - " + ex.getMessage());
}
throw exception;
} catch (RepositoryException e) {
throw new IOException("Error resolving artifact " + artifact.toString(), e);
} finally {
releaseSession(session);
}
}
use of org.eclipse.aether.resolution.ArtifactRequest in project elasticsearch-maven-plugin by alexcojocaru.
the class MyArtifactResolver method resolveArtifact.
/**
* Resolves an Artifact from the repositories.
*
* @param coordinates The artifact coordinates
* @return The local file resolved/downloaded for the given coordinates
* @throws ResolutionException If the artifact cannot be resolved
*/
public File resolveArtifact(String coordinates) throws ResolutionException {
ArtifactRequest request = new ArtifactRequest();
Artifact artifact = new DefaultArtifact(coordinates);
request.setArtifact(artifact);
request.setRepositories(remoteRepositories);
log.debug(String.format("Resolving artifact %s from %s", artifact, remoteRepositories));
ArtifactResult result;
try {
result = repositorySystem.resolveArtifact(repositorySession, request);
} catch (ArtifactResolutionException e) {
throw new ResolutionException(e.getMessage(), e);
}
log.debug(String.format("Resolved artifact %s to %s from %s", artifact, result.getArtifact().getFile(), result.getRepository()));
return result.getArtifact().getFile();
}
Aggregations