use of org.eclipse.aether.RepositorySystemSession in project che by eclipse.
the class CheArtifactResolver method resolve.
// ------------------------------------------------------------------------
//
// ------------------------------------------------------------------------
public ArtifactResolutionResult resolve(ArtifactResolutionRequest request) {
Artifact rootArtifact = request.getArtifact();
Set<Artifact> artifacts = request.getArtifactDependencies();
Map<String, Artifact> managedVersions = request.getManagedVersionMap();
List<ResolutionListener> listeners = request.getListeners();
ArtifactFilter collectionFilter = request.getCollectionFilter();
ArtifactFilter resolutionFilter = request.getResolutionFilter();
RepositorySystemSession session = getSession(request.getLocalRepository());
//TODO: hack because metadata isn't generated in m2e correctly and i want to run the maven i have in the workspace
if (source == null) {
try {
source = container.lookup(ArtifactMetadataSource.class);
} catch (ComponentLookupException e) {
// won't happen
}
}
if (listeners == null) {
listeners = new ArrayList<ResolutionListener>();
if (logger.isDebugEnabled()) {
listeners.add(new DebugResolutionListener(logger));
}
listeners.add(new WarningResolutionListener(logger));
}
ArtifactResolutionResult result = new ArtifactResolutionResult();
if (request.isResolveRoot()) /* && rootArtifact.getFile() == null */
{
try {
resolve(rootArtifact, request.getRemoteRepositories(), session);
} catch (ArtifactResolutionException e) {
result.addErrorArtifactException(e);
return result;
} catch (ArtifactNotFoundException e) {
result.addMissingArtifact(request.getArtifact());
return result;
}
}
ArtifactResolutionRequest collectionRequest = request;
if (request.isResolveTransitively()) {
MetadataResolutionRequest metadataRequest = new DefaultMetadataResolutionRequest(request);
metadataRequest.setArtifact(rootArtifact);
metadataRequest.setResolveManagedVersions(managedVersions == null);
try {
ResolutionGroup resolutionGroup = source.retrieve(metadataRequest);
if (managedVersions == null) {
managedVersions = resolutionGroup.getManagedVersions();
}
Set<Artifact> directArtifacts = resolutionGroup.getArtifacts();
if (artifacts == null || artifacts.isEmpty()) {
artifacts = directArtifacts;
} else {
List<Artifact> allArtifacts = new ArrayList<Artifact>();
allArtifacts.addAll(artifacts);
allArtifacts.addAll(directArtifacts);
Map<String, Artifact> mergedArtifacts = new LinkedHashMap<String, Artifact>();
for (Artifact artifact : allArtifacts) {
String conflictId = artifact.getDependencyConflictId();
if (!mergedArtifacts.containsKey(conflictId)) {
mergedArtifacts.put(conflictId, artifact);
}
}
artifacts = new LinkedHashSet<Artifact>(mergedArtifacts.values());
}
collectionRequest = new ArtifactResolutionRequest(request);
collectionRequest.setServers(request.getServers());
collectionRequest.setMirrors(request.getMirrors());
collectionRequest.setProxies(request.getProxies());
collectionRequest.setRemoteRepositories(resolutionGroup.getResolutionRepositories());
} catch (ArtifactMetadataRetrievalException e) {
ArtifactResolutionException are = new ArtifactResolutionException("Unable to get dependency information for " + rootArtifact.getId() + ": " + e.getMessage(), rootArtifact, metadataRequest.getRemoteRepositories(), e);
result.addMetadataResolutionException(are);
return result;
}
}
if (artifacts == null || artifacts.isEmpty()) {
if (request.isResolveRoot()) {
result.addArtifact(rootArtifact);
}
return result;
}
// After the collection we will have the artifact object in the result but they will not be resolved yet.
result = artifactCollector.collect(artifacts, rootArtifact, managedVersions, collectionRequest, source, collectionFilter, listeners, null);
if (result.hasMetadataResolutionExceptions() || result.hasVersionRangeViolations() || result.hasCircularDependencyExceptions()) {
return result;
}
if (result.getArtifactResolutionNodes() != null) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
CountDownLatch latch = new CountDownLatch(result.getArtifactResolutionNodes().size());
for (ResolutionNode node : result.getArtifactResolutionNodes()) {
Artifact artifact = node.getArtifact();
if (resolutionFilter == null || resolutionFilter.include(artifact)) {
executor.execute(new ResolveTask(classLoader, latch, artifact, session, node.getRemoteRepositories(), result));
} else {
latch.countDown();
}
}
try {
latch.await();
} catch (InterruptedException e) {
result.addErrorArtifactException(new ArtifactResolutionException("Resolution interrupted", rootArtifact, e));
}
}
// have been resolved.
if (request.isResolveRoot()) {
// Add the root artifact (as the first artifact to retain logical order of class path!)
Set<Artifact> allArtifacts = new LinkedHashSet<Artifact>();
allArtifacts.add(rootArtifact);
allArtifacts.addAll(result.getArtifacts());
result.setArtifacts(allArtifacts);
}
return result;
}
use of org.eclipse.aether.RepositorySystemSession in project intellij-community by JetBrains.
the class CustomMaven3ArtifactResolver method resolve.
// ------------------------------------------------------------------------
//
// ------------------------------------------------------------------------
public ArtifactResolutionResult resolve(ArtifactResolutionRequest request) {
Artifact rootArtifact = request.getArtifact();
Set<Artifact> artifacts = request.getArtifactDependencies();
Map<String, Artifact> managedVersions = request.getManagedVersionMap();
List<ResolutionListener> listeners = request.getListeners();
ArtifactFilter collectionFilter = request.getCollectionFilter();
ArtifactFilter resolutionFilter = request.getResolutionFilter();
RepositorySystemSession session = getSession(request.getLocalRepository());
//TODO: hack because metadata isn't generated in m2e correctly and i want to run the maven i have in the workspace
if (source == null) {
try {
source = container.lookup(ArtifactMetadataSource.class);
} catch (ComponentLookupException e) {
// won't happen
}
}
if (listeners == null) {
listeners = new ArrayList<ResolutionListener>();
if (logger.isDebugEnabled()) {
listeners.add(new DebugResolutionListener(logger));
}
listeners.add(new WarningResolutionListener(logger));
}
ArtifactResolutionResult result = new ArtifactResolutionResult();
if (request.isResolveRoot()) /* && rootArtifact.getFile() == null */
{
try {
resolve(rootArtifact, request.getRemoteRepositories(), session);
} catch (ArtifactResolutionException e) {
result.addErrorArtifactException(e);
return result;
} catch (ArtifactNotFoundException e) {
result.addMissingArtifact(request.getArtifact());
return result;
}
}
ArtifactResolutionRequest collectionRequest = request;
if (request.isResolveTransitively()) {
MetadataResolutionRequest metadataRequest = new DefaultMetadataResolutionRequest(request);
metadataRequest.setArtifact(rootArtifact);
metadataRequest.setResolveManagedVersions(managedVersions == null);
try {
ResolutionGroup resolutionGroup = source.retrieve(metadataRequest);
if (managedVersions == null) {
managedVersions = resolutionGroup.getManagedVersions();
}
Set<Artifact> directArtifacts = resolutionGroup.getArtifacts();
if (artifacts == null || artifacts.isEmpty()) {
artifacts = directArtifacts;
} else {
List<Artifact> allArtifacts = new ArrayList<Artifact>();
allArtifacts.addAll(artifacts);
allArtifacts.addAll(directArtifacts);
Map<String, Artifact> mergedArtifacts = new LinkedHashMap<String, Artifact>();
for (Artifact artifact : allArtifacts) {
String conflictId = artifact.getDependencyConflictId();
if (!mergedArtifacts.containsKey(conflictId)) {
mergedArtifacts.put(conflictId, artifact);
}
}
artifacts = new LinkedHashSet<Artifact>(mergedArtifacts.values());
}
collectionRequest = new ArtifactResolutionRequest(request);
collectionRequest.setServers(request.getServers());
collectionRequest.setMirrors(request.getMirrors());
collectionRequest.setProxies(request.getProxies());
collectionRequest.setRemoteRepositories(resolutionGroup.getResolutionRepositories());
} catch (ArtifactMetadataRetrievalException e) {
ArtifactResolutionException are = new ArtifactResolutionException("Unable to get dependency information for " + rootArtifact.getId() + ": " + e.getMessage(), rootArtifact, metadataRequest.getRemoteRepositories(), e);
result.addMetadataResolutionException(are);
return result;
}
}
if (artifacts == null || artifacts.isEmpty()) {
if (request.isResolveRoot()) {
result.addArtifact(rootArtifact);
}
return result;
}
// After the collection we will have the artifact object in the result but they will not be resolved yet.
result = artifactCollector.collect(artifacts, rootArtifact, managedVersions, collectionRequest, source, collectionFilter, listeners, null);
if (result.hasMetadataResolutionExceptions() || result.hasVersionRangeViolations() || result.hasCircularDependencyExceptions()) {
return result;
}
if (result.getArtifactResolutionNodes() != null) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
CountDownLatch latch = new CountDownLatch(result.getArtifactResolutionNodes().size());
for (ResolutionNode node : result.getArtifactResolutionNodes()) {
Artifact artifact = node.getArtifact();
if (resolutionFilter == null || resolutionFilter.include(artifact)) {
executor.execute(new ResolveTask(classLoader, latch, artifact, session, node.getRemoteRepositories(), result));
} else {
latch.countDown();
}
}
try {
latch.await();
} catch (InterruptedException e) {
result.addErrorArtifactException(new ArtifactResolutionException("Resolution interrupted", rootArtifact, e));
}
}
// have been resolved.
if (request.isResolveRoot()) {
// Add the root artifact (as the first artifact to retain logical order of class path!)
Set<Artifact> allArtifacts = new LinkedHashSet<Artifact>();
allArtifacts.add(rootArtifact);
allArtifacts.addAll(result.getArtifacts());
result.setArtifacts(allArtifacts);
}
return result;
}
use of org.eclipse.aether.RepositorySystemSession in project bazel by bazelbuild.
the class MavenDownloader method download.
/**
* Download the Maven artifact to the output directory. Returns the path to the jar.
*/
public Path download(String name, WorkspaceAttributeMapper mapper, Path outputDirectory, MavenServerValue serverValue) throws IOException, EvalException {
this.name = name;
this.outputDirectory = outputDirectory;
String url = serverValue.getUrl();
Server server = serverValue.getServer();
Artifact artifact;
String artifactId = mapper.get("artifact", Type.STRING);
String sha1 = mapper.isAttributeValueExplicitlySpecified("sha1") ? mapper.get("sha1", Type.STRING) : null;
if (sha1 != null && !KeyType.SHA1.isValid(sha1)) {
throw new IOException("Invalid SHA-1 for maven_jar " + name + ": '" + sha1 + "'");
}
try {
artifact = new DefaultArtifact(artifactId);
} catch (IllegalArgumentException e) {
throw new IOException(e.getMessage());
}
boolean isCaching = repositoryCache.isEnabled() && KeyType.SHA1.isValid(sha1);
if (isCaching) {
Path downloadPath = getDownloadDestination(artifact);
Path cachedDestination = repositoryCache.get(sha1, downloadPath, KeyType.SHA1);
if (cachedDestination != null) {
return cachedDestination;
}
}
MavenConnector connector = new MavenConnector(outputDirectory.getPathString());
RepositorySystem system = connector.newRepositorySystem();
RepositorySystemSession session = connector.newRepositorySystemSession(system);
RemoteRepository repository = new RemoteRepository.Builder(name, MavenServerValue.DEFAULT_ID, url).setAuthentication(new MavenAuthentication(server)).build();
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
artifactRequest.setRepositories(ImmutableList.of(repository));
try {
ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
artifact = artifactResult.getArtifact();
} catch (ArtifactResolutionException e) {
throw new IOException("Failed to fetch Maven dependency: " + e.getMessage());
}
Path downloadPath = outputDirectory.getRelative(artifact.getFile().getAbsolutePath());
// Verify checksum.
if (!Strings.isNullOrEmpty(sha1)) {
RepositoryCache.assertFileChecksum(sha1, downloadPath, KeyType.SHA1);
}
if (isCaching) {
repositoryCache.put(sha1, downloadPath, KeyType.SHA1);
}
return downloadPath;
}
use of org.eclipse.aether.RepositorySystemSession in project spf4j by zolyfarkas.
the class MavenRepositoryUtils method getVersions.
public static List<Version> getVersions(final List<RemoteRepository> repos, final File localRepo, final String groupId, final String artifactId, final String versionExpr) throws VersionRangeResolutionException {
RepositorySystem repositorySystem = getRepositorySystem();
RepositorySystemSession session = getRepositorySystemSession(repositorySystem, localRepo);
return getVersions(groupId, artifactId, versionExpr, repos, repositorySystem, session);
}
use of org.eclipse.aether.RepositorySystemSession in project spf4j by zolyfarkas.
the class MavenRepositoryUtils method resolveArtifact.
public static File resolveArtifact(final List<RemoteRepository> repos, final File localRepo, final String groupId, final String artifactId, final String classifier, final String extension, final String versionExpr) throws ArtifactResolutionException {
RepositorySystem repositorySystem = getRepositorySystem();
RepositorySystemSession session = getRepositorySystemSession(repositorySystem, localRepo);
return resolveArtifact(groupId, artifactId, classifier, extension, versionExpr, repos, repositorySystem, session);
}
Aggregations