Search in sources :

Example 1 with DefaultInvocationRequest

use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project spring-boot by spring-projects.

the class ApplicationBuilder method packageApplication.

private void packageApplication(File appFolder) throws MavenInvocationException {
    InvocationRequest invocation = new DefaultInvocationRequest();
    invocation.setBaseDirectory(appFolder);
    invocation.setGoals(Collections.singletonList("package"));
    InvocationResult execute = new DefaultInvoker().execute(invocation);
    assertThat(execute.getExitCode()).isEqualTo(0);
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationResult(org.apache.maven.shared.invoker.InvocationResult)

Example 2 with DefaultInvocationRequest

use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project maven-plugins by apache.

the class InvokerPropertiesTest method testConfigureRequestMavenOpts.

public void testConfigureRequestMavenOpts() throws Exception {
    Properties props = new Properties();
    InvokerProperties facade = new InvokerProperties(props);
    InvocationRequest request = new DefaultInvocationRequest();
    request.setMavenOpts("default");
    facade.configureInvocation(request, 0);
    assertEquals("default", request.getMavenOpts());
    props.setProperty("invoker.mavenOpts", "-Xmx512m");
    facade.configureInvocation(request, 0);
    assertEquals("-Xmx512m", request.getMavenOpts());
}
Also used : InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) Properties(java.util.Properties)

Example 3 with DefaultInvocationRequest

use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project maven-plugins by apache.

the class InvokerPropertiesTest method testConfigureRequestOffline.

public void testConfigureRequestOffline() throws Exception {
    Properties props = new Properties();
    InvokerProperties facade = new InvokerProperties(props);
    InvocationRequest request = new DefaultInvocationRequest();
    request.setOffline(true);
    facade.configureInvocation(request, 0);
    assertTrue(request.isOffline());
    request.setOffline(false);
    facade.configureInvocation(request, 0);
    assertFalse(request.isOffline());
    props.setProperty("invoker.offline", "true");
    facade.configureInvocation(request, 0);
    assertTrue(request.isOffline());
    props.setProperty("invoker.offline", "false");
    facade.configureInvocation(request, 0);
    assertFalse(request.isOffline());
}
Also used : InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) Properties(java.util.Properties)

Example 4 with DefaultInvocationRequest

use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project maven-plugins by apache.

the class InvokerPropertiesTest method testConfigureRequestGoals.

public void testConfigureRequestGoals() throws Exception {
    Properties props = new Properties();
    InvokerProperties facade = new InvokerProperties(props);
    InvocationRequest request = new DefaultInvocationRequest();
    request.setGoals(Collections.singletonList("test"));
    facade.configureInvocation(request, 0);
    assertEquals(Collections.singletonList("test"), request.getGoals());
    props.setProperty("invoker.goals", "verify");
    facade.configureInvocation(request, 0);
    assertEquals(Collections.singletonList("verify"), request.getGoals());
    props.setProperty("invoker.goals", "   ");
    facade.configureInvocation(request, 0);
    assertEquals(Arrays.asList(new String[0]), request.getGoals());
    props.setProperty("invoker.goals", "  clean , test   verify  ");
    facade.configureInvocation(request, 0);
    assertEquals(Arrays.asList(new String[] { "clean", "test", "verify" }), request.getGoals());
    props.setProperty("invoker.goals", "");
    facade.configureInvocation(request, 0);
    assertEquals(Arrays.asList(new String[0]), request.getGoals());
}
Also used : InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) Properties(java.util.Properties)

Example 5 with DefaultInvocationRequest

use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project maven-plugins by apache.

the class AbstractInvokerMojo method runBuild.

/**
     * Runs the specified project.
     *
     * @param basedir The base directory of the project, must not be <code>null</code>.
     * @param pomFile The (already interpolated) POM file, may be <code>null</code> for a POM-less Maven invocation.
     * @param settingsFile The (already interpolated) user settings file for the build, may be <code>null</code>. Will
     *            be merged with the settings file of the invoking Maven process.
     * @param invokerProperties The properties to use.
     * @return <code>true</code> if the project was launched or <code>false</code> if the selector script indicated that
     *         the project should be skipped.
     * @throws org.apache.maven.plugin.MojoExecutionException If the project could not be launched.
     * @throws org.apache.maven.shared.scriptinterpreter.RunFailureException If either a hook script or the build itself
     *             failed.
     */
private boolean runBuild(File basedir, File pomFile, File settingsFile, File actualJavaHome, InvokerProperties invokerProperties) throws MojoExecutionException, RunFailureException {
    if (getLog().isDebugEnabled() && !invokerProperties.getProperties().isEmpty()) {
        Properties props = invokerProperties.getProperties();
        getLog().debug("Using invoker properties:");
        for (String key : new TreeSet<String>(props.stringPropertyNames())) {
            String value = props.getProperty(key);
            getLog().debug("  " + key + " = " + value);
        }
    }
    List<String> goals = getGoals(basedir);
    List<String> profiles = getProfiles(basedir);
    Map<String, Object> context = new LinkedHashMap<String, Object>();
    FileLogger logger = setupBuildLogFile(basedir);
    try {
        try {
            scriptRunner.run("selector script", basedir, selectorScript, context, logger, BuildJob.Result.SKIPPED, false);
        } catch (RunErrorException e) {
            throw e;
        } catch (RunFailureException e) {
            return false;
        }
        scriptRunner.run("pre-build script", basedir, preBuildHookScript, context, logger, BuildJob.Result.FAILURE_PRE_HOOK, false);
        final InvocationRequest request = new DefaultInvocationRequest();
        request.setLocalRepositoryDirectory(localRepositoryPath);
        request.setBatchMode(true);
        request.setShowErrors(showErrors);
        request.setDebug(debug);
        request.setShowVersion(showVersion);
        setupLoggerForBuildJob(logger, request);
        if (mavenHome != null) {
            invoker.setMavenHome(mavenHome);
            // FIXME: Should we really take care of M2_HOME?
            request.addShellEnvironment("M2_HOME", mavenHome.getAbsolutePath());
        }
        if (mavenExecutable != null) {
            invoker.setMavenExecutable(new File(mavenExecutable));
        }
        if (actualJavaHome != null) {
            request.setJavaHome(actualJavaHome);
        }
        if (environmentVariables != null) {
            for (Map.Entry<String, String> variable : environmentVariables.entrySet()) {
                request.addShellEnvironment(variable.getKey(), variable.getValue());
            }
        }
        for (int invocationIndex = 1; ; invocationIndex++) {
            if (invocationIndex > 1 && !invokerProperties.isInvocationDefined(invocationIndex)) {
                break;
            }
            request.setBaseDirectory(basedir);
            request.setPomFile(pomFile);
            request.setGoals(goals);
            request.setProfiles(profiles);
            request.setMavenOpts(mavenOpts);
            request.setOffline(false);
            request.setUserSettingsFile(settingsFile);
            Properties systemProperties = getSystemProperties(basedir, invokerProperties.getSystemPropertiesFile(invocationIndex));
            request.setProperties(systemProperties);
            invokerProperties.configureInvocation(request, invocationIndex);
            if (getLog().isDebugEnabled()) {
                try {
                    getLog().debug("Using MAVEN_OPTS: " + request.getMavenOpts());
                    getLog().debug("Executing: " + new MavenCommandLineBuilder().build(request));
                } catch (CommandLineConfigurationException e) {
                    getLog().debug("Failed to display command line: " + e.getMessage());
                }
            }
            InvocationResult result;
            try {
                result = invoker.execute(request);
            } catch (final MavenInvocationException e) {
                getLog().debug("Error invoking Maven: " + e.getMessage(), e);
                throw new RunFailureException("Maven invocation failed. " + e.getMessage(), BuildJob.Result.FAILURE_BUILD);
            }
            verify(result, invocationIndex, invokerProperties, logger);
        }
        scriptRunner.run("post-build script", basedir, postBuildHookScript, context, logger, BuildJob.Result.FAILURE_POST_HOOK, true);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        if (logger != null) {
            logger.close();
        }
    }
    return true;
}
Also used : RunErrorException(org.apache.maven.shared.scriptinterpreter.RunErrorException) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) IOException(java.io.IOException) Properties(java.util.Properties) InvocationResult(org.apache.maven.shared.invoker.InvocationResult) LinkedHashMap(java.util.LinkedHashMap) RunFailureException(org.apache.maven.shared.scriptinterpreter.RunFailureException) TreeSet(java.util.TreeSet) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) MavenCommandLineBuilder(org.apache.maven.shared.invoker.MavenCommandLineBuilder) CommandLineConfigurationException(org.apache.maven.shared.invoker.CommandLineConfigurationException) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

DefaultInvocationRequest (org.apache.maven.shared.invoker.DefaultInvocationRequest)12 InvocationRequest (org.apache.maven.shared.invoker.InvocationRequest)12 Properties (java.util.Properties)8 InvokerProperties (org.apache.maven.plugins.invoker.InvokerProperties)7 File (java.io.File)5 InvocationResult (org.apache.maven.shared.invoker.InvocationResult)5 MavenInvocationException (org.apache.maven.shared.invoker.MavenInvocationException)4 DefaultInvoker (org.apache.maven.shared.invoker.DefaultInvoker)3 IOException (java.io.IOException)2 Invoker (org.apache.maven.shared.invoker.Invoker)2 Reader (java.io.Reader)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 CommandLineConfigurationException (org.apache.maven.shared.invoker.CommandLineConfigurationException)1 MavenCommandLineBuilder (org.apache.maven.shared.invoker.MavenCommandLineBuilder)1 RunErrorException (org.apache.maven.shared.scriptinterpreter.RunErrorException)1 RunFailureException (org.apache.maven.shared.scriptinterpreter.RunFailureException)1