use of org.eclipse.aether.RepositorySystemSession in project liferay-ide by liferay.
the class AetherUtil method getLatestAvailableArtifact.
public static Artifact getLatestAvailableArtifact(String gavCoords) {
Artifact retval = null;
RepositorySystem system = newRepositorySystem();
RepositorySystemSession session = newRepositorySystemSession(system);
String latestVersion = getLatestVersion(gavCoords, system, session);
String[] gav = gavCoords.split(":");
Artifact defaultArtifact = new DefaultArtifact(gav[0] + ":" + gav[1] + ":" + latestVersion);
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(defaultArtifact);
artifactRequest.addRepository(newCentralRepository());
try {
ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
retval = artifactResult.getArtifact();
} catch (ArtifactResolutionException e) {
LiferayMavenCore.logError("Unable to get latest Liferay archetype", e);
artifactRequest.setArtifact(new DefaultArtifact(gavCoords));
try {
retval = system.resolveArtifact(session, artifactRequest).getArtifact();
} catch (ArtifactResolutionException e1) {
LiferayMavenCore.logError("Unable to get default Liferay archetype", e1);
}
}
if (retval == null) {
retval = defaultArtifact;
}
return retval;
}
use of org.eclipse.aether.RepositorySystemSession in project revapi by revapi.
the class ArtifactResolver method resolveNewestMatching.
/**
* Tries to find the newest version of the artifact that matches given regular expression.
* The found version will be older than the {@code upToVersion} or newest available if {@code upToVersion} is null.
*
* @param gav the coordinates of the artifact. The version part is ignored
* @param upToVersion the version up to which the versions will be matched
* @param versionMatcher the matcher to match the version
* @param remoteOnly true if only remotely available artifacts should be considered
* @param upToInclusive whether the {@code upToVersion} should be considered inclusive or exclusive
* @return the resolved artifact
* @throws VersionRangeResolutionException
*/
public Artifact resolveNewestMatching(String gav, @Nullable String upToVersion, Pattern versionMatcher, boolean remoteOnly, boolean upToInclusive) throws VersionRangeResolutionException, ArtifactResolutionException {
Artifact artifact = new DefaultArtifact(gav);
artifact = artifact.setVersion(upToVersion == null ? "[,)" : "[," + upToVersion + (upToInclusive ? "]" : ")"));
VersionRangeRequest rangeRequest = new VersionRangeRequest(artifact, repositories, null);
RepositorySystemSession session = remoteOnly ? makeRemoteOnly(this.session) : this.session;
VersionRangeResult result = repositorySystem.resolveVersionRange(session, rangeRequest);
List<Version> versions = new ArrayList<>(result.getVersions());
Collections.reverse(versions);
for (Version v : versions) {
if (versionMatcher.matcher(v.toString()).matches()) {
return resolveArtifact(artifact.setVersion(v.toString()), session);
}
}
throw new VersionRangeResolutionException(result) {
@Override
public String getMessage() {
return "Failed to find a version of artifact '" + gav + "' that would correspond to an expression '" + versionMatcher + "'. The versions found were: " + versions;
}
};
}
use of org.eclipse.aether.RepositorySystemSession in project revapi by revapi.
the class Analyzer method resolveArtifacts.
@SuppressWarnings("unchecked")
void resolveArtifacts() {
if (resolvedOldApi == null) {
final ArtifactResolver resolver = new ArtifactResolver(repositorySystem, repositorySystemSession, project.getRemoteProjectRepositories());
Function<String, MavenArchive> toFileArchive = gav -> {
try {
Artifact a = resolveConstrained(project, gav, versionRegex, resolver);
return MavenArchive.of(a);
} catch (ArtifactResolutionException | VersionRangeResolutionException | IllegalArgumentException e) {
throw new MarkerException(e.getMessage(), e);
}
};
List<MavenArchive> oldArchives = new ArrayList<>(1);
try {
if (oldGavs != null) {
oldArchives = Stream.of(oldGavs).map(toFileArchive).collect(toList());
}
if (oldArtifacts != null) {
oldArchives.addAll(Stream.of(oldArtifacts).map(MavenArchive::of).collect(toList()));
}
} catch (MarkerException | IllegalArgumentException e) {
String message = "Failed to resolve old artifacts: " + e.getMessage() + ".";
if (failOnMissingArchives) {
throw new IllegalStateException(message, e);
} else {
log.warn(message + " The API analysis will proceed comparing the new archives against an empty" + " archive.");
}
}
List<MavenArchive> newArchives = new ArrayList<>(1);
try {
if (newGavs != null) {
newArchives = Stream.of(newGavs).map(toFileArchive).collect(toList());
}
if (newArtifacts != null) {
newArchives.addAll(Stream.of(newArtifacts).map(MavenArchive::of).collect(toList()));
}
} catch (MarkerException | IllegalArgumentException e) {
String message = "Failed to resolve new artifacts: " + e.getMessage() + ".";
if (failOnMissingArchives) {
throw new IllegalStateException(message, e);
} else {
log.warn(message + " The API analysis will not proceed.");
return;
}
}
// now we need to be a little bit clever. When using RELEASE or LATEST as the version of the old artifact
// it might happen that it gets resolved to the same version as the new artifacts - this notoriously happens
// when releasing using the release plugin - you first build your artifacts, put them into the local repo
// and then do the site updates for the released version. When you do the site, maven will find the released
// version in the repo and resolve RELEASE to it. You compare it against what you just built, i.e. the same
// code, et voila, the site report doesn't ever contain any found differences...
Set<MavenArchive> oldTransitiveDeps = new HashSet<>();
Set<MavenArchive> newTransitiveDeps = new HashSet<>();
if (resolveDependencies) {
String[] resolvedOld = oldArchives.stream().map(MavenArchive::getName).toArray(String[]::new);
String[] resolvedNew = newArchives.stream().map(MavenArchive::getName).toArray(String[]::new);
oldTransitiveDeps.addAll(collectDeps("old", resolver, resolvedOld));
newTransitiveDeps.addAll(collectDeps("new", resolver, resolvedNew));
}
resolvedOldApi = API.of(oldArchives).supportedBy(oldTransitiveDeps).build();
resolvedNewApi = API.of(newArchives).supportedBy(newTransitiveDeps).build();
}
}
use of org.eclipse.aether.RepositorySystemSession in project drools by kiegroup.
the class MavenProjectLoader method parseMavenPom.
public static MavenProject parseMavenPom(InputStream pomStream, boolean offline) {
MavenEmbedder mavenEmbedder = null;
try {
mavenEmbedder = newMavenEmbedder(offline);
final MavenProject project = mavenEmbedder.readProject(pomStream);
if (IS_FORCE_OFFLINE) {
final Set<Artifact> artifacts = new HashSet<>();
final RepositorySystemSession session = Aether.getAether().getSession();
for (Dependency dep : project.getDependencies()) {
Artifact artifact = new DefaultArtifact(dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getScope(), dep.getType(), dep.getClassifier(), new DefaultArtifactHandler());
if (resolve(session, artifact)) {
artifacts.add(artifact);
} else {
log.error("Artifact can't be resolved {}'", artifact.toString());
}
}
if (!artifacts.isEmpty()) {
project.setArtifacts(artifacts);
}
}
return project;
} catch (Exception e) {
log.error("Unable to create MavenProject from InputStream", e);
throw new RuntimeException(e);
} finally {
if (mavenEmbedder != null) {
mavenEmbedder.dispose();
}
}
}
use of org.eclipse.aether.RepositorySystemSession in project drools by kiegroup.
the class WiredComponentProvider method getRepositorySystemSession.
@Override
public RepositorySystemSession getRepositorySystemSession(MavenExecutionRequest mavenExecutionRequest) throws ComponentLookupException {
DefaultMaven defaultMaven = (DefaultMaven) lookup(Maven.class);
RepositorySystemSession session = defaultMaven.newRepositorySession(mavenExecutionRequest);
inject(session.getArtifactTypeRegistry(), ArtifactHandlerManager.class, "handlerManager");
return session;
}
Aggregations