Search in sources :

Example 26 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest 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 27 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest in project che by eclipse.

the class EffectivePomWriter method getEffectivePom.

public static String getEffectivePom(MavenServerImpl server, final File pom, List<String> activeProfiles, List<String> inactiveProfiles) {
    StringWriter stringWriter = new StringWriter();
    try {
        MavenExecutionRequest request = server.newMavenRequest(pom, activeProfiles, inactiveProfiles, Collections.emptyList());
        server.runMavenRequest(request, () -> {
            try {
                ProjectBuilder builder = server.getMavenComponent(ProjectBuilder.class);
                ProjectBuildingResult projectBuildingResult = builder.build(new File(pom.getPath()), request.getProjectBuildingRequest());
                MavenProject project = projectBuildingResult.getProject();
                XMLWriter writer = new PrettyPrintXMLWriter(stringWriter, "    ");
                writeHeader(writer);
                writeEffectivePom(project, writer);
            } catch (ProjectBuildingException | MojoExecutionException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        });
    } catch (Exception e) {
        return null;
    }
    return stringWriter.toString();
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) JDOMException(org.jdom.JDOMException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StringWriter(java.io.StringWriter) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) File(java.io.File)

Example 28 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest 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)

Example 29 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest in project intellij-community by JetBrains.

the class Maven3ServerIndexerImpl method updateIndex.

@Override
public void updateIndex(int id, MavenServerSettings settings, final MavenServerProgressIndicator indicator) throws RemoteException, MavenServerIndexerException, MavenServerProcessCanceledException {
    final IndexingContext index = getIndex(id);
    try {
        File repository = index.getRepository();
        if (repository != null) {
            // is local repository
            if (repository.exists()) {
                indicator.setIndeterminate(true);
                try {
                    myIndexer.scan(index, new MyScanningListener(indicator), false);
                } finally {
                    indicator.setIndeterminate(false);
                }
            }
        } else {
            final Maven3ServerEmbedder embedder = createEmbedder(settings);
            MavenExecutionRequest r = embedder.createRequest(null, Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList());
            final IndexUpdateRequest request = new IndexUpdateRequest(index);
            try {
                embedder.executeWithMavenSession(r, new Runnable() {

                    @Override
                    public void run() {
                        request.setResourceFetcher(new Maven3ServerIndexFetcher(index.getRepositoryId(), index.getRepositoryUrl(), embedder.getComponent(WagonManager.class), embedder.getComponent(RepositorySystem.class), new WagonTransferListenerAdapter(indicator) {

                            @Override
                            protected void downloadProgress(long downloaded, long total) {
                                super.downloadProgress(downloaded, total);
                                try {
                                    myIndicator.setFraction(((double) downloaded) / total);
                                } catch (RemoteException e) {
                                    throw new RuntimeRemoteException(e);
                                }
                            }

                            @Override
                            public void transferCompleted(TransferEvent event) {
                                super.transferCompleted(event);
                                try {
                                    myIndicator.setText2("Processing indices...");
                                } catch (RemoteException e) {
                                    throw new RuntimeRemoteException(e);
                                }
                            }
                        }));
                        try {
                            myUpdater.fetchAndUpdateIndex(request);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            } finally {
                embedder.release();
            }
        }
    } catch (RuntimeRemoteException e) {
        throw e.getCause();
    } catch (ProcessCanceledException e) {
        throw new MavenServerProcessCanceledException();
    } catch (Exception e) {
        throw new MavenServerIndexerException(wrapException(e));
    }
}
Also used : MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) IndexUpdateRequest(org.sonatype.nexus.index.updater.IndexUpdateRequest) TransferEvent(org.apache.maven.wagon.events.TransferEvent) IOException(java.io.IOException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ArchetypeDataSourceException(org.apache.maven.archetype.source.ArchetypeDataSourceException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) RepositorySystem(org.apache.maven.repository.RepositorySystem) WagonManager(org.apache.maven.artifact.manager.WagonManager) IndexingContext(org.sonatype.nexus.index.context.IndexingContext) RemoteException(java.rmi.RemoteException) File(java.io.File) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 30 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest in project intellij-community by JetBrains.

the class MavenEffectivePomDumper method evaluateEffectivePom.

// See org.apache.maven.plugins.help.EffectivePomMojo#execute from maven-help-plugin
@Nullable
public static String evaluateEffectivePom(final Maven3ServerEmbedder embedder, @NotNull final File file, @NotNull List<String> activeProfiles, @NotNull List<String> inactiveProfiles) throws RemoteException, MavenServerProcessCanceledException {
    final StringWriter w = new StringWriter();
    try {
        final MavenExecutionRequest request = embedder.createRequest(file, activeProfiles, inactiveProfiles, Collections.<String>emptyList());
        embedder.executeWithMavenSession(request, new Runnable() {

            @Override
            public void run() {
                try {
                    // copied from DefaultMavenProjectBuilder.buildWithDependencies
                    ProjectBuilder builder = embedder.getComponent(ProjectBuilder.class);
                    ProjectBuildingResult buildingResult = builder.build(new File(file.getPath()), request.getProjectBuildingRequest());
                    MavenProject project = buildingResult.getProject();
                    XMLWriter writer = new PrettyPrintXMLWriter(new PrintWriter(w), StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), "\n", null, null);
                    writeHeader(writer);
                    writeEffectivePom(project, writer);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    } catch (Exception e) {
        return null;
    }
    return w.toString();
}
Also used : MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RemoteException(java.rmi.RemoteException) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)56 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)40 File (java.io.File)24 MavenSession (org.apache.maven.execution.MavenSession)20 MavenExecutionResult (org.apache.maven.execution.MavenExecutionResult)14 MavenProject (org.apache.maven.project.MavenProject)14 DefaultMavenExecutionResult (org.apache.maven.execution.DefaultMavenExecutionResult)10 IOException (java.io.IOException)9 MavenExecutionRequestPopulationException (org.apache.maven.execution.MavenExecutionRequestPopulationException)9 MavenExecutionRequestPopulator (org.apache.maven.execution.MavenExecutionRequestPopulator)9 DefaultPlexusContainer (org.codehaus.plexus.DefaultPlexusContainer)8 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)7 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)7 FileNotFoundException (java.io.FileNotFoundException)6 Path (java.nio.file.Path)6 Properties (java.util.Properties)6 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)6 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)6 LifecycleExecutionException (org.apache.maven.lifecycle.LifecycleExecutionException)5 ProjectBuilder (org.apache.maven.project.ProjectBuilder)5