Configuring shared libs in an EAR file with WildFly

In this article we will  learn what is the correct placement of shared libraries with Enterprise Application running in an EAR file. In the second part of this tutorial we will learn a simple way to change the default lib directory in an ear file.

Shared Modules in an EAR file

A typical three-layers Enterprise application includes a Front-end Layer, a Persistence Layer and some common Classes referenced by both layers.

The recommended practice is to place the shared Classes in an EAR archive. This allows the EJB and Web Classloader to resolve the common utility Classes referenced. However you have to instruct WildFly about these Classes. You have basically three options for referencing utility Classes:

Option 1: Place the libraries in the “lib” folder
jboss shared libs common
By placing libraries or configuration files in the “lib” folder (at the root of your EAR), you will be able to resolve your classes without the need to add any configuration.
Though this is the fastest solution, it might not be supported in all application servers. For example Bea Weblogic uses the APP-INF/lib to load common libraries

Option 2: Add a jboss-app.xml configuration file
jboss common shared libs
If you want even more customization for your application, you can use a JBoss/WildFly specific deployment descriptor, named jboss-app.xml which is to be placed in the META-INF folder of your EAR.
For example:

<application>
  <display-name>My Application</display-name>
  <module>
    <web>
      <web-uri>myapp.war</web-uri>
      <context-root>/myapp</context-root>
    </web>
  </module>

  <module>
    <ejb>myapp.jar</ejb>
  </module>
  
  <library-directory>APP-INF/lib</library-directory>

</application>

The advantage of this solution is that you can choose any location for your libraries. The cons is that it is not portable across another application server.

Packaging Java EE Client Application Modules

Java EE client application modules are Java archive (JAR) files that contain client components and resources, such as Java classes and interfaces, that you can use to access enterprise beans in a Java EE application. Client application modules can simplify accessing enterprise beans from standalone Java applications or applets.

You can include Client application modules as part of an enterprise archive (EAR) file, along with other components such as enterprise beans and web components. They are declared in the application.xml deployment descriptor of the EAR file.

Here is an example of how you can declare a client application module in an application.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<application>
   <module>
      <web>
         <web-uri>TestWeb.war</web-uri>
         <context-root>testWeb</context-root>
      </web>
   </module>
   <module>
      <ejb>TestEJB.jar</ejb>
   </module>
   <module>
      <java>Utility.jar</java>
   </module>
</application>

Please keep in mind that the <java> tag does not instruct the container to run the module as server-side modules. This is not its purpose, which is to declare client-side code that runs outside of WildFly’s JVM.

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