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();
}
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();
}
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();
}
}
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.tools.server.DomainManager
, org.wildfly.plugin.tools.server.ServerManager
and org.wildfly.plugin.tools.server.StandaloneManager
utilities to interact with a running server or start a server.
Below is an example of starting a server and using the ServerManager
to interact with the server and deploy an
application.
final Path wildflyHome = Paths.get(System.getProperty("user.home"), "servers", "wildfly-33.0.0.Final");
try (StandaloneManager serverManager = ServerManager.start(Configuration.create(StandaloneCommandBuilder.of(wildflyHome)))) {
// Wait at the maximum 30 seconds for the server to start
if (!serverManager.waitFor(30, TimeUnit.SECONDS)) {
throw new RuntimeException("Server did not start within 30 seconds.");
}
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
final DeploymentManager deploymentManager = serverManager.deploymentManager();
final Deployment deployment = Deployment.of(deploymentPath);
deploymentManager.forceDeploy(deployment).assertSuccess();
}
Below is an example of creating a ServerManger
to interact with an already running server.
try (ServerManager serverManager = ServerManager.builder().build().get(60L, TimeUnit.SECONDS)) {
LOGGER.infof("Managing %s", serverManager.containerDescription());
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
final DeploymentManager deploymentManager = serverManager.deploymentManager();
final Deployment deployment = Deployment.of(deploymentPath);
deploymentManager.forceDeploy(deployment).assertSuccess();
}