The deployment manager can be used to deploy or redeploy content to a running server as well as undeploy content. It works with both standalone servers and managed domains.
A simple example is deploying a WAR from the file system.
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
final Deployment deployment = Deployment.of(deploymentPath);
deploymentManager.forceDeploy(deployment).assertSuccess();
}
You can also deploy an input stream.
final String deploymentName = "example.war";
final WebArchive archive = ShrinkWrap.create(WebArchive.class, deploymentName);
archive.add(EmptyAsset.INSTANCE, "META-INF/beans.xml");
archive.addPackage("org.jboss.example");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
final Deployment deployment = Deployment.of(archive.as(ZipExporter.class).exportAsInputStream(), deploymentName);
deploymentManager.forceDeploy(deployment).assertSuccess();
}
Managed Domain Example
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
final Deployment deployment = Deployment.of(deploymentPath)
.addServerGroups("main-server-group", "other-server-group");
deploymentManager.deploy(deployment).assertSuccess();
}
Redeploy Example
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
if (deploymentManager.hasDeployment(deploymentPath.getFileName().toString())) {
deploymentManager.redeploy(Deployment.of(deploymentPath)).assertSuccess();
}
}
Undeploy Example
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
final DeploymentResult result = deploymentManager.undeploy(UndeployDescription.of("example.war").setFailOnMissing(true));
if (!result.successful()) {
logger.errorf("Failed to undeploy example.war. %s", result.getFailureMessage());
}
}
There is a helper if you’d rather execute operations on your own as well using the org.wildfly.plugin.core.DeploymentOperations
.
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
final Operation op = DeploymentOperations.createDeployOperation(Deployment.of(deploymentPath));
final ModelNode outcome = client.execute(op);
if (!Operations.isSuccessfulOutcome(outcome)) {
throw new DeploymentException(Operations.getFailureDescription(outcome).asString());
}
}
You can also use the org.wildfly.plugin.core.ServerHelper
to interact with a running server.
final Path wildflyHome = Paths.get(System.getProperty("user.home"), "servers", "wildfly-10.0.0.Final");
final Process process = Launcher.of(StandaloneCommandBuilder.of(wildflyHome)).launch();
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
// Wait at the maximum 30 seconds for the server to start
ServerHelper.waitForStandalone(client, 30);
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
final Deployment deployment = Deployment.of(deploymentPath);
deploymentManager.forceDeploy(deployment).assertSuccess();
// Shutdown the standalone server
ServerHelper.shutdownStandalone(client);
} finally {
process.destroy();
}