use of org.eclipse.aether.RepositorySystemSession in project kie-wb-common by kiegroup.
the class MavenArtifactResolverTest method resolveArtifactOffline.
@Test
public void resolveArtifactOffline() throws Exception {
final boolean[] executedOffline = { true };
RepositorySystemSession session = Aether.getAether().getSession();
assertThat(checksIfArtifactIsPresent(session)).isFalse();
File file = new File("target/test-classes/fake-uberfire-m2repo-editor-backend-100-SNAPSHOT.jar");
assertThat(file).exists();
Artifact artifact = getArtifact();
artifact = artifact.setFile(file);
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
ArtifactResult result;
try {
Aether.getAether().getSystem().resolveArtifact(session, artifactRequest);
} catch (ArtifactResolutionException ex) {
assertThat(ex).isNotNull();
}
deployTestJar(artifact, session);
MavenArtifactResolver resolver = new MavenArtifactResolver() {
public URI resolve(final String groupId, final String artifactId, final String version) throws Exception {
return internalResolver(true, groupId, artifactId, version);
}
URI resolveEmbedded(final String groupId, final String artifactId, final String version) throws IOException {
executedOffline[0] = false;
return super.resolveEmbedded(groupId, artifactId, version);
}
};
URI uri = resolver.resolve(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
assertThat(uri).isNotNull();
assertThat(uri.getPath()).endsWith(File.separator + "fake-uberfire-m2repo-editor-backend" + File.separator + "100-SNAPSHOT" + File.separator + "fake-uberfire-m2repo-editor-backend-100-SNAPSHOT.jar");
result = Aether.getAether().getSystem().resolveArtifact(session, artifactRequest);
assertThat(result.isMissing()).isFalse();
assertThat(result.isResolved()).isTrue();
assertThat(executedOffline[0]).isTrue();
}
use of org.eclipse.aether.RepositorySystemSession in project kie-wb-common by kiegroup.
the class MavenArtifactResolver method internalResolver.
URI internalResolver(final boolean isOffline, final String groupId, final String artifactId, final String version) throws Exception {
try {
if (!isOffline) {
return resolveEmbedded(groupId, artifactId, version);
}
GAV gav = new GAV(groupId, artifactId, version);
org.eclipse.aether.artifact.Artifact jarArtifact = new DefaultArtifact(gav.getGroupId(), gav.getArtifactId(), JAR_ARTIFACT, gav.getVersion());
RepositorySystemSession session = Aether.getAether().getSession();
ArtifactRequest artifactReq = new ArtifactRequest();
artifactReq.setArtifact(jarArtifact);
ArtifactResult result = Aether.getAether().getSystem().resolveArtifact(session, artifactReq);
return result.getArtifact().getFile().toURI();
} catch (Exception e) {
logger.error("Unable to get artifact: " + groupId + ":" + artifactId + ":" + version + " from maven repository", e);
throw new Exception("Unable to get artifact: " + groupId + ":" + artifactId + ":" + version + " from maven repository", e);
}
}
use of org.eclipse.aether.RepositorySystemSession in project che by eclipse.
the class MavenServerImpl method runMavenRequest.
public void runMavenRequest(MavenExecutionRequest request, Runnable runnable) {
DefaultMaven maven = (DefaultMaven) getMavenComponent(Maven.class);
RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request);
request.getProjectBuildingRequest().setRepositorySession(repositorySystemSession);
MavenSession mavenSession = new MavenSession(container, repositorySystemSession, request, new DefaultMavenExecutionResult());
LegacySupport legacySupport = getMavenComponent(LegacySupport.class);
MavenSession previousSession = legacySupport.getSession();
legacySupport.setSession(mavenSession);
try {
for (AbstractMavenLifecycleParticipant participant : getLifecycleParticipants(Collections.emptyList())) {
participant.afterSessionStart(mavenSession);
}
runnable.run();
} catch (MavenExecutionException e) {
throw new RuntimeException(e);
} finally {
legacySupport.setSession(previousSession);
}
}
use of org.eclipse.aether.RepositorySystemSession in project che by eclipse.
the class MavenServerImpl method internalResolveProject.
private MavenResult internalResolveProject(File pom, List<String> activeProfiles, List<String> inactiveProfiles, List<ResolutionListener> dependencyTreeResolutionListeners) {
MavenExecutionRequest request = newMavenRequest(pom, activeProfiles, inactiveProfiles, Collections.emptyList());
request.setUpdateSnapshots(updateSnapshots);
AtomicReference<MavenResult> reference = new AtomicReference<>();
runMavenRequest(request, () -> {
try {
ProjectBuilder builder = getMavenComponent(ProjectBuilder.class);
List<ProjectBuildingResult> resultList = builder.build(Collections.singletonList(pom), false, request.getProjectBuildingRequest());
ProjectBuildingResult result = resultList.get(0);
MavenProject mavenProject = result.getProject();
RepositorySystemSession repositorySession = getMavenComponent(LegacySupport.class).getRepositorySession();
if (repositorySession instanceof DefaultRepositorySystemSession) {
((DefaultRepositorySystemSession) repositorySession).setTransferListener(new ArtifactTransferListener(mavenProgressNotifier));
if (workspaceCache != null) {
((DefaultRepositorySystemSession) repositorySession).setWorkspaceReader(new MavenWorkspaceReader(workspaceCache));
}
}
List<Exception> exceptions = new ArrayList<>();
loadExtensions(mavenProject, exceptions);
mavenProject.setDependencyArtifacts(mavenProject.createArtifacts(getMavenComponent(ArtifactFactory.class), null, null));
ArtifactResolutionRequest resolutionRequest = new ArtifactResolutionRequest();
resolutionRequest.setArtifact(mavenProject.getArtifact());
resolutionRequest.setRemoteRepositories(mavenProject.getRemoteArtifactRepositories());
resolutionRequest.setArtifactDependencies(mavenProject.getDependencyArtifacts());
resolutionRequest.setListeners(dependencyTreeResolutionListeners);
resolutionRequest.setLocalRepository(localRepo);
resolutionRequest.setManagedVersionMap(mavenProject.getManagedVersionMap());
resolutionRequest.setResolveTransitively(true);
resolutionRequest.setResolveRoot(false);
ArtifactResolver resolver = getMavenComponent(ArtifactResolver.class);
ArtifactResolutionResult resolve = resolver.resolve(resolutionRequest);
mavenProject.setArtifacts(resolve.getArtifacts());
reference.set(new MavenResult(mavenProject, exceptions));
} catch (Exception e) {
reference.set(new MavenResult(null, null, Collections.singletonList(e)));
}
});
return reference.get();
}
use of org.eclipse.aether.RepositorySystemSession in project che by eclipse.
the class MavenServerImpl method resolveArtifact.
@Override
public MavenArtifact resolveArtifact(MavenArtifactKey artifactKey, List<MavenRemoteRepository> repositories) throws RemoteException {
Artifact artifact = getMavenComponent(ArtifactFactory.class).createArtifactWithClassifier(artifactKey.getGroupId(), artifactKey.getArtifactId(), artifactKey.getVersion(), artifactKey.getPackaging(), artifactKey.getClassifier());
List<ArtifactRepository> repos = new ArrayList<>();
ArtifactRepositoryFactory factory = getMavenComponent(ArtifactRepositoryFactory.class);
for (MavenRemoteRepository repository : repositories) {
try {
ArtifactRepository artifactRepository = ProjectUtils.buildArtifactRepository(MavenModelUtil.convertToMavenRepository(repository), factory, container);
repos.add(artifactRepository);
} catch (InvalidRepositoryException e) {
MavenServerContext.getLogger().error(e);
}
}
MavenExecutionRequest request = newMavenRequest(null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
repos.forEach(request::addRemoteRepository);
DefaultMaven maven = (DefaultMaven) getMavenComponent(Maven.class);
RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request);
org.eclipse.aether.impl.ArtifactResolver artifactResolver = getMavenComponent(org.eclipse.aether.impl.ArtifactResolver.class);
InternalLoggerFactory loggerFactory = new InternalLoggerFactory();
if (artifactResolver instanceof DefaultArtifactResolver) {
((DefaultArtifactResolver) artifactResolver).setLoggerFactory(loggerFactory);
}
org.eclipse.aether.RepositorySystem repositorySystem = getMavenComponent(org.eclipse.aether.RepositorySystem.class);
if (repositorySystem instanceof DefaultRepositorySystem) {
((DefaultRepositorySystem) repositorySystem).setLoggerFactory(loggerFactory);
}
List<RemoteRepository> remoteRepositories = RepositoryUtils.toRepos(request.getRemoteRepositories());
remoteRepositories = repositorySystem.newResolutionRepositories(repositorySystemSession, remoteRepositories);
try {
ArtifactResult artifactResult = repositorySystem.resolveArtifact(repositorySystemSession, new ArtifactRequest(RepositoryUtils.toArtifact(artifact), remoteRepositories, null));
return MavenModelUtil.convertArtifact(RepositoryUtils.toArtifact(artifactResult.getArtifact()), localRepository);
} catch (ArtifactResolutionException e) {
MavenServerContext.getLogger().info(e);
}
return MavenModelUtil.convertArtifact(artifact, localRepository);
}
Aggregations