Example usage for org.apache.maven Maven execute

List of usage examples for org.apache.maven Maven execute

Introduction

In this page you can find the example usage for org.apache.maven Maven execute.

Prototype

MavenExecutionResult execute(MavenExecutionRequest request);

Source Link

Usage

From source file:com.github.swt_release_fetcher.Artifact.java

License:Apache License

public void runMavenDeploy(File pomFile, File jarFile, File sourcesFile) {
    Properties properties = new Properties();
    properties.put("pomFile", pomFile.getAbsolutePath());
    properties.put("file", jarFile.getAbsolutePath());
    properties.put("sources", sourcesFile.getAbsolutePath());
    properties.put("repositoryId", "googlecode");
    properties.put("url", REPOSITORY_URL);

    MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    request.setPom(pomFile);/*from w  w w.j a  v a 2s  .com*/
    request.setGoals(Arrays.asList(new String[] { "deploy:deploy-file" }));
    request.setSystemProperties(properties);

    Maven maven = EmbeddedMaven.get();
    MavenExecutionResult result = maven.execute(request);

    if (result.hasExceptions()) {
        System.out.println("Maven deploy failed!");
        System.out.println(result.getExceptions());
        throw new RuntimeException("Maven deploy failed!", result.getExceptions().get(0));
    } else {
        System.out.println("Maven deploy succeeded!");
    }
}

From source file:hudson.maven.MavenEmbedder.java

License:Apache License

public MavenExecutionResult execute(MavenRequest mavenRequest) throws MavenEmbedderException {
    ClassLoader original = Thread.currentThread().getContextClassLoader();
    try {/*from  ww w.  j  a v a2  s . c  o  m*/
        Maven maven = lookup(Maven.class);
        Thread.currentThread().setContextClassLoader(this.plexusContainer.getContainerRealm());
        return maven.execute(buildMavenExecutionRequest(mavenRequest));
    } catch (MavenEmbedderException e) {
        throw new MavenEmbedderException(e.getMessage(), e);
    } catch (ComponentLookupException e) {
        throw new MavenEmbedderException(e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}

From source file:org.appformer.maven.integration.embedder.MavenEmbedder.java

License:Apache License

public MavenExecutionResult execute(final MavenRequest mavenRequest) throws MavenEmbedderException {
    final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
    try {/*from w ww  .j av  a  2 s  .  c  o  m*/
        Thread.currentThread().setContextClassLoader(componentProvider.getSystemClassLoader());
        final Maven maven = componentProvider.lookup(Maven.class);
        return maven.execute(buildMavenExecutionRequest(mavenRequest));
    } catch (final MavenEmbedderException e) {
        log.error("An MavenEmbedderException occurred during maven execution.", e);
        throw e;
    } catch (final Throwable e) {
        log.error("An exception occurred during maven execution.", e);
        throw new MavenEmbedderException(e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(originalCl);
    }
}

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

License:Apache License

private org.apache.maven.execution.MavenExecutionResult safeExecute(MavenExecutionRequest request, Maven maven)
        throws RemoteException {
    MavenLeakDetector detector = new MavenLeakDetector().mark();
    org.apache.maven.execution.MavenExecutionResult result = maven.execute(request);
    detector.check();//from  w  ww  .ja  v a 2s .c o  m
    return result;
}

From source file:org.jvnet.hudson.maven3.launcher.Maven31Launcher.java

License:Apache License

public static int main(String[] args) throws Exception {
    ClassLoader orig = Thread.currentThread().getContextClassLoader();
    try {//from   ww  w  .ja  va  2s  .c  o  m

        ClassRealm containerRealm = (ClassRealm) Thread.currentThread().getContextClassLoader();

        ContainerConfiguration cc = new DefaultContainerConfiguration().setName("maven")
                .setRealm(containerRealm).setClassPathScanning(PlexusConstants.SCANNING_INDEX)
                .setAutoWiring(true);

        DefaultPlexusContainer container = new DefaultPlexusContainer(cc);
        Slf4jLoggerManager mavenLoggerManager = new Slf4jLoggerManager();
        container.setLoggerManager(mavenLoggerManager);

        Maven maven = (Maven) container.lookup("org.apache.maven.Maven", "default");

        EventSpyDispatcher eventSpyDispatcher = container.lookup(EventSpyDispatcher.class);

        if (eventSpiesList != null && !eventSpiesList.isEmpty()) {
            List<EventSpy> eventSpies = eventSpyDispatcher.getEventSpies();
            if (eventSpies == null) {
                eventSpies = new ArrayList<EventSpy>(1);
            }
            eventSpies.addAll(eventSpiesList);

            // get event spies added with plexus components
            // see Maven31Maven addPlexusComponents
            // PlexusModuleContributor extension
            List<EventSpy> spies = container.lookupList(EventSpy.class);
            if (spies != null && !spies.isEmpty()) {
                eventSpies.addAll(spies);
            }

            eventSpyDispatcher.setEventSpies(eventSpies);
        }

        MavenExecutionRequest request = getMavenExecutionRequest(args, container);

        MavenExecutionResult result = maven.execute(request);
        hudsonMavenExecutionResult = new HudsonMavenExecutionResult(result);

        // we don't care about cli mavenExecutionResult will be study in the the plugin
        return 0;// cli.doMain( args, null );
    } catch (ComponentLookupException e) {
        throw new Exception(e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(orig);
    }
}

From source file:org.jvnet.hudson.maven3.launcher.Maven35Launcher.java

License:Apache License

public static int main(String[] args, ClassWorld classWorld) throws Exception {
    ClassLoader orig = Thread.currentThread().getContextClassLoader();
    try {// w w  w  .j  a v  a 2s  .c om

        ClassRealm containerRealm = (ClassRealm) Thread.currentThread().getContextClassLoader();

        ContainerConfiguration cc = new DefaultContainerConfiguration().setName("maven")
                .setRealm(containerRealm).setClassPathScanning(PlexusConstants.SCANNING_INDEX)
                .setAutoWiring(true);

        DefaultPlexusContainer container = new DefaultPlexusContainer(cc);
        Slf4jLoggerManager mavenLoggerManager = new Slf4jLoggerManager();
        container.setLoggerManager(mavenLoggerManager);

        Maven maven = (Maven) container.lookup("org.apache.maven.Maven", "default");

        EventSpyDispatcher eventSpyDispatcher = container.lookup(EventSpyDispatcher.class);

        if (eventSpiesList != null && !eventSpiesList.isEmpty()) {
            List<EventSpy> eventSpies = eventSpyDispatcher.getEventSpies();
            if (eventSpies == null) {
                eventSpies = new ArrayList<EventSpy>(1);
            }
            eventSpies.addAll(eventSpiesList);

            // get event spies added with plexus components
            // see Maven31Maven addPlexusComponents
            // PlexusModuleContributor extension
            List<EventSpy> spies = container.lookupList(EventSpy.class);
            if (spies != null && !spies.isEmpty()) {
                eventSpies.addAll(spies);
            }

            eventSpyDispatcher.setEventSpies(eventSpies);
        }

        MavenExecutionRequest request = getMavenExecutionRequest(args, container);

        MavenExecutionResult result = maven.execute(request);
        hudsonMavenExecutionResult = new HudsonMavenExecutionResult(result);

        // we don't care about cli mavenExecutionResult will be study in the the plugin
        return 0;// cli.doMain( args, null );
    } catch (ComponentLookupException e) {
        throw new Exception(e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(orig);
    }
}

From source file:org.jvnet.hudson.maven3.launcher.Maven3Launcher.java

License:Apache License

public static int main(String[] args) throws Exception {
    ClassLoader orig = Thread.currentThread().getContextClassLoader();
    try {/*from   w  ww.j  a  v a 2 s .c o  m*/

        ClassRealm containerRealm = (ClassRealm) Thread.currentThread().getContextClassLoader();

        ContainerConfiguration cc = new DefaultContainerConfiguration().setName("maven")
                .setRealm(containerRealm);

        DefaultPlexusContainer container = new DefaultPlexusContainer(cc);
        MavenLoggerManager mavenLoggerManager = new MavenLoggerManager(new PrintStreamLogger(System.out));
        container.setLoggerManager(mavenLoggerManager);

        Maven maven = (Maven) container.lookup("org.apache.maven.Maven", "default");
        /*
        EventSpyDispatcher eventSpyDispatcher = container.lookup( EventSpyDispatcher.class );
                
        if ( eventSpiesList != null && !eventSpiesList.isEmpty())
        {
        List<EventSpy> eventSpies = eventSpyDispatcher.getEventSpies();
        if ( eventSpies == null )
        {
            eventSpies = new ArrayList<EventSpy>( 1 );
        }
        eventSpies.addAll( eventSpiesList );
                
        // get event spies added with plexus components
        // see Maven31Maven addPlexusComponents
        // PlexusModuleContributor extension
        List<EventSpy> spies = container.lookupList( EventSpy.class );
        if (spies != null && !spies.isEmpty())
        {
            eventSpies.addAll( spies );
        }
                
        eventSpyDispatcher.setEventSpies( eventSpies );
        }
        */
        MavenExecutionRequest request = getMavenExecutionRequest(args, container);

        MavenExecutionResult result = maven.execute(request);
        hudsonMavenExecutionResult = new HudsonMavenExecutionResult(result);

        // we don't care about cli mavenExecutionResult will be study in the the plugin
        return 0;// cli.doMain( args, null );
    } catch (ComponentLookupException e) {
        throw new Exception(e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(orig);
    }
}

From source file:org.rapidoid.plugin.app.EmbeddedMavenCli.java

License:Apache License

private void executeReq(List<String> goals, String baseDirectory, boolean updateSnapshots,
        Map<String, String> properties) throws Exception {

    PlexusContainer container = session.getContainer();
    Maven maven = container.lookup(Maven.class);
    MavenExecutionRequestPopulator executionRequestPopulator = container
            .lookup(MavenExecutionRequestPopulator.class);

    MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    request = executionRequestPopulator.populateDefaults(request);

    request.setPom(session.getRequest().getPom());
    request.setGoals(goals);//from   ww  w  . j  ava 2  s.  c o  m
    request.setUpdateSnapshots(updateSnapshots);
    request.setBaseDirectory(new File(baseDirectory));

    Properties props = new Properties();
    props.putAll(properties);
    request.setUserProperties(props);

    String localRepo = System.getProperty("maven.repo.local");
    if (localRepo != null) {
        request.setLocalRepositoryPath(localRepo);
        ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
        ArtifactRepository repo = new DefaultArtifactRepository("repo", "file://" + localRepo, layout);

        request.setLocalRepository(repo);
        props.put("maven.repo.local", localRepo);
    }

    MavenExecutionResult result = maven.execute(request);

    if (result.hasExceptions()) {
        for (Throwable e : result.getExceptions()) {
            throw U.rte(e); // TODO is throwing the first exception enough?
        }
    }
}