use of org.wildfly.swarm.tools.exec.SwarmProcess in project wildfly-swarm by wildfly-swarm.
the class MultiStartMojo method startProject.
@SuppressWarnings("unchecked")
protected void startProject(MavenProject project, String executionId, XmlPlexusConfiguration process) throws InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, MojoFailureException, MojoExecutionException, PluginManagerException {
Plugin plugin = this.project.getPlugin("org.wildfly.swarm:wildfly-swarm-plugin");
Xpp3Dom config = getConfiguration(project, executionId);
Xpp3Dom processConfig = getProcessConfiguration(process);
Xpp3Dom globalConfig = getGlobalConfig();
Xpp3Dom mergedConfig = Xpp3DomUtils.mergeXpp3Dom(processConfig, config);
mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, globalConfig);
PluginDescriptor pluginDescriptor = this.pluginManager.loadPlugin(plugin, project.getRemotePluginRepositories(), this.repositorySystemSession);
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo("start");
MojoExecution mojoExecution = new MojoExecution(mojoDescriptor, mergedConfig);
mavenSession.setCurrentProject(project);
this.pluginManager.executeMojo(mavenSession, mojoExecution);
List<SwarmProcess> launched = (List<SwarmProcess>) mavenSession.getPluginContext(pluginDescriptor, project).get(SWARM_PROCESS);
List<SwarmProcess> procs = (List<SwarmProcess>) getPluginContext().get(SWARM_PROCESS);
if (procs == null) {
procs = new ArrayList<>();
getPluginContext().put(SWARM_PROCESS, procs);
}
procs.addAll(launched);
mavenSession.setCurrentProject(this.project);
}
use of org.wildfly.swarm.tools.exec.SwarmProcess in project wildfly-swarm by wildfly-swarm.
the class StopMojo method executeSpecific.
@SuppressWarnings("unchecked")
@Override
public void executeSpecific() throws MojoExecutionException, MojoFailureException {
if (this.execution.getExecutionId().equals("default-cli")) {
getLog().error("wildfly-swarm:stop is not usable from the CLI");
return;
}
List<SwarmProcess> value = (List<SwarmProcess>) getPluginContext().get("swarm-process");
if (value == null) {
getLog().error("No known processes to stop");
return;
}
for (SwarmProcess each : value) {
stop(each);
}
File tmp = (File) getPluginContext().get("swarm-cp-file");
if (tmp != null && tmp.exists()) {
tmp.delete();
}
Path tmpDir = new File(System.getProperty("java.io.tmpdir")).toPath();
if (tmpDir.toFile().exists()) {
File[] filesTmp = tmpDir.toFile().listFiles();
for (File tmpFile : filesTmp) {
Matcher matcher = tempFilePattern.matcher(tmpFile.getName());
if (matcher.matches()) {
TempFileManager.deleteRecursively(tmpFile);
}
}
}
}
use of org.wildfly.swarm.tools.exec.SwarmProcess in project wildfly-swarm by wildfly-swarm.
the class MultiStartMojo method startArtifact.
@SuppressWarnings("unchecked")
protected void startArtifact(Artifact artifact, XmlPlexusConfiguration process) throws InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, MojoFailureException, MojoExecutionException, PluginManagerException {
List<SwarmProcess> procs = (List<SwarmProcess>) getPluginContext().get(SWARM_PROCESS);
if (procs == null) {
procs = new ArrayList<>();
getPluginContext().put(SWARM_PROCESS, procs);
}
SwarmExecutor executor = new SwarmExecutor();
executor.withExecutableJar(artifact.getFile().toPath());
executor.withProperties(this.properties);
executor.withEnvironment(this.environment);
PlexusConfiguration props = process.getChild("properties");
for (PlexusConfiguration each : props.getChildren()) {
executor.withProperty(each.getName(), each.getValue());
}
PlexusConfiguration env = process.getChild("environment");
for (PlexusConfiguration each : env.getChildren()) {
executor.withEnvironment(each.getName(), each.getValue());
}
int startTimeoutSeconds;
try {
startTimeoutSeconds = Integer.valueOf(props.getChild("start.timeout.seconds").getValue("30"));
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Wrong format of the start timeout for " + artifact + "!. Integer expected.", nfe);
}
try {
SwarmProcess launched = executor.execute();
launched.awaitReadiness(startTimeoutSeconds, TimeUnit.SECONDS);
procs.add(launched);
} catch (IOException | InterruptedException e) {
throw new MojoFailureException("Unable to execute: " + artifact, e);
}
}
use of org.wildfly.swarm.tools.exec.SwarmProcess in project wildfly-swarm by wildfly-swarm.
the class StartMojo method executeSpecific.
@SuppressWarnings({ "unchecked", "ThrowableResultOfMethodCallIgnored" })
@Override
public void executeSpecific() throws MojoExecutionException, MojoFailureException {
initProperties(true);
initEnvironment();
final SwarmExecutor executor;
if (this.useUberJar) {
executor = uberJarExecutor();
} else if (this.project.getPackaging().equals("war")) {
executor = warExecutor();
} else if (this.project.getPackaging().equals("jar")) {
executor = jarExecutor();
} else {
throw new MojoExecutionException("Unsupported packaging: " + this.project.getPackaging());
}
executor.withJVMArguments(this.jvmArguments);
if (this.argumentsProp != null) {
StringTokenizer args = new StringTokenizer(this.argumentsProp);
while (args.hasMoreTokens()) {
this.arguments.add(args.nextToken());
}
}
executor.withArguments(this.arguments);
final SwarmProcess process;
try {
File tmp;
try {
tmp = Files.createTempFile("swarm-process-file", null).toFile();
} catch (IOException e) {
throw new MojoFailureException("Error while creating process file");
}
process = executor.withDebug(debugPort).withProcessFile(tmp).withProperties(this.properties).withStdoutFile(this.stdoutFile != null ? this.stdoutFile.toPath() : null).withStderrFile(this.stderrFile != null ? this.stderrFile.toPath() : null).withEnvironment(this.environment).withWorkingDirectory(this.project.getBasedir().toPath()).withProperty("remote.maven.repo", String.join(",", this.project.getRemoteProjectRepositories().stream().map(RemoteRepository::getUrl).collect(Collectors.toList()))).execute();
int startTimeoutSeconds;
try {
startTimeoutSeconds = Integer.valueOf(this.properties.getProperty("start.timeout.seconds", "120"));
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Wrong format of the start timeout!. Integer expected.", nfe);
}
process.awaitReadiness(startTimeoutSeconds, TimeUnit.SECONDS);
if (!process.isAlive()) {
throw new MojoFailureException("Process failed to start");
}
if (process.getError() != null) {
throw new MojoFailureException("Error starting process", process.getError());
}
} catch (IOException e) {
throw new MojoFailureException("unable to execute", e);
} catch (InterruptedException e) {
throw new MojoFailureException("Error waiting for deployment", e);
}
List<SwarmProcess> procs = (List<SwarmProcess>) getPluginContext().get("swarm-process");
if (procs == null) {
procs = new ArrayList<>();
getPluginContext().put("swarm-process", procs);
}
procs.add(process);
if (waitForProcess) {
try {
process.waitFor();
} catch (InterruptedException e) {
try {
process.stop(10, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
// Do nothing
}
} finally {
process.destroyForcibly();
}
}
}
Aggregations