Introduction to Thorntail

IMPORTANT: The Thorntail project (formerly known as WildFly Swarm) has reached End Of Life. The last Thorntail release is the 2.7.0. You are recommended to evaluate a migration plan for your applications. Check this tutorial to learn more: How to migrate Thorntail applications

Thorntail is the new name for WildFly Swarm, which can be used to create micro-services-like Java Enterprise Applications. In this tutorial we will learn how to create a simple MicroService application using Thorntail and how to migrate from WildFly Swarm.

First of all, some words about the renaming of this project. According to Bob McWhirter (senior engineer @Red Hat) the reason is that the term “Swarm” was fairly overloaded and the project itself reclaimed its own identity. Also the wording WildFly has been removed from the project name to indicate that the project won’t be merely an Appendix of WildFly but it is going to grown on its own.

That being said, how do we get started at creating applications with that Thorntail? We can start from the new Project Inizializer which is available at:  https://thorntail.io/generator/

As you can see from the following picture, it’s pretty much the same as the WildFly Swarm generator. You have to provide the project coordinates and the libraries you are to link. Then click on Generate Project:

thorntail microservices java ee tutorial

The main difference is in the pom.xml file which now contains references to thorntail BOM, as you can see:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <name>Thorntail Example</name>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
    <version.thorntail>2.2.1.Final</version.thorntail>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.thorntail</groupId>
        <artifactId>bom-all</artifactId>
        <version>${version.thorntail}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>demo</finalName>
    <plugins>
      <plugin>
        <groupId>io.thorntail</groupId>
        <artifactId>thorntail-maven-plugin</artifactId>
        <version>${version.thorntail}</version>
        
        <executions>
          <execution>
            <goals>
              <goal>package</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>jaxrs</artifactId>
    </dependency>
  </dependencies>
</project>

 The project has generated for you a minimal HelloWorld REST Service:

package com.example.demo.rest;


import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;


@Path("/hello")
public class HelloWorldEndpoint {

        @GET
        @Produces("text/plain")
        public Response doGet() {
                return Response.ok("Hello from Thorntail!").build();
        }
}

 You can run the demo application with:

$ mvn thorntail:run

 Migrating from WildFly Swarm to ThornTail

Migrating from the older WildFly Swarm is quite easy. As a matter of fact, you can just replace the older WildFly Swarm artifacts:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>bom</artifactId>
            <version>${version.wildfly.swarm}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

 With Thorntail BOM:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.thorntail</groupId>
        <artifactId>bom-all</artifactId>
        <version>${version.thorntail}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

Also, the individual dependencies have been renamed. So, for example:

 <dependency>
         <groupId>org.wildfly.swarm</groupId>
         <artifactId>jaxrs</artifactId>
</dependency>

Must be changed to:

    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>jaxrs</artifactId>
    </dependency>

If you don’t want to change manually the pom.xml file then you can use the Thorntail Maven plugin which is able to turn your older WildFly Swarm into a Thorntail project:

mvn io.thorntail:thorntail-maven-plugin:2.2.1.Final:migrate-from-wildfly-swarm
    

This will take care of changing all groupId and artifactId references from WildFly Swarm to Thorntail along with corresponding version numbers.

Found the article helpful? if so please follow us on Socials