Search in sources :

Example 1 with DeploymentManager

use of org.wildfly.plugin.core.DeploymentManager in project wildfly-maven-plugin by wildfly.

the class RunMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        return;
    }
    MavenRepositoriesEnricher.enrich(mavenSession, project, repositories);
    mavenRepoManager = new MavenArtifactRepositoryManager(repoSystem, session, repositories);
    final Log log = getLog();
    final Path deploymentContent = getDeploymentContent();
    // The deployment must exist before we do anything
    if (Files.notExists(deploymentContent)) {
        throw new MojoExecutionException(String.format("The deployment '%s' could not be found.", deploymentContent.toAbsolutePath()));
    }
    // Validate the environment
    final Path wildflyPath = provisionIfRequired(deploymentContent.getParent().resolve(provisioningDir));
    if (!ServerHelper.isValidHomeDirectory(wildflyPath)) {
        throw new MojoExecutionException(String.format("JBOSS_HOME '%s' is not a valid directory.", wildflyPath));
    }
    final StandaloneCommandBuilder commandBuilder = createCommandBuilder(wildflyPath);
    // Print some server information
    log.info("JAVA_HOME : " + commandBuilder.getJavaHome());
    log.info("JBOSS_HOME: " + commandBuilder.getWildFlyHome());
    log.info("JAVA_OPTS : " + Utils.toString(commandBuilder.getJavaOptions(), " "));
    try {
        if (addUser != null && addUser.hasUsers()) {
            log.info("Adding users: " + addUser);
            addUser.addUsers(commandBuilder.getWildFlyHome(), commandBuilder.getJavaHome());
        }
        // Start the server
        log.info("Server is starting up. Press CTRL + C to stop the server.");
        Process process = startContainer(commandBuilder);
        try (ModelControllerClient client = createClient()) {
            // Execute commands before the deployment is done
            final CommandConfiguration cmdConfig = CommandConfiguration.of(this::createClient, this::getClientConfiguration).addCommands(commands).addScripts(scripts).setJBossHome(commandBuilder.getWildFlyHome()).setFork(true).setStdout("none").setTimeout(timeout).build();
            commandExecutor.execute(cmdConfig, mavenRepoManager);
            // Create the deployment and deploy
            final Deployment deployment = Deployment.of(deploymentContent).setName(name).setRuntimeName(runtimeName);
            final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
            deploymentManager.forceDeploy(deployment);
        } catch (MojoExecutionException | MojoFailureException e) {
            if (process != null) {
                process.destroyForcibly().waitFor(10L, TimeUnit.SECONDS);
            }
            throw e;
        }
        try {
            // Wait for the process to die
            boolean keepRunning = true;
            while (keepRunning) {
                final int exitCode = process.waitFor();
                // 10 is the magic code used in the scripts to survive a :shutdown(restart=true) operation
                if (exitCode == 10) {
                    // Ensure the current process is destroyed and restart a new one
                    process.destroy();
                    process = startContainer(commandBuilder);
                } else {
                    keepRunning = false;
                }
            }
        } catch (Exception e) {
            throw new MojoExecutionException("The server failed to start", e);
        } finally {
            if (process != null)
                process.destroy();
        }
    } 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) Log(org.apache.maven.plugin.logging.Log) CommandConfiguration(org.wildfly.plugin.cli.CommandConfiguration) DeploymentManager(org.wildfly.plugin.core.DeploymentManager) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Deployment(org.wildfly.plugin.core.Deployment) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ProvisioningException(org.jboss.galleon.ProvisioningException) StandaloneCommandBuilder(org.wildfly.core.launcher.StandaloneCommandBuilder) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) MavenArtifactRepositoryManager(org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager)

Example 2 with DeploymentManager

use of org.wildfly.plugin.core.DeploymentManager in project wildfly-maven-plugin by wildfly.

the class UndeployArtifactMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().debug(String.format("Skipping undeploy of artifact %s:%s", groupId, artifactId));
        return;
    }
    if (artifactId == null) {
        throw new MojoDeploymentException("undeploy-artifact must specify the artifactId");
    }
    if (groupId == null) {
        throw new MojoDeploymentException("undeploy-artifact must specify the groupId");
    }
    final String deploymentName;
    if (name == null) {
        final Set<Artifact> dependencies = project.getDependencyArtifacts();
        Artifact artifact = null;
        for (final Artifact a : dependencies) {
            if (Objects.equals(a.getArtifactId(), artifactId) && Objects.equals(a.getGroupId(), groupId) && Objects.equals(a.getClassifier(), classifier)) {
                artifact = a;
                break;
            }
        }
        if (artifact == null) {
            throw new MojoDeploymentException("Could not resolve artifact to deploy %s:%s", groupId, artifactId);
        }
        deploymentName = artifact.getFile().getName();
    } else {
        deploymentName = name;
    }
    final DeploymentResult result;
    try (ModelControllerClient client = createClient();
        MavenModelControllerClientConfiguration configuration = getClientConfiguration()) {
        final boolean failOnMissing = !ignoreMissingDeployment;
        final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
        result = deploymentManager.undeploy(UndeployDescription.of(deploymentName).addServerGroups(getServerGroups()).setFailOnMissing(failOnMissing));
    } catch (IOException e) {
        throw new MojoFailureException(String.format("Failed to execute %s goal.", goal()), e);
    }
    if (!result.successful()) {
        throw new MojoDeploymentException("Failed to undeploy %s. Reason: %s", deploymentName, result.getFailureMessage());
    }
}
Also used : ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) DeploymentManager(org.wildfly.plugin.core.DeploymentManager) MavenModelControllerClientConfiguration(org.wildfly.plugin.common.MavenModelControllerClientConfiguration) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DeploymentResult(org.wildfly.plugin.core.DeploymentResult) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact)

Example 3 with DeploymentManager

use of org.wildfly.plugin.core.DeploymentManager in project wildfly-maven-plugin by wildfly.

the class UndeployMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().debug(String.format("Skipping undeploy of %s:%s", project.getGroupId(), project.getArtifactId()));
        return;
    }
    final PackageType packageType = PackageType.resolve(project);
    // Configure the name if it wasn't yet set
    if (name == null) {
        name = String.format("%s.%s", project.getBuild().getFinalName(), packageType.getFileExtension());
    }
    if (checkPackaging && packageType.isIgnored()) {
        getLog().debug(String.format("Ignoring packaging type %s.", packageType.getPackaging()));
    } else {
        final DeploymentResult result;
        try (ModelControllerClient client = createClient();
            MavenModelControllerClientConfiguration configuration = getClientConfiguration()) {
            final boolean failOnMissing = !ignoreMissingDeployment;
            final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
            if (matchPattern == null) {
                result = deploymentManager.undeploy(UndeployDescription.of(name).addServerGroups(getServerGroups()).setFailOnMissing(failOnMissing));
            } else {
                final Set<UndeployDescription> matchedDeployments = findDeployments(deploymentManager, failOnMissing);
                if (matchedDeployments.isEmpty()) {
                    if (failOnMissing) {
                        throw new MojoDeploymentException("No deployments matched the match-pattern %s.", matchPattern);
                    }
                    // nothing to undeploy
                    return;
                }
                result = deploymentManager.undeploy(matchedDeployments);
            }
        } catch (IOException e) {
            throw new MojoFailureException("Failed to execute undeploy goal.", e);
        }
        if (!result.successful()) {
            throw new MojoDeploymentException("Failed to undeploy %s. Reason: %s", name, result.getFailureMessage());
        }
    }
}
Also used : UndeployDescription(org.wildfly.plugin.core.UndeployDescription) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) DeploymentManager(org.wildfly.plugin.core.DeploymentManager) MavenModelControllerClientConfiguration(org.wildfly.plugin.common.MavenModelControllerClientConfiguration) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DeploymentResult(org.wildfly.plugin.core.DeploymentResult) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)3 DeploymentManager (org.wildfly.plugin.core.DeploymentManager)3 MavenModelControllerClientConfiguration (org.wildfly.plugin.common.MavenModelControllerClientConfiguration)2 DeploymentResult (org.wildfly.plugin.core.DeploymentResult)2 Path (java.nio.file.Path)1 TimeoutException (java.util.concurrent.TimeoutException)1 Artifact (org.apache.maven.artifact.Artifact)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 Log (org.apache.maven.plugin.logging.Log)1 ProvisioningException (org.jboss.galleon.ProvisioningException)1 MavenArtifactRepositoryManager (org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager)1 StandaloneCommandBuilder (org.wildfly.core.launcher.StandaloneCommandBuilder)1 CommandConfiguration (org.wildfly.plugin.cli.CommandConfiguration)1 Deployment (org.wildfly.plugin.core.Deployment)1 UndeployDescription (org.wildfly.plugin.core.UndeployDescription)1