= WildFly Plugins Core API

The WildFly Plugins Core API offers some simple API's for deploying applications to http://wildfly.org[WildFly] or https://www.redhat.com/en/technologies/jboss-middleware/application-platform[JBoss EAP].

== Deployment Manager

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.
[source,java]
----
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.
[source,java]
----
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();
}
----

[source,java]
.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();
}
----

[source,java]
.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();
    }
}
----

[source,java]
.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());
    }
}
----

== Deployment Operation Helper

There is a helper if you'd rather execute operations on your own as well using the `org.wildfly.plugin.core.DeploymentOperations`.

[source,java]
----
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());
    }
}
----

== Server Helper

You can also use the `org.wildfly.plugin.core.ServerHelper` to interact with a running server.

[source,java]
----
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();
}
----