Over a million developers have joined DZone.

Hello World With Wildfly Swarm

With the release of Wildfly Swarm, Java EE apps can be deployed alongside a stripped down version of Wildfly. Swarm is still in beta at the moment, and the documentation is missing a few steps that are required to get a "Hello World" application up and running, so let’s dive in and see how we can build a Java EE Microservice JAR file.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Microservices—you can’t be an enterprise developer these days without at least considering Microservices as a deployment strategy.

Unfortunately for Java developers, Microservices represent almost the opposite of the way we have been building and deploying apps. Web and app servers have been designed to host multiple applications, while Microservices tends to do away with this middle layer, deploying a single, self-contained application directly on the OS or container.

Spring Boot has found popularity embracing this idea of Microservices by providing a convenient way to build web applications as a self-contained JAR file which includes Tomcat or Jetty right alongside your application code. All the while, Java EE developers were left on the sideline.

With the release of Wildfly Swarm, Java EE devs are no longer second class citizens. Swarm provides a way for Java EE apps to be deployed alongside a stripped down version of Wildfly. Swarm is still in beta at the moment, and the documentation is missing a few steps that are required to get a "Hello World" application up and running, so let’s dive in and see how we can build a Java EE Microservice JAR file.

It all starts with the build file.

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        classpath "org.wildfly.swarm:wildfly-swarm-plugin:1.0.0.Beta2"
    }
}

group 'com.matthewcasperson'
version '1.0-SNAPSHOT'

apply plugin: 'war'
apply plugin: 'wildfly-swarm'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    mavenLocal()
    maven {
        url 'http://repository.jboss.org/nexus/content/groups/public-jboss'
    }
    maven {
        url 'https://maven.repository.redhat.com/nexus/content/repositories/public'
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'org.wildfly.swarm:bootstrap:1.0.0.Beta2'
    compile 'org.wildfly.swarm:jsf:1.0.0.Beta2'
    compile 'org.wildfly.swarm:weld:1.0.0.Beta2'
}

The documentation does mention most of what you’ll find in this Gradle build file. However, I found that I needed to reference a few additional Maven repos in order to satisfy all the dependencies.

Because this is a Java EE application, we’ll demo a JSF web application. Again the documentation shows you almost everything you need to do here, except for the fact that the JSF dependency also requires the Weld (CDI) dependency. Without CDI, JSF will throw an NPE.

I also found that I needed to reference the Bootstrap dependency for the build to complete.

Believe it or not, these entries in the build file are all we need to take an otherwise stock standard JSF “Hello World” application built as a self-contained uberjar. Run the command:

./gradlew wildfly-swarm-package

Be prepared to download the entire internet’s collection of Java dependencies.

This command will generate an uberjar that can be run with the command:

java -jar build/libs/swarmdemo1-swarm.jar

Once Wildfly has started up, open up http://localhost:8080 and you’ll see the contents of the index.xhtml file.

Grab the source code for this article from GitHub.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
wildfly swarm ,java

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}