How to create a shared library

The shared library feature in WebLogic Server provides an easy way to share one or more different types of JEE modules among multiple Enterprise Applications. You have two main options for sharing a library across your domain applications: you can either drop the library into the DOMAIN_HOME/lib or deploy the application itself as a library.

Deploy the library into the DOMAIN_HOME/lib

The domain library directory (lib) is one mechanism that can be used for adding application libraries to the server classpath. The jars located in this directory will be picked up and added dynamically to the end of the server classpath at server startup. The jars will be ordered lexically in the classpath.

 

It is possible to override the DOMAIN_HOME/lib directory by using the -Dweblogic.ext.dirs system property during startup. This property specifies a list of directories to pick up jars from and dynamically append to the end of the server classpath using java.io.File.pathSeparator as the delimiter between path entries.

Beware that, once you put your libraries in the DOMAIN_HOME/lib directory, you have to restart the server when the jar changes.

Deploying the application as a shared library

The deployment process is discussed in detail in Chapter 8 of this book. Anyway, deploying a new application as shared library requires following a very simple procedure:

  1. Open the Admin Console and click on Deployments. Once in the Deployment Summary screen, click on the Install button.
  2. Browse to your deployments and, once selected, you can opt to install it as an application or as library. Choose: “Install the deployment as a library

Once completed the deployment, you will have a deployment view of your library which resembles the following example:

Referencing the shared library in your applications

Applications can reference shared libraries by adding a library-ref element into the WebLogic custom deployment descriptors. For example, the following entry in the weblogic.xml references the above shared library named SharedLib:

<wls:library-ref>

   <wls:library-name>SharedLib</wls:library-name>

</wls:library-ref>

The main difference from barely adding files to the classpath is that a shared library can actually be a Java EE application. Working with a shared library gives you also more flexibility since you are able to deploy multiple versions of the library and use deployment descriptors to refer to the right version. You can include library specifications in the META-INF/MANIFEST.MF file:

Manifest-Version: 1.0

Specification-Version: 1.0

Implementation-Version: 1.1

Here’s the deployment outcome on the WLS console, when deploying the Shared Library application including the above library specifications:

And here’s how to reference an exact version in your weblogic.xml file:

<wls:library-ref>

   <wls:library-name>SharedLib</wls:library-name>

   <wls:specification-version>1.0</wls:specification-version>

   <wls:implementation-version>1.1</wls:implementation-version>

   <wls:exact-match>true</wls:exact-match>

</wls:library-ref>

Francesco Google+