How to live reload applications on WildFly

In this article we will discuss how to use WildFly Maven plugin to automate the redeployment of applications on WildFly when you apply some changes to the application class files.

What is WildFly Maven plugin

WildFly Maven Plugin is a plugin for Apache Maven that provides a set of goals to manage and deploy applications to a WildFly application server. It enables developers to automate the deployment process and reduce the time required for deployment tasks.

Besides, it allows to perform management task on the applications such as executing CLI commands.

The following article discuss more in detail the usage of this plugin: How to configure and run Maven WildFly plugin

In this article we will use the latest version of it which includes the dev goal to allow automatic redeployment of applications upon changes to the source code.

Configuring the Plugin

To configure the plugin, include the latest version of it in your pom.xml file:

  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <plugin>
        <groupId>org.wildfly.plugins</groupId>
        <artifactId>wildfly-maven-plugin</artifactId>
        <version>4.1.0.Beta3</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>

Additionally, we are configuring the maven-war-plugin to disable failure in case the web.xml file is not available in the application.

Include a simple Servlet in your Web application with some output to check the reload:

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();
		writer.println("<h1>Hello World Servlet on WildFly</h1>");
		writer.close();
}

Then start WildFly using the wildfly:dev goal:

mvn install wildfly:dev

WildFly Server will start. Next, try to access the application:

Next, let’s modify the Servlet output:

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();
		writer.println("<h1>Live reload on WildFly</h1>");
		writer.close();
}

Save the Class file and access again the Servlet:

As you can see, the application was automatically re-deployed behind the hoods!

Using the dev goal against a running server

In the current version of the plugin, a WIldFly server is downloaded when you start the dev goal. However, by the time this JIRA is done https://issues.redhat.com/browse/WFMP-196 it will be possible to use the dev goal against a running WildFly Server

Many thanks to James Perkins (JBoss Team) for upgrading this plugin and for help in writing this article.

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