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);
}
}
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);
}
}
Aggregations