Search in sources :

Example 1 with StandardOutput

use of org.wildfly.plugin.common.StandardOutput in project wildfly-maven-plugin by wildfly.

the class StartMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    final Log log = getLog();
    if (skip) {
        log.debug("Skipping server start");
        return;
    }
    MavenRepositoriesEnricher.enrich(mavenSession, project, repositories);
    mavenRepoManager = new MavenArtifactRepositoryManager(repoSystem, session, repositories);
    // Validate the environment
    final Path jbossHome = provisionIfRequired(targetDir.toPath().resolve(provisioningDir));
    if (!ServerHelper.isValidHomeDirectory(jbossHome)) {
        throw new MojoExecutionException(String.format("JBOSS_HOME '%s' is not a valid directory.", jbossHome));
    }
    // Determine how stdout should be consumed
    try {
        final StandardOutput out = StandardOutput.parse(stdout, true);
        // the maven process may have been finished
        try (ModelControllerClient client = createClient()) {
            if (ServerHelper.isStandaloneRunning(client) || ServerHelper.isDomainRunning(client)) {
                throw new MojoExecutionException(String.format("%s server is already running?", serverType));
            }
            final CommandBuilder commandBuilder = createCommandBuilder(jbossHome);
            log.info(String.format("%s server is starting up.", serverType));
            final Launcher launcher = Launcher.of(commandBuilder).setRedirectErrorStream(true);
            if (env != null) {
                launcher.addEnvironmentVariables(env);
            }
            out.getRedirect().ifPresent(launcher::redirectOutput);
            final Process process = launcher.launch();
            // Note that if this thread is started and no shutdown goal is executed this stop the stdout and stderr
            // from being logged any longer. The user was warned in the documentation.
            out.startConsumer(process);
            if (serverType == ServerType.DOMAIN) {
                ServerHelper.waitForDomain(process, DomainClient.Factory.create(client), startupTimeout);
            } else {
                ServerHelper.waitForStandalone(process, client, startupTimeout);
            }
            if (!process.isAlive()) {
                throw new MojoExecutionException("The process has been terminated before the start goal has completed.");
            }
        }
    } catch (MojoExecutionException e) {
        throw e;
    } catch (Exception e) {
        throw new MojoExecutionException("The server failed to start", e);
    }
}
Also used : Path(java.nio.file.Path) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) Log(org.apache.maven.plugin.logging.Log) MavenArtifactRepositoryManager(org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager) Launcher(org.wildfly.core.launcher.Launcher) StandardOutput(org.wildfly.plugin.common.StandardOutput) DomainCommandBuilder(org.wildfly.core.launcher.DomainCommandBuilder) StandaloneCommandBuilder(org.wildfly.core.launcher.StandaloneCommandBuilder) CommandBuilder(org.wildfly.core.launcher.CommandBuilder) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ProvisioningException(org.jboss.galleon.ProvisioningException)

Example 2 with StandardOutput

use of org.wildfly.plugin.common.StandardOutput in project wildfly-maven-plugin by wildfly.

the class AbstractCommandExecutor method executeInNewProcess.

private void executeInNewProcess(final T config, final Path scriptFile) throws MojoExecutionException {
    getLogger().debug("Executing CLI scripts");
    try {
        final StandardOutput out = StandardOutput.parse(config.getStdout(), false, config.isAppend());
        final int exitCode = executeInNewProcess(config, scriptFile, out);
        if (exitCode != 0) {
            final StringBuilder msg = new StringBuilder("Failed to execute commands: ");
            switch(out.getTarget()) {
                case COLLECTING:
                    msg.append(out);
                    break;
                case FILE:
                    final Path stdoutPath = out.getStdoutPath();
                    msg.append("See ").append(stdoutPath).append(" for full details of failure.").append(System.lineSeparator());
                    final List<String> lines = Files.readAllLines(stdoutPath);
                    lines.subList(Math.max(lines.size() - 4, 0), lines.size()).forEach(line -> msg.append(line).append(System.lineSeparator()));
                    break;
                case SYSTEM_ERR:
                case SYSTEM_OUT:
                case INHERIT:
                    msg.append("See previous messages for failure messages.");
                    break;
                default:
                    msg.append("Reason unknown");
            }
            if (config.isFailOnError()) {
                throw new MojoExecutionException(msg.toString());
            } else {
                getLogger().warn(msg.toString());
            }
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to execute scripts.", e);
    }
}
Also used : Path(java.nio.file.Path) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StandardOutput(org.wildfly.plugin.common.StandardOutput) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 StandardOutput (org.wildfly.plugin.common.StandardOutput)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 Log (org.apache.maven.plugin.logging.Log)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1 ProvisioningException (org.jboss.galleon.ProvisioningException)1 MavenArtifactRepositoryManager (org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager)1 CommandBuilder (org.wildfly.core.launcher.CommandBuilder)1 DomainCommandBuilder (org.wildfly.core.launcher.DomainCommandBuilder)1 Launcher (org.wildfly.core.launcher.Launcher)1 StandaloneCommandBuilder (org.wildfly.core.launcher.StandaloneCommandBuilder)1