Enterprise Java

WildFly Swarm – Deploying Java EE Applications As Standalone Jars

WildFly Swarm provides an easy solution for deploying Java EE applications as a standalone Jar file. This makes it very easy to deploy applications, particularly REST or web services. Swarm is very similar to Spring Boot in that respect, in that it enables web (.War) applications to be developed quickly and then deployed as standalone applications without the need to deploy an application server.

The WildFly Swarm documentation describes itself as:

Swarm offers an innovative approach to packaging and running Java EE applications by packaging them with just enough of the platform to “java -jar” your application.

Deploying an Java EE application as a Swarm application is fairly straightforward:

  1. Add dependencies on the aspects of WildFly Swarm that you are to use to your project’s pom.xml file, for example, JAX-RS or JPA.
  2. Add the WildFly Swarm plugin to your project’s pom.xml file to enable an executable Jar file to be created.
  3. Sit back, relax and run your application as a standalone executable Jar !

To give a demonstration of how easy this is, I’ve created a very simple JAX-RS web service that returns a list of fruits – very similar to the Angular.js factory I created a few years ago.

The code for this sample application is available on GitHub

To create a JAX-RS web service and run as a WlidFly Swarm application, we just need to add the wildfly-swarm-jaxrs dependency. All of the dependencies that can be used with WildFly Swarm are documented in the User’s Guide

<properties>
    <swarm.version>1.0.0.Alpha5</swarm.version>
</properties>

<dependencies>
    ...
    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>wildfly-swarm-jaxrs</artifactId>
        <version>${swarm.version}</version>
    </dependency>
    ...
</dependencies>

Adding the WildFly Swarm plugin to your pom.xml to create an executable Jar file is equally simple.

<plugins>
    ...
    <plugin>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>wildfly-swarm-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>package</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    ...
</plugins>

All you need to do now, is perform a mvn package command to create an executable Jar file which you can start by executing java -jar myJar.jar

When creating and running a Swarm application, it’s important to note that the URL for the application will be different from when running inside a standalone application server.

When the application is deployed under an application server, the context path with usually include the name of the deloyed application, for example:

http://localhost:8080/swarm-rs-1.0-SNAPSHOT/api/fruit

When this is deployed as a Swarm application, the context path is not required (there is only one Java EE application running), so the URL will look something more like:

http://localhost:8080/api/fruit

David Salter

David is a software developer who has been developing software professionally since 1991. He started programming in Java way back with Java 1.0 developing desktop applications and applets. Since 2001 he has been developing enterprise and web applications using both Java standards and open source solutions. David is the author of "NetBeans IDE 8 Cookbook", "Building SOA-Based Composite Applications using NetBeans 6" and "Seam 2.x Web Development”.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button