Search in sources :

Example 1 with RunFailureException

use of org.apache.maven.shared.scriptinterpreter.RunFailureException 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);
    boolean selectorResult = true;
    try {
        try {
            scriptRunner.run("selector script", basedir, selectorScript, context, logger, BuildJob.Result.SKIPPED, false);
        } catch (RunErrorException e) {
            selectorResult = false;
            throw e;
        } catch (RunFailureException e) {
            selectorResult = false;
            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);
            String customSettingsFile = invokerProperties.getSettingsFile(invocationIndex);
            if (customSettingsFile != null) {
                File interpolateSettingsFile = interpolateSettings(new File(customSettingsFile));
                File mergeSettingsFile = mergeSettings(interpolateSettingsFile);
                request.setUserSettingsFile(mergeSettingsFile);
            } else {
                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);
        }
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        if (selectorResult) {
            runPostBuildHook(basedir, context, logger);
        }
        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)

Example 2 with RunFailureException

use of org.apache.maven.shared.scriptinterpreter.RunFailureException in project maven-plugins by apache.

the class AbstractInvokerMojo method runBuild.

/**
 * Runs the specified project.
 *
 * @param projectsDir The base directory of all projects, must not be <code>null</code>.
 * @param buildJob The build job to run, must not be <code>null</code>.
 * @param settingsFile The (already interpolated) user settings file for the build, may be <code>null</code> to use
 *            the current user settings.
 * @throws org.apache.maven.plugin.MojoExecutionException If the project could not be launched.
 */
private void runBuild(File projectsDir, BuildJob buildJob, File settingsFile, File actualJavaHome, CharSequence actualJreVersion) throws MojoExecutionException {
    // FIXME: Think about the following code part -- START
    File pomFile = new File(projectsDir, buildJob.getProject());
    File basedir;
    if (pomFile.isDirectory()) {
        basedir = pomFile;
        pomFile = new File(basedir, "pom.xml");
        if (!pomFile.exists()) {
            pomFile = null;
        } else {
            buildJob.setProject(buildJob.getProject() + File.separator + "pom.xml");
        }
    } else {
        basedir = pomFile.getParentFile();
    }
    File interpolatedPomFile = interpolatePomFile(pomFile, basedir);
    // FIXME: Think about the following code part -- ^^^^^^^ END
    getLog().info(buffer().a("Building: ").strong(buildJob.getProject()).toString());
    InvokerProperties invokerProperties = getInvokerProperties(basedir);
    // let's set what details we can
    buildJob.setName(invokerProperties.getJobName());
    buildJob.setDescription(invokerProperties.getJobDescription());
    try {
        int selection = getSelection(invokerProperties, actualJreVersion);
        if (selection == 0) {
            long milliseconds = System.currentTimeMillis();
            boolean executed;
            try {
                // CHECKSTYLE_OFF: LineLength
                executed = runBuild(basedir, interpolatedPomFile, settingsFile, actualJavaHome, invokerProperties);
            // CHECKSTYLE_ON: LineLength
            } finally {
                milliseconds = System.currentTimeMillis() - milliseconds;
                buildJob.setTime(milliseconds / 1000.0);
            }
            if (executed) {
                buildJob.setResult(BuildJob.Result.SUCCESS);
                if (!suppressSummaries) {
                    getLog().info(pad(buildJob).success("SUCCESS").a(' ') + formatTime(buildJob.getTime()));
                }
            } else {
                buildJob.setResult(BuildJob.Result.SKIPPED);
                if (!suppressSummaries) {
                    getLog().info(pad(buildJob).warning("SKIPPED").a(' ') + formatTime(buildJob.getTime()));
                }
            }
        } else {
            buildJob.setResult(BuildJob.Result.SKIPPED);
            StringBuilder message = new StringBuilder();
            if (selection == Selector.SELECTOR_MULTI) {
                message.append("non-matching selectors");
            } else {
                if ((selection & Selector.SELECTOR_MAVENVERSION) != 0) {
                    message.append("Maven version");
                }
                if ((selection & Selector.SELECTOR_JREVERSION) != 0) {
                    if (message.length() > 0) {
                        message.append(", ");
                    }
                    message.append("JRE version");
                }
                if ((selection & Selector.SELECTOR_OSFAMILY) != 0) {
                    if (message.length() > 0) {
                        message.append(", ");
                    }
                    message.append("OS");
                }
            }
            if (!suppressSummaries) {
                getLog().info(pad(buildJob).warning("SKIPPED") + " due to " + message.toString());
            }
            // Abuse failureMessage, the field in the report which should contain the reason for skipping
            // Consider skipCode + I18N
            buildJob.setFailureMessage("Skipped due to " + message.toString());
        }
    } catch (RunErrorException e) {
        buildJob.setResult(BuildJob.Result.ERROR);
        buildJob.setFailureMessage(e.getMessage());
        if (!suppressSummaries) {
            getLog().info(pad(buildJob).failure("ERROR").a(' ') + formatTime(buildJob.getTime()));
            getLog().info("  " + e.getMessage());
        }
    } catch (RunFailureException e) {
        buildJob.setResult(e.getType());
        buildJob.setFailureMessage(e.getMessage());
        if (!suppressSummaries) {
            getLog().info(pad(buildJob).failure("FAILED").a(' ') + formatTime(buildJob.getTime()));
            getLog().info("  " + e.getMessage());
        }
    } finally {
        deleteInterpolatedPomFile(interpolatedPomFile);
        writeBuildReport(buildJob);
    }
}
Also used : RunErrorException(org.apache.maven.shared.scriptinterpreter.RunErrorException) RunFailureException(org.apache.maven.shared.scriptinterpreter.RunFailureException) File(java.io.File)

Example 3 with RunFailureException

use of org.apache.maven.shared.scriptinterpreter.RunFailureException in project maven-archetype by apache.

the class IntegrationTestMojo method invokePostArchetypeGenerationGoals.

private void invokePostArchetypeGenerationGoals(String goals, File basedir, File goalFile) throws IntegrationTestFailure, IOException, MojoExecutionException {
    FileLogger logger = setupLogger(basedir);
    if (!StringUtils.isBlank(goals)) {
        getLog().info("Invoking post-archetype-generation goals: " + goals);
        if (!localRepositoryPath.exists()) {
            localRepositoryPath.mkdirs();
        }
        // @formatter:off
        InvocationRequest request = new DefaultInvocationRequest().setBaseDirectory(basedir).setGoals(Arrays.asList(StringUtils.split(goals, ","))).setLocalRepositoryDirectory(localRepositoryPath).setInteractive(false).setShowErrors(true);
        // @formatter:on
        request.setDebug(debug);
        request.setShowVersion(showVersion);
        if (logger != null) {
            request.setErrorHandler(logger);
            request.setOutputHandler(logger);
        }
        File interpolatedSettingsFile = null;
        if (settingsFile != null) {
            File interpolatedSettingsDirectory = new File(project.getBuild().getOutputDirectory(), "archetype-it");
            if (interpolatedSettingsDirectory.exists()) {
                FileUtils.deleteDirectory(interpolatedSettingsDirectory);
            }
            interpolatedSettingsDirectory.mkdir();
            interpolatedSettingsFile = new File(interpolatedSettingsDirectory, "interpolated-" + settingsFile.getName());
            buildInterpolatedFile(settingsFile, interpolatedSettingsFile);
            request.setUserSettingsFile(interpolatedSettingsFile);
        }
        try {
            InvocationResult result = invoker.execute(request);
            getLog().info("Post-archetype-generation invoker exit code: " + result.getExitCode());
            if (result.getExitCode() != 0) {
                throw new IntegrationTestFailure("Execution failure: exit code = " + result.getExitCode(), result.getExecutionException());
            }
        } catch (MavenInvocationException e) {
            throw new IntegrationTestFailure("Cannot run additions goals.", e);
        }
    } else {
        getLog().info("No post-archetype-generation goals to invoke.");
    }
    // verify result
    ScriptRunner scriptRunner = new ScriptRunner(getLog());
    scriptRunner.setScriptEncoding(encoding);
    Map<String, Object> context = new LinkedHashMap<String, Object>();
    context.put("projectDir", basedir);
    try {
        scriptRunner.run("post-build script", goalFile.getParentFile(), postBuildHookScript, context, logger, "failure post script", true);
    } catch (RunFailureException e) {
        throw new IntegrationTestFailure("post build script failure failure: " + e.getMessage(), e);
    }
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) ScriptRunner(org.apache.maven.shared.scriptinterpreter.ScriptRunner) InvocationResult(org.apache.maven.shared.invoker.InvocationResult) LinkedHashMap(java.util.LinkedHashMap) RunFailureException(org.apache.maven.shared.scriptinterpreter.RunFailureException) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) File(java.io.File)

Aggregations

File (java.io.File)3 RunFailureException (org.apache.maven.shared.scriptinterpreter.RunFailureException)3 LinkedHashMap (java.util.LinkedHashMap)2 DefaultInvocationRequest (org.apache.maven.shared.invoker.DefaultInvocationRequest)2 InvocationRequest (org.apache.maven.shared.invoker.InvocationRequest)2 InvocationResult (org.apache.maven.shared.invoker.InvocationResult)2 MavenInvocationException (org.apache.maven.shared.invoker.MavenInvocationException)2 RunErrorException (org.apache.maven.shared.scriptinterpreter.RunErrorException)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)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 ScriptRunner (org.apache.maven.shared.scriptinterpreter.ScriptRunner)1