Search in sources :

Example 31 with RepositorySystemSession

use of org.eclipse.aether.RepositorySystemSession in project kie-wb-common by kiegroup.

the class MavenArtifactResolverTest method resolveArtifactNotOffline.

@Test
public void resolveArtifactNotOffline() throws Exception {
    final boolean[] executedOffline = { false };
    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(false, 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]).isFalse();
}
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 32 with RepositorySystemSession

use of org.eclipse.aether.RepositorySystemSession in project gradle by gradle.

the class MavenProjectsCreator method createNow.

private Set<MavenProject> createNow(Settings settings, File pomFile) throws PlexusContainerException, ComponentLookupException, MavenExecutionRequestPopulationException, ProjectBuildingException {
    ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration().setClassWorld(new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader())).setName("mavenCore").setClassPathScanning(PlexusConstants.SCANNING_INDEX).setAutoWiring(true);
    DefaultPlexusContainer container = new DefaultPlexusContainer(containerConfiguration);
    ProjectBuilder builder = container.lookup(ProjectBuilder.class);
    MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
    final Properties properties = SystemProperties.getInstance().withSystemProperties(() -> {
        final Properties currentProperties = new Properties();
        currentProperties.putAll(System.getProperties());
        return currentProperties;
    });
    executionRequest.setSystemProperties(properties);
    MavenExecutionRequestPopulator populator = container.lookup(MavenExecutionRequestPopulator.class);
    populateFromSettings(settings, executionRequest, populator);
    populator.populateDefaults(executionRequest);
    ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest();
    buildingRequest.getRemoteRepositories().forEach(repository -> {
        if (repository.getId().equals(RepositorySystem.DEFAULT_REMOTE_REPO_ID)) {
            repository.setUrl(RepositoryHandler.MAVEN_CENTRAL_URL);
        }
    });
    buildingRequest.setProcessPlugins(false);
    MavenProject mavenProject = builder.build(pomFile, buildingRequest).getProject();
    Set<MavenProject> reactorProjects = new LinkedHashSet<>();
    // TODO adding the parent project first because the converter needs it this way ATM. This is oversimplified.
    // the converter should not depend on the order of reactor projects.
    // we should add coverage for nested multi-project builds with multiple parents.
    reactorProjects.add(mavenProject);
    List<ProjectBuildingResult> allProjects = builder.build(ImmutableList.of(pomFile), true, buildingRequest);
    // noinspection NullableProblems
    CollectionUtils.collect(allProjects, reactorProjects, ProjectBuildingResult::getProject);
    MavenExecutionResult result = new DefaultMavenExecutionResult();
    result.setProject(mavenProject);
    RepositorySystemSession repoSession = new DefaultRepositorySystemSession();
    @SuppressWarnings("deprecation") MavenSession session = new MavenSession(container, repoSession, executionRequest, result);
    session.setCurrentProject(mavenProject);
    return reactorProjects;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) Properties(java.util.Properties) SystemProperties(org.gradle.internal.SystemProperties) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenSession(org.apache.maven.execution.MavenSession) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) MavenExecutionRequestPopulator(org.apache.maven.execution.MavenExecutionRequestPopulator)

Example 33 with RepositorySystemSession

use of org.eclipse.aether.RepositorySystemSession in project wildfly-swarm by wildfly-swarm.

the class MavenArtifactResolvingHelper method resolveAll.

@Override
public Set<ArtifactSpec> resolveAll(Collection<ArtifactSpec> specs, boolean transitive, boolean defaultExcludes) throws Exception {
    if (specs.isEmpty()) {
        return Collections.emptySet();
    }
    List<DependencyNode> nodes;
    if (transitive) {
        final CollectRequest request = new CollectRequest();
        request.setRepositories(this.remoteRepositories);
        // SWARM-1031
        if (this.dependencyManagement != null) {
            List<Dependency> managedDependencies = this.dependencyManagement.getDependencies().stream().map(mavenDep -> {
                DefaultArtifact artifact = new DefaultArtifact(mavenDep.getGroupId(), mavenDep.getArtifactId(), mavenDep.getType(), mavenDep.getVersion());
                return new Dependency(artifact, mavenDep.getScope());
            }).collect(Collectors.toList());
            request.setManagedDependencies(managedDependencies);
        }
        specs.forEach(spec -> request.addDependency(new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), spec.type(), spec.version()), "compile")));
        RepositorySystemSession tempSession = new RepositorySystemSessionWrapper(this.session, new ConflictResolver(new NearestVersionSelector(), new JavaScopeSelector(), new SimpleOptionalitySelector(), new JavaScopeDeriver()), defaultExcludes);
        CollectResult result = this.system.collectDependencies(tempSession, request);
        PreorderNodeListGenerator gen = new PreorderNodeListGenerator();
        result.getRoot().accept(gen);
        nodes = gen.getNodes();
    } else {
        nodes = new ArrayList<>();
        for (ArtifactSpec spec : specs) {
            Dependency dependency = new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), spec.type(), spec.version()), "compile");
            DefaultDependencyNode node = new DefaultDependencyNode(dependency);
            nodes.add(node);
        }
    }
    List<DependencyNode> extraDependencies = ExtraArtifactsHandler.getExtraDependencies(nodes);
    nodes.addAll(extraDependencies);
    resolveDependenciesInParallel(nodes);
    return nodes.stream().filter(node -> !"system".equals(node.getDependency().getScope())).map(node -> {
        final Artifact artifact = node.getArtifact();
        return new ArtifactSpec(node.getDependency().getScope(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getExtension(), artifact.getClassifier(), null);
    }).map(this::resolve).filter(Objects::nonNull).collect(Collectors.toSet());
}
Also used : LocalArtifactRequest(org.eclipse.aether.repository.LocalArtifactRequest) Proxy(org.eclipse.aether.repository.Proxy) Dependency(org.eclipse.aether.graph.Dependency) DependencyManagement(org.apache.maven.model.DependencyManagement) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArtifactRepositoryPolicy(org.apache.maven.artifact.repository.ArtifactRepositoryPolicy) ArrayList(java.util.ArrayList) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) CollectRequest(org.eclipse.aether.collection.CollectRequest) CollectResult(org.eclipse.aether.collection.CollectResult) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) RepositoryPolicy(org.eclipse.aether.repository.RepositoryPolicy) Collection(java.util.Collection) Authentication(org.apache.maven.artifact.repository.Authentication) Artifact(org.eclipse.aether.artifact.Artifact) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) Set(java.util.Set) LocalArtifactResult(org.eclipse.aether.repository.LocalArtifactResult) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) Collectors(java.util.stream.Collectors) ArtifactResolvingHelper(org.wildfly.swarm.tools.ArtifactResolvingHelper) Objects(java.util.Objects) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) AuthenticationBuilder(org.eclipse.aether.util.repository.AuthenticationBuilder) List(java.util.List) ArtifactResolver(org.eclipse.aether.impl.ArtifactResolver) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) Collections(java.util.Collections) RepositorySystem(org.eclipse.aether.RepositorySystem) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) CollectResult(org.eclipse.aether.collection.CollectResult) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 34 with RepositorySystemSession

use of org.eclipse.aether.RepositorySystemSession in project gate-core by GateNLP.

the class SimpleMavenCache method cacheArtifact.

public void cacheArtifact(Artifact artifact) throws IOException, SettingsBuildingException, DependencyCollectionException, DependencyResolutionException, ArtifactResolutionException, ModelBuildingException {
    List<RemoteRepository> repos = getRepositoryList();
    Dependency dependency = new Dependency(artifact, "runtime");
    RepositorySystem repoSystem = getRepositorySystem();
    RepositorySystemSession repoSession = getRepositorySession(repoSystem, null);
    CollectRequest collectRequest = new CollectRequest(dependency, repos);
    DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot();
    DependencyRequest dependencyRequest = new DependencyRequest();
    dependencyRequest.setRoot(node);
    DependencyResult result = repoSystem.resolveDependencies(repoSession, dependencyRequest);
    for (ArtifactResult ar : result.getArtifactResults()) {
        // generate output file name from the *requested* artifact rather
        // than the resolved one (e.g. if we requested a -SNAPSHOT version
        // but got a timestamped one)
        File file = getArtifactFile(ar.getRequest().getArtifact());
        FileUtils.copyFile(ar.getArtifact().getFile(), file);
        // get the POM corresponding to the specific resolved JAR
        Artifact pomArtifact = new SubArtifact(ar.getArtifact(), "", "pom");
        ArtifactRequest artifactRequest = new ArtifactRequest(pomArtifact, repos, null);
        ArtifactResult artifactResult = repoSystem.resolveArtifact(repoSession, artifactRequest);
        // but copy it to a file named for the original requested version number
        file = getArtifactFile(new SubArtifact(ar.getRequest().getArtifact(), "", "pom"));
        FileUtils.copyFile(artifactResult.getArtifact().getFile(), file);
        cacheParents(artifactResult.getArtifact().getFile(), repoSystem, repoSession, repos);
    }
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DependencyResult(org.eclipse.aether.resolution.DependencyResult) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) Utils.getRepositorySystem(gate.util.maven.Utils.getRepositorySystem) RepositorySystem(org.eclipse.aether.RepositorySystem) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) File(java.io.File)

Example 35 with RepositorySystemSession

use of org.eclipse.aether.RepositorySystemSession in project sts4 by spring-projects.

the class MavenBridge method createSession.

@SuppressWarnings("deprecation")
public MavenSession createSession(MavenExecutionRequest request, MavenProject project) throws MavenException {
    RepositorySystemSession repoSession = createRepositorySession(request);
    MavenExecutionResult result = new DefaultMavenExecutionResult();
    MavenSession mavenSession = new MavenSession(plexus, repoSession, request, result);
    if (project != null) {
        mavenSession.setProjects(Collections.singletonList(project));
    }
    return mavenSession;
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) MavenSession(org.apache.maven.execution.MavenSession) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult)

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