Example usage for org.apache.maven.plugin LegacySupport setSession

List of usage examples for org.apache.maven.plugin LegacySupport setSession

Introduction

In this page you can find the example usage for org.apache.maven.plugin LegacySupport setSession.

Prototype

void setSession(MavenSession session);

Source Link

Document

Sets the currently active session.

Usage

From source file:org.apache.sling.ide.eclipse.m2e.EmbeddedArchetypeInstaller.java

License:Apache License

public void installArchetype() throws CoreException {
    try {/*from  www .ja  v  a 2 s .c  o m*/
        IMaven maven = MavenPlugin.getMaven();
        // first get the plexus container
        PlexusContainer container = ((MavenImpl) MavenPlugin.getMaven()).getPlexusContainer();

        // then get the DefaultMaven
        DefaultMaven mvn = (DefaultMaven) container.lookup(Maven.class);

        // now create a RepositorySystemSession
        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
        request.setLocalRepository(maven.getLocalRepository());

        // We need to support Maven 3.0.x as well, so we use reflection to
        // access Aether APIs in a manner which is compatible with all Maven 3.x versions
        // See https://maven.apache.org/docs/3.1.0/release-notes.html
        MavenSession session = reflectiveCreateMavenSession(container, mvn, request);
        LegacySupport legacy = container.lookup(LegacySupport.class);
        legacy.setSession(session);

        // then lookup the DefaultArtifactInstaller
        DefaultArtifactInstaller dai = (DefaultArtifactInstaller) container.lookup(ArtifactInstaller.class);

        final Set<Entry<String, InputStream>> entries = origins.entrySet();
        for (Iterator<Entry<String, InputStream>> it = entries.iterator(); it.hasNext();) {
            final Entry<String, InputStream> entry = it.next();
            final String fileExtension = entry.getKey();
            File tmpFile = File.createTempFile("slingClipseTmp", fileExtension);

            try (InputStream in = entry.getValue(); FileOutputStream fos = new FileOutputStream(tmpFile)) {
                IOUtils.copy(in, fos);
                // the below code uses the fileExtension as a type. Most of the time this is correct
                // and should be fine for our usage
                Artifact jarArtifact = new DefaultArtifact(groupId, artifactId, version, "", fileExtension, "",
                        new DefaultArtifactHandler(fileExtension));
                dai.install(tmpFile, jarArtifact, maven.getLocalRepository());
            } finally {
                FileUtils.deleteQuietly(tmpFile);
            }
        }

        Archetype archetype = new Archetype();
        archetype.setGroupId(groupId);
        archetype.setArtifactId(artifactId);
        archetype.setVersion(version);
        org.apache.maven.archetype.Archetype archetyper = MavenPluginActivator.getDefault().getArchetype();
        archetyper.updateLocalCatalog(archetype);
    } catch (CoreException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
    }
}

From source file:org.echocat.jomon.maven.boot.ArtifactFactory.java

License:Mozilla Public License

private MavenExecutionRequest injectDefaults(MavenExecutionRequest request,
        AtomicReference<PlexusContainer> containerReference, AtomicReference<DefaultMaven> oldMaven,
        DefaultMavenExecutionResult executionResult)
        throws ComponentLookupException, MavenExecutionRequestPopulationException {
    final MavenExecutionRequestPopulator populator = containerReference.get()
            .lookup(MavenExecutionRequestPopulator.class);
    final MavenExecutionRequest resultRequest = populator.populateDefaults(request);

    final RepositorySystemSession repositorySystemSession = oldMaven.get().newRepositorySession(resultRequest);
    resultRequest.getProjectBuildingRequest().setRepositorySession(repositorySystemSession);

    final MavenSession session = new MavenSession(containerReference.get(), repositorySystemSession, request,
            executionResult);//from  w  w  w  .j  a va2 s .  c  om
    final LegacySupport legacySupport = containerReference.get().lookup(LegacySupport.class);
    legacySupport.setSession(session);
    return resultRequest;
}

From source file:org.echocat.jomon.maven.MavenEnvironmentFactory.java

License:Mozilla Public License

@Nonnull
protected void injectDefaults(@Nonnull MavenExecutionRequest request,
        @Nonnull AtomicReference<PlexusContainer> containerReference,
        @Nonnull RepositorySystemSession repositorySystemSession,
        @Nonnull DefaultMavenExecutionResult executionResult) throws Exception {
    final MavenSession session = new MavenSession(containerReference.get(), repositorySystemSession, request,
            executionResult);//from  w  w w.j a v  a2s .co  m
    final LegacySupport legacySupport = containerReference.get().lookup(LegacySupport.class);
    legacySupport.setSession(session);
}

From source file:org.eclipse.che.maven.server.MavenServerImpl.java

License:Open Source License

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 {/* w w w  .j  a  v  a2s. c o  m*/
        for (AbstractMavenLifecycleParticipant participant : getLifecycleParticipants(
                Collections.emptyList())) {
            participant.afterSessionStart(mavenSession);
        }
        runnable.run();
    } catch (MavenExecutionException e) {
        throw new RuntimeException(e);
    } finally {
        legacySupport.setSession(previousSession);
    }
}

From source file:org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.java

License:Open Source License

public <V> V execute(MavenProject project, ICallable<V> callable, IProgressMonitor monitor)
        throws CoreException {
    Deque<MavenExecutionContext> stack = context.get();
    if (stack == null) {
        stack = new ArrayDeque<MavenExecutionContext>();
        context.set(stack);/*  w  ww  .  java2s  .c om*/
    }
    final MavenExecutionContext parent = stack.peek();

    if (this == parent) {
        // shortcut the setup logic, this is nested invocation of the same context
        return executeBare(project, callable, monitor);
    }

    // remember original configuration to "pop" the session stack properly
    final ArtifactRepository origlocalRepository = localRepository;
    final FilterRepositorySystemSession origRepositorySession = repositorySession;
    final MavenSession origMavenSession = mavenSession;
    final MavenExecutionRequest origRequest = request;

    if (request == null && parent != null) {
        this.request = parent.request;
        this.localRepository = parent.localRepository;
        this.repositorySession = parent.repositorySession;
        this.mavenSession = parent.mavenSession;
    } else {
        if (request == null) {
            request = newExecutionRequest();
        }
        maven.populateDefaults(request);
        populateSystemProperties(request);
        this.localRepository = request.getLocalRepository();
        this.repositorySession = maven.createRepositorySession(request);
        if (parent != null) {
            this.repositorySession.setData(parent.repositorySession.getData());
        }
        final MavenExecutionResult result = new DefaultMavenExecutionResult();
        this.mavenSession = new MavenSession(maven.getPlexusContainer(), repositorySession, request, result);
    }

    final LegacySupport legacySupport = maven.lookup(LegacySupport.class);
    final MavenSession origLegacySession = legacySupport.getSession(); // TODO validate == origSession

    stack.push(this);
    legacySupport.setSession(mavenSession);
    try {
        return executeBare(project, callable, monitor);
    } finally {
        stack.pop();
        if (stack.isEmpty()) {
            context.set(null); // TODO decide if this is useful
        }
        legacySupport.setSession(origLegacySession);
        mavenSession = origMavenSession;
        repositorySession = origRepositorySession;
        localRepository = origlocalRepository;
        request = origRequest;
    }
}

From source file:org.eclipse.m2e.core.internal.MavenPluginActivator.java

License:Open Source License

/**
 * @deprecated use {@link IMavenExecutionContext} instead.
 *///  w w w .  j av a 2s  .c  om
public MavenSession setSession(MavenSession session) {
    LegacySupport legacy = lookup(LegacySupport.class);
    MavenSession old = legacy.getSession();
    legacy.setSession(session);
    return old;
}

From source file:org.eclipse.m2e.core.MavenPlugin.java

License:Open Source License

public MavenSession setSession(MavenSession session) {
    LegacySupport legacy = lookup(LegacySupport.class);
    MavenSession old = legacy.getSession();
    legacy.setSession(session);
    return old;// ww  w. j ava 2s .  c  om
}

From source file:org.jetbrains.idea.maven.server.Maven30ServerEmbedderImpl.java

License:Apache License

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 {//from  w w w  .  jav  a2 s.c  o m
        for (AbstractMavenLifecycleParticipant listener : getLifecycleParticipants(
                Collections.<MavenProject>emptyList())) {
            listener.afterSessionStart(mavenSession);
        }
    } catch (MavenExecutionException e) {
        throw new RuntimeException(e);
    }

    try {
        runnable.run();
    } finally {
        legacySupport.setSession(oldSession);
    }
}