Sunday, May 28, 2017

JavaEE: Reloading with TomEE Maven Plugin

On a previous post, we learned how to run a simple JaveEE code using a TomEE Maven Plugin.  As a developer, we often change codes and want to see the change immediately.  The TomEE maven plugin provides a <reloadOnUpdate> and a <synchronization> configuration.

Synchronization is a block defining a source and target folder, which will be synchronized. Configuration of the plugin shown on a previous post needs to be updated.

In addition to the <reloadOnUpdate> and <synchronization> configuration, the plugin need to use some internal beans called a openejb for the reloading.  This is an updated <plugin> element in the pom.xml.

<plugin>
   <groupId>org.apache.tomee.maven</groupId>
   <artifactId>tomee-maven-plugin</artifactId>
   <version>${tomee.version}</version>
   <configuration>
      <tomeeVersion>${tomee.version}</tomeeVersion>
      <tomeeClassifier>plus</tomeeClassifier>
            
      <webapps>
         <webapp>${project.groupId}:${project.artifactId}:${project.version}?name=${project.artifactId}</webapp> 
      </webapps>
                    
      <synchronization>
         <extensions>
            <extension>.class</extension>
         </extensions>
      </synchronization>
      <reloadOnUpdate>true</reloadOnUpdate>
      <systemVariables>
         <tomee.serialization.class.blacklist>-</tomee.serialization.class.blacklist>
         <!-- When TomEE starts it starts an internal application (called openejb). 
             The goal is to offer some internal beans mainly used by TomEE tooling 
             (TomEE deployer, reload feature of TomEE maven plugin…).
             Don't use it if it is not necessary
         -->
         <openejb.system.apps>true</openejb.system.apps>
      </systemVariables>
   </configuration>
</plugin>

Now, restart the tomee plugin.
mvn clean install tomee:run

Then, open a URL again as shown on a previous posthttp://localhost:8080/demo/hello

Now, update the String in the sayHello method shown on a previous post.  Then, recompile the changes by running this:  mvn compile
On a command console, you will see that TomEE plugin restarts, but the change is not visible on http://localhost:8080/demo/hello

In fact, you can see the update on this url: http://localhost:8080/demo-0.1-SNAPSHOT/hello

So, the added <webapp> element to change the context name didn't work during the reloading.  To avoid this issue, I will use a <finalName> element provided by the maven instead of using a tomee plugin configuration.

<build>
   <finalName>${project.artifactId}</finalName>
   <plugins>
      <plugin>
         <groupId>org.apache.tomee.maven</groupId>
         <artifactId>tomee-maven-plugin</artifactId>
         <version>${tomee.version}</version>
         <configuration>
            <tomeeVersion>${tomee.version}</tomeeVersion>
            <tomeeClassifier>plus</tomeeClassifier>
            <!-- Removed   
            <webapps>
 <webapp>${project.groupId}:${project.artifactId}:${project.version}?name=${project.artifactId}</webapp> 
             </webapps>
             -->
       
             <synchronization>
                  :
                  :
          </configuration>
       </plugin>
   </plugins>
</build>

Now, the changed code will be auto reloaded when the codes are compiled, and be visible on a url http://localhost:8080/demo/hello

If you use an IDE that can build a project automatically, the changed code will be reloaded even without running the mvn compile  It can be convenient, but frequent reloading may annoy developers too.

No comments:

Post a Comment

Java 9: Flow - Reactive Programming

Programming world has always been changed fast enough and many programming / design paradigms have been introduced such as object oriented p...