Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WFMP-53] Fix run goal to work correctly. Refactored to no extend the… #122

Merged
merged 1 commit into from
Aug 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
@SuppressWarnings("WeakerAccess")
final class PackageType implements Comparable<PackageType> {
public final class PackageType implements Comparable<PackageType> {


private static final PackageType MAVEN_PLUGIN = new PackageType("maven-project", true);
Expand Down
171 changes: 134 additions & 37 deletions plugin/src/main/java/org/wildfly/plugin/server/RunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.jboss.as.controller.client.ModelControllerClient;
import org.wildfly.core.launcher.CommandBuilder;
import org.wildfly.core.launcher.StandaloneCommandBuilder;
import org.wildfly.plugin.cli.CommandExecutor;
import org.wildfly.plugin.cli.Commands;
import org.wildfly.plugin.common.AbstractServerConnection;
import org.wildfly.plugin.common.PropertyNames;
import org.wildfly.plugin.core.Deployment;
import org.wildfly.plugin.core.DeploymentManager;
import org.wildfly.plugin.core.ServerHelper;
import org.wildfly.plugin.core.ServerProcess;
import org.wildfly.plugin.deployment.DeployMojo;
import org.wildfly.plugin.deployment.PackageType;
import org.wildfly.plugin.server.ArtifactResolver.ArtifactNameSplitter;

/**
Expand All @@ -59,10 +65,13 @@
*/
@Mojo(name = "run", requiresDependencyResolution = ResolutionScope.RUNTIME)
@Execute(phase = LifecyclePhase.PACKAGE)
public class RunMojo extends DeployMojo {
public class RunMojo extends AbstractServerConnection {

public static final String WILDFLY_DIR = "wildfly-run";

@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;

@Inject
private ArtifactResolver artifactResolver;

Expand Down Expand Up @@ -183,23 +192,92 @@ public class RunMojo extends DeployMojo {
@Parameter
private Map<String, String> env;

private Process process;
/**
* Specifies the name used for the deployment.
*/
@Parameter(property = PropertyNames.DEPLOYMENT_NAME)
private String name;

/**
* The runtime name for the deployment.
* <p>
* In some cases users may wish to have two deployments with the same {@code runtime-name} (e.g. two versions of
* {@code example.war}) both available in the management configuration, in which case the deployments would need to
* have distinct {@code name} values but would have the same {@code runtime-name}.
* </p>
*/
@Parameter(alias = "runtime-name", property = PropertyNames.DEPLOYMENT_RUNTIME_NAME)
private String runtimeName;


/**
* The target directory the application to be deployed is located.
*/
@Parameter(defaultValue = "${project.build.directory}/", property = PropertyNames.DEPLOYMENT_TARGET_DIR)
private File targetDir;

/**
* The file name of the application to be deployed.
* <p>
* The {@code filename} property does have a default of <code>${project.build.finalName}.${project.packaging}</code>.
* The default value is not injected as it normally would be due to packaging types like {@code ejb} that result in
* a file with a {@code .jar} extension rather than an {@code .ejb} extension.
* </p>
*/
@Parameter(property = PropertyNames.DEPLOYMENT_FILENAME)
private String filename;

/**
* By default certain package types are ignored when processing, e.g. {@code maven-project} and {@code pom}. Set
* this value to {@code false} if this check should be bypassed.
*/
@Parameter(alias = "check-packaging", property = PropertyNames.CHECK_PACKAGING, defaultValue = "true")
private boolean checkPackaging;

/**
* Commands to run before the deployment
*
* @deprecated use the {@code execute-commands} goal
*/
@Parameter(alias = "before-deployment")
@Deprecated
private Commands beforeDeployment;

/**
* Executions to run after the deployment
*
* @deprecated use the {@code execute-commands} goal
*/
@Parameter(alias = "after-deployment")
@Deprecated
private Commands afterDeployment;

/**
* Set to {@code true} if you want the deployment to be skipped, otherwise {@code false}.
*/
@Parameter(defaultValue = "false", property = PropertyNames.SKIP)
private boolean skip;

@Inject
private CommandExecutor commandExecutor;

@Override
protected void beforeDeployment(final ModelControllerClient client) throws MojoExecutionException, MojoFailureException, IOException {
super.beforeDeployment(client);
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
return;
}
final Log log = getLog();
final File deploymentFile = file();
final Path deploymentContent = getDeploymentContent();
// The deployment must exist before we do anything
if (!deploymentFile.exists()) {
throw new MojoExecutionException(String.format("The deployment '%s' could not be found.", deploymentFile.getAbsolutePath()));
if (Files.notExists(deploymentContent)) {
throw new MojoExecutionException(String.format("The deployment '%s' could not be found.", deploymentContent.toAbsolutePath()));
}
// Validate the environment
final Path jbossHome = extractIfRequired(deploymentFile.getParentFile().toPath());
if (!Files.isDirectory(jbossHome)) {
throw new MojoExecutionException(String.format("JBOSS_HOME '%s' is not a valid directory.", jbossHome));
final Path wildflyPath = extractIfRequired(deploymentContent.getParent());
if (!Files.isDirectory(wildflyPath)) {
throw new MojoExecutionException(String.format("JBOSS_HOME '%s' is not a valid directory.", wildflyPath));
}
final StandaloneCommandBuilder commandBuilder = createCommandBuilder();
final StandaloneCommandBuilder commandBuilder = createCommandBuilder(wildflyPath);

// Print some server information
log.info(String.format("JAVA_HOME=%s", commandBuilder.getJavaHome()));
Expand All @@ -211,38 +289,46 @@ protected void beforeDeployment(final ModelControllerClient client) throws MojoE
}
// Start the server
log.info("Server is starting up. Press CTRL + C to stop the server.");
process = startContainer(commandBuilder);
} catch (Exception e) {
throw new MojoExecutionException("The server failed to start", e);
}
}

@Override
protected void afterDeployment(final ModelControllerClient client) throws MojoExecutionException, MojoFailureException, IOException {
super.afterDeployment(client);
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(createCommandBuilder());
} else {
keepRunning = false;
Process process = startContainer(commandBuilder);
try (ModelControllerClient client = createClient()) {
if (beforeDeployment != null) {
commandExecutor.execute(client, wildflyPath, beforeDeployment);
}
final Deployment deployment = Deployment.of(deploymentContent)
.setName(name)
.setRuntimeName(runtimeName);
final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
deploymentManager.forceDeploy(deployment);
if (afterDeployment != null) {
commandExecutor.execute(client, wildflyPath, afterDeployment);
}
}
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);
} finally {
if (process != null) process.destroy();
}
}

private StandaloneCommandBuilder createCommandBuilder() {
final StandaloneCommandBuilder commandBuilder = StandaloneCommandBuilder.of(jbossHome)
private StandaloneCommandBuilder createCommandBuilder(final Path wildflyPath) {
final StandaloneCommandBuilder commandBuilder = StandaloneCommandBuilder.of(wildflyPath)
.setJavaHome(javaHome)
.addModuleDirs(modulesPath.getModulePaths());

Expand Down Expand Up @@ -311,4 +397,15 @@ private Process startContainer(final CommandBuilder commandBuilder) throws IOExc
}
return process;
}

private Path getDeploymentContent() {
final PackageType packageType = PackageType.resolve(project);
final String filename;
if (this.filename == null) {
filename = String.format("%s.%s", project.getBuild().getFinalName(), packageType.getFileExtension());
} else {
filename = this.filename;
}
return targetDir.toPath().resolve(filename);
}
}