Search in sources :

Example 1 with SwarmProcess

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);
}
Also used : PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) MojoDescriptor(org.apache.maven.plugin.descriptor.MojoDescriptor) MojoExecution(org.apache.maven.plugin.MojoExecution) ArrayList(java.util.ArrayList) List(java.util.List) SwarmProcess(org.wildfly.swarm.tools.exec.SwarmProcess) Plugin(org.apache.maven.model.Plugin)

Example 2 with SwarmProcess

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);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Matcher(java.util.regex.Matcher) List(java.util.List) SwarmProcess(org.wildfly.swarm.tools.exec.SwarmProcess) File(java.io.File)

Example 3 with SwarmProcess

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);
    }
}
Also used : XmlPlexusConfiguration(org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration) PlexusConfiguration(org.codehaus.plexus.configuration.PlexusConfiguration) SwarmExecutor(org.wildfly.swarm.tools.exec.SwarmExecutor) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) SwarmProcess(org.wildfly.swarm.tools.exec.SwarmProcess) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with SwarmProcess

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();
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SwarmExecutor(org.wildfly.swarm.tools.exec.SwarmExecutor) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) SwarmProcess(org.wildfly.swarm.tools.exec.SwarmProcess) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) FractionList(org.wildfly.swarm.fractions.FractionList) List(java.util.List) File(java.io.File)

Aggregations

List (java.util.List)4 SwarmProcess (org.wildfly.swarm.tools.exec.SwarmProcess)4 ArrayList (java.util.ArrayList)3 File (java.io.File)2 IOException (java.io.IOException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 SwarmExecutor (org.wildfly.swarm.tools.exec.SwarmExecutor)2 Path (java.nio.file.Path)1 StringTokenizer (java.util.StringTokenizer)1 Matcher (java.util.regex.Matcher)1 Plugin (org.apache.maven.model.Plugin)1 MojoExecution (org.apache.maven.plugin.MojoExecution)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoDescriptor (org.apache.maven.plugin.descriptor.MojoDescriptor)1 PluginDescriptor (org.apache.maven.plugin.descriptor.PluginDescriptor)1 PlexusConfiguration (org.codehaus.plexus.configuration.PlexusConfiguration)1 XmlPlexusConfiguration (org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration)1 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)1 FractionList (org.wildfly.swarm.fractions.FractionList)1