Search in sources :

Example 1 with RepositorySystemSession

use of org.sonatype.aether.RepositorySystemSession in project intellij-community by JetBrains.

the class Maven30ServerEmbedderImpl method resolvePlugin.

@Override
public Collection<MavenArtifact> resolvePlugin(@NotNull final MavenPlugin plugin, @NotNull final List<MavenRemoteRepository> repositories, int nativeMavenProjectId, final boolean transitive) throws RemoteException, MavenServerProcessCanceledException {
    try {
        Plugin mavenPlugin = new Plugin();
        mavenPlugin.setGroupId(plugin.getGroupId());
        mavenPlugin.setArtifactId(plugin.getArtifactId());
        mavenPlugin.setVersion(plugin.getVersion());
        MavenProject project = RemoteNativeMavenProjectHolder.findProjectById(nativeMavenProjectId);
        Plugin pluginFromProject = project.getBuild().getPluginsAsMap().get(plugin.getGroupId() + ':' + plugin.getArtifactId());
        if (pluginFromProject != null) {
            mavenPlugin.setDependencies(pluginFromProject.getDependencies());
        }
        final MavenExecutionRequest request = createRequest(null, Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList());
        DefaultMaven maven = (DefaultMaven) getComponent(Maven.class);
        RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request);
        PluginDependenciesResolver pluginDependenciesResolver = getComponent(PluginDependenciesResolver.class);
        org.sonatype.aether.artifact.Artifact pluginArtifact = pluginDependenciesResolver.resolve(mavenPlugin, project.getRemotePluginRepositories(), repositorySystemSession);
        org.sonatype.aether.graph.DependencyNode node = pluginDependenciesResolver.resolve(mavenPlugin, pluginArtifact, null, project.getRemotePluginRepositories(), repositorySystemSession);
        PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
        node.accept(nlg);
        List<MavenArtifact> res = new ArrayList<MavenArtifact>();
        for (org.sonatype.aether.artifact.Artifact artifact : nlg.getArtifacts(true)) {
            if (!Comparing.equal(artifact.getArtifactId(), plugin.getArtifactId()) || !Comparing.equal(artifact.getGroupId(), plugin.getGroupId())) {
                res.add(MavenModelConverter.convertArtifact(RepositoryUtils.toArtifact(artifact), getLocalRepositoryFile()));
            }
        }
        return res;
    } catch (Exception e) {
        Maven3ServerGlobals.getLogger().info(e);
        return Collections.emptyList();
    }
}
Also used : DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession) RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) PreorderNodeListGenerator(org.sonatype.aether.util.graph.PreorderNodeListGenerator) 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) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ContextException(org.codehaus.plexus.context.ContextException) InvalidRepositoryException(org.apache.maven.artifact.InvalidRepositoryException) PluginDependenciesResolver(org.apache.maven.plugin.internal.PluginDependenciesResolver) Plugin(org.apache.maven.model.Plugin)

Example 2 with RepositorySystemSession

use of org.sonatype.aether.RepositorySystemSession in project intellij-community by JetBrains.

the class Maven30ServerEmbedderImpl method executeWithMavenSession.

public void executeWithMavenSession(MavenExecutionRequest request, Runnable runnable) {
    DefaultMaven maven = (DefaultMaven) getComponent(Maven.class);
    RepositorySystemSession repositorySession = maven.newRepositorySession(request);
    request.getProjectBuildingRequest().setRepositorySession(repositorySession);
    MavenSession mavenSession = new MavenSession(myContainer, repositorySession, request, new DefaultMavenExecutionResult());
    LegacySupport legacySupport = getComponent(LegacySupport.class);
    MavenSession oldSession = legacySupport.getSession();
    legacySupport.setSession(mavenSession);
    /** adapted from {@link DefaultMaven#doExecute(MavenExecutionRequest)} */
    try {
        for (AbstractMavenLifecycleParticipant listener : getLifecycleParticipants(Collections.<MavenProject>emptyList())) {
            listener.afterSessionStart(mavenSession);
        }
    } catch (MavenExecutionException e) {
        throw new RuntimeException(e);
    }
    try {
        runnable.run();
    } finally {
        legacySupport.setSession(oldSession);
    }
}
Also used : DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession) RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) LegacySupport(org.apache.maven.plugin.LegacySupport)

Example 3 with RepositorySystemSession

use of org.sonatype.aether.RepositorySystemSession in project intellij-community by JetBrains.

the class CustomMaven30ArtifactResolver method resolve.

// ------------------------------------------------------------------------
//
// ------------------------------------------------------------------------
public ArtifactResolutionResult resolve(ArtifactResolutionRequest request) {
    Artifact rootArtifact = request.getArtifact();
    Set<Artifact> artifacts = request.getArtifactDependencies();
    Map 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;
}
Also used : DefaultMetadataResolutionRequest(org.apache.maven.repository.legacy.metadata.DefaultMetadataResolutionRequest) MetadataResolutionRequest(org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest) ArtifactMetadataRetrievalException(org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) DefaultMetadataResolutionRequest(org.apache.maven.repository.legacy.metadata.DefaultMetadataResolutionRequest) RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) ResolutionGroup(org.apache.maven.artifact.metadata.ResolutionGroup) Artifact(org.apache.maven.artifact.Artifact) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) MavenWorkspaceMap(org.jetbrains.idea.maven.model.MavenWorkspaceMap) ArtifactMetadataSource(org.apache.maven.artifact.metadata.ArtifactMetadataSource)

Example 4 with RepositorySystemSession

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

the class MavenProjectsCreator method createNow.

private Set<MavenProject> createNow(Settings settings, File pomFile) throws PlexusContainerException, PlexusConfigurationException, ComponentLookupException, MavenExecutionRequestPopulationException, ProjectBuildingException {
    ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration().setClassWorld(new ClassWorld("plexus.core", ClassWorld.class.getClassLoader())).setName("mavenCore");
    DefaultPlexusContainer container = new DefaultPlexusContainer(containerConfiguration);
    ProjectBuilder builder = container.lookup(ProjectBuilder.class);
    MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
    final Properties properties = new Properties();
    properties.putAll(SystemProperties.getInstance().asMap());
    executionRequest.setSystemProperties(properties);
    MavenExecutionRequestPopulator populator = container.lookup(MavenExecutionRequestPopulator.class);
    populator.populateFromSettings(executionRequest, settings);
    populator.populateDefaults(executionRequest);
    ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest();
    buildingRequest.setProcessPlugins(false);
    MavenProject mavenProject = builder.build(pomFile, buildingRequest).getProject();
    Set<MavenProject> reactorProjects = new LinkedHashSet<MavenProject>();
    // 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);
    CollectionUtils.collect(allProjects, reactorProjects, new Transformer<MavenProject, ProjectBuildingResult>() {

        public MavenProject transform(ProjectBuildingResult original) {
            return original.getProject();
        }
    });
    MavenExecutionResult result = new DefaultMavenExecutionResult();
    result.setProject(mavenProject);
    RepositorySystemSession repoSession = new DefaultRepositorySystemSession();
    MavenSession session = new MavenSession(container, repoSession, executionRequest, result);
    session.setCurrentProject(mavenProject);
    return reactorProjects;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) Properties(java.util.Properties) SystemProperties(org.gradle.internal.SystemProperties) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession)

Example 5 with RepositorySystemSession

use of org.sonatype.aether.RepositorySystemSession in project archiva by apache.

the class Maven3DependencyTreeBuilder method resolve.

private void resolve(ResolveRequest resolveRequest) {
    RepositorySystem system = newRepositorySystem();
    RepositorySystemSession session = newRepositorySystemSession(system, resolveRequest.localRepoDir);
    org.sonatype.aether.artifact.Artifact artifact = new DefaultArtifact(resolveRequest.groupId + ":" + resolveRequest.artifactId + ":" + resolveRequest.version);
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, ""));
    // add remote repositories
    for (RemoteRepository remoteRepository : resolveRequest.remoteRepositories) {
        collectRequest.addRepository(new org.sonatype.aether.repository.RemoteRepository(remoteRepository.getId(), "default", remoteRepository.getUrl()));
    }
    collectRequest.setRequestContext("project");
    try {
        CollectResult collectResult = system.collectDependencies(session, collectRequest);
        collectResult.getRoot().accept(resolveRequest.dependencyVisitor);
        log.debug("test");
    } catch (DependencyCollectionException e) {
        log.error(e.getMessage(), e);
    }
}
Also used : MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) DependencyCollectionException(org.sonatype.aether.collection.DependencyCollectionException) CollectResult(org.sonatype.aether.collection.CollectResult) RemoteRepository(org.apache.archiva.admin.model.beans.RemoteRepository) Dependency(org.sonatype.aether.graph.Dependency) CollectRequest(org.sonatype.aether.collection.CollectRequest) RepositorySystem(org.sonatype.aether.RepositorySystem) DefaultArtifact(org.sonatype.aether.util.artifact.DefaultArtifact)

Aggregations

RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)26 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)15 Artifact (org.sonatype.aether.artifact.Artifact)13 DefaultArtifact (org.sonatype.aether.util.artifact.DefaultArtifact)11 RepositorySystem (org.sonatype.aether.RepositorySystem)10 Test (org.junit.Test)8 Dependency (org.sonatype.aether.graph.Dependency)8 TestRepositorySystemSession (org.sonatype.aether.test.impl.TestRepositorySystemSession)7 CollectRequest (org.sonatype.aether.collection.CollectRequest)6 ArtifactRequest (org.sonatype.aether.resolution.ArtifactRequest)6 ArtifactResult (org.sonatype.aether.resolution.ArtifactResult)6 File (java.io.File)5 LocalArtifactRequest (org.sonatype.aether.repository.LocalArtifactRequest)5 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)5 MavenRepositorySystemSession (org.apache.maven.repository.internal.MavenRepositorySystemSession)4 CollectResult (org.sonatype.aether.collection.CollectResult)4 VersionResolver (org.sonatype.aether.impl.VersionResolver)4 LocalArtifactResult (org.sonatype.aether.repository.LocalArtifactResult)4 LocalMetadataRegistration (org.sonatype.aether.repository.LocalMetadataRegistration)4 VersionRequest (org.sonatype.aether.resolution.VersionRequest)4