Search in sources :

Example 1 with MavenEmbedder

use of org.appformer.maven.integration.embedder.MavenEmbedder in project kie-wb-common by kiegroup.

the class MavenBuildExecutor method executeMaven.

public static void executeMaven(final File pom, final PrintStream stdout, final PrintStream stderr, final Properties properties, final String... goals) {
    final PrintStream oldout = System.out;
    final PrintStream olderr = System.err;
    final Properties oldProperties = System.getProperties();
    if (properties != null) {
        properties.keySet().forEach((o) -> {
            if (properties.getProperty((String) o) != null) {
                System.setProperty((String) o, properties.getProperty((String) o));
            }
        });
    }
    final MavenEmbedder mavenEmbedder = newMavenEmbedder();
    try {
        if (stdout != null) {
            System.setOut(stdout);
        }
        if (stderr != null) {
            System.setErr(stderr);
        }
        final MavenRequest mavenRequest = MavenProjectLoader.createMavenRequest(false);
        mavenRequest.setGoals(Arrays.asList(goals));
        mavenRequest.setPom(pom.getAbsolutePath());
        final MavenExecutionResult result = mavenEmbedder.execute(mavenRequest);
        if (result.hasExceptions()) {
            for (Throwable t : result.getExceptions()) {
                LOG.error("Error Running Maven", t);
            }
            throw new BuildException("Maven found issues trying to build the pom file: " + pom.getAbsolutePath() + ". Look at the Error Logs for more information");
        }
    } catch (final MavenEmbedderException ex) {
        throw new BuildException("Maven coudn't build the project for pom file: " + pom.getAbsolutePath(), ex);
    } finally {
        System.setProperties(oldProperties);
        mavenEmbedder.dispose();
        System.setOut(oldout);
        System.setErr(olderr);
    }
}
Also used : PrintStream(java.io.PrintStream) MavenEmbedder(org.appformer.maven.integration.embedder.MavenEmbedder) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) MavenEmbedderException(org.appformer.maven.integration.embedder.MavenEmbedderException) BuildException(org.guvnor.ala.exceptions.BuildException) Properties(java.util.Properties) MavenRequest(org.appformer.maven.integration.embedder.MavenRequest)

Example 2 with MavenEmbedder

use of org.appformer.maven.integration.embedder.MavenEmbedder in project kie-wb-common by kiegroup.

the class ExecuteGoalsCommandTest method createMavenEmbedderTest.

@Test
public void createMavenEmbedderTest() throws MavenEmbedderException {
    final ExecuteGoalsCommand command = new ExecuteGoalsCommand(BASE_DIRECTORY);
    final MavenEmbedder mavenEmbedder = command.createMavenEmbedder();
    assertFalse(mavenEmbedder.getMavenExecutionRequest().isOffline());
}
Also used : MavenEmbedder(org.appformer.maven.integration.embedder.MavenEmbedder) Test(org.junit.Test)

Example 3 with MavenEmbedder

use of org.appformer.maven.integration.embedder.MavenEmbedder in project kie-wb-common by kiegroup.

the class BuildProjectCommandTest method executeSuccessTest.

@Test
public void executeSuccessTest() throws MavenEmbedderException, ProjectBuildingException {
    final BuildProjectCommand command = spy(new BuildProjectCommand(BASE_DIRECTORY));
    final MavenEmbedder mavenEmbedder = mock(MavenEmbedder.class);
    doReturn(Collections.emptyList()).when(mavenEmbedder).buildProjects(any(File.class), eq(false));
    doReturn(mavenEmbedder).when(command).createMavenEmbedder();
    final MavenExecutionResult executionResult = command.execute();
    assertFalse(executionResult.hasExceptions());
}
Also used : MavenEmbedder(org.appformer.maven.integration.embedder.MavenEmbedder) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) File(java.io.File) Test(org.junit.Test)

Example 4 with MavenEmbedder

use of org.appformer.maven.integration.embedder.MavenEmbedder in project kie-wb-common by kiegroup.

the class BuildProjectCommandTest method executeFailedTest.

@Test
public void executeFailedTest() throws MavenEmbedderException, ProjectBuildingException {
    final BuildProjectCommand command = spy(new BuildProjectCommand(BASE_DIRECTORY));
    final MavenEmbedder mavenEmbedder = mock(MavenEmbedder.class);
    doThrow(ProjectBuildingException.class).when(mavenEmbedder).buildProjects(any(File.class), eq(false));
    doReturn(mavenEmbedder).when(command).createMavenEmbedder();
    final MavenExecutionResult executionResult = command.execute();
    assertTrue(executionResult.hasExceptions());
}
Also used : MavenEmbedder(org.appformer.maven.integration.embedder.MavenEmbedder) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) File(java.io.File) Test(org.junit.Test)

Example 5 with MavenEmbedder

use of org.appformer.maven.integration.embedder.MavenEmbedder in project kie-wb-common by kiegroup.

the class AbstractMavenCommand method execute.

public MavenExecutionResult execute() throws MavenEmbedderException {
    final MavenEmbedder mavenEmbedder = createMavenEmbedder();
    final MavenRequest mavenRequest = prepareExecution();
    try {
        return mavenEmbedder.execute(mavenRequest);
    } finally {
        mavenEmbedder.dispose();
    }
}
Also used : MavenEmbedder(org.appformer.maven.integration.embedder.MavenEmbedder) MavenRequest(org.appformer.maven.integration.embedder.MavenRequest)

Aggregations

MavenEmbedder (org.appformer.maven.integration.embedder.MavenEmbedder)7 MavenExecutionResult (org.apache.maven.execution.MavenExecutionResult)5 Test (org.junit.Test)4 File (java.io.File)3 MavenRequest (org.appformer.maven.integration.embedder.MavenRequest)3 PrintStream (java.io.PrintStream)1 Properties (java.util.Properties)1 DefaultMavenExecutionResult (org.apache.maven.execution.DefaultMavenExecutionResult)1 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)1 MavenEmbedderException (org.appformer.maven.integration.embedder.MavenEmbedderException)1 BuildException (org.guvnor.ala.exceptions.BuildException)1