Search in sources :

Example 16 with RepositorySystemSession

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();
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) File(java.io.File) URI(java.net.URI) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) Test(org.junit.Test)

Example 17 with RepositorySystemSession

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);
    }
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) GAV(org.guvnor.common.services.project.model.GAV) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) IOException(java.io.IOException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 18 with RepositorySystemSession

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);
    }
}
Also used : DefaultMaven(org.apache.maven.DefaultMaven) Maven(org.apache.maven.Maven) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) MavenSession(org.apache.maven.execution.MavenSession) MavenExecutionException(org.apache.maven.MavenExecutionException) LegacySupport(org.apache.maven.plugin.LegacySupport) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) AbstractMavenLifecycleParticipant(org.apache.maven.AbstractMavenLifecycleParticipant) DefaultMaven(org.apache.maven.DefaultMaven)

Example 19 with RepositorySystemSession

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();
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) LegacySupport(org.apache.maven.plugin.LegacySupport) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) InitializationException(org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException) ModelInterpolationException(org.apache.maven.project.interpolation.ModelInterpolationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) InvalidProjectModelException(org.apache.maven.project.InvalidProjectModelException) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) MavenExecutionException(org.apache.maven.MavenExecutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) InvalidRepositoryException(org.apache.maven.artifact.InvalidRepositoryException) MavenExecutionRequestPopulationException(org.apache.maven.execution.MavenExecutionRequestPopulationException) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) CheArtifactResolver(org.eclipse.che.maven.CheArtifactResolver) DefaultArtifactResolver(org.eclipse.aether.internal.impl.DefaultArtifactResolver) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) ArtifactResolutionRequest(org.apache.maven.artifact.resolver.ArtifactResolutionRequest) ArtifactResolutionResult(org.apache.maven.artifact.resolver.ArtifactResolutionResult)

Example 20 with RepositorySystemSession

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);
}
Also used : DefaultMaven(org.apache.maven.DefaultMaven) Maven(org.apache.maven.Maven) InvalidRepositoryException(org.apache.maven.artifact.InvalidRepositoryException) DefaultRepositorySystem(org.eclipse.aether.internal.impl.DefaultRepositorySystem) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ArrayList(java.util.ArrayList) MavenRemoteRepository(org.eclipse.che.maven.data.MavenRemoteRepository) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultArtifactResolver(org.eclipse.aether.internal.impl.DefaultArtifactResolver) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ArtifactRepositoryFactory(org.apache.maven.artifact.repository.ArtifactRepositoryFactory) DefaultMaven(org.apache.maven.DefaultMaven) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifact(org.eclipse.che.maven.data.MavenArtifact) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) MavenRemoteRepository(org.eclipse.che.maven.data.MavenRemoteRepository)

Aggregations

RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)38 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)19 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)14 RepositorySystem (org.eclipse.aether.RepositorySystem)13 Artifact (org.eclipse.aether.artifact.Artifact)13 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)13 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)12 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)11 ArrayList (java.util.ArrayList)10 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)10 File (java.io.File)8 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)7 List (java.util.List)5 Artifact (org.apache.maven.artifact.Artifact)5 MavenSession (org.apache.maven.execution.MavenSession)5 IOException (java.io.IOException)4 DefaultMaven (org.apache.maven.DefaultMaven)4 Maven (org.apache.maven.Maven)4 InvalidRepositoryException (org.apache.maven.artifact.InvalidRepositoryException)4 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)4