Shared libraries
Shared libraries are files used by multiple applications. We can use shared libraries and global libraries to reduce the number of duplicate library files on the system. When properly configured, applications and resource adapters may access classes and resources of libraries, but the opposite is not true, libraries have no access to application or resource adapter classes or resources.
Library elements
Liberty libraries have three child elements, folder, file, and fileset, as shown in the following server.xml file example:
<library> <folder dir="..." /> <file name="..." /> <fileset dir="..." includes="*.jar" scanInterval="5s" /> </library>
folder | All resources under each configured folder are loadable |
file | Each configured file should be either a native library or a container for resources (such as a JAR or a ZIP file). All resources within a container are loadable and any other filetype specified will have no effect. |
fileset | Each configured fileset is effectively a collection of files. Each file in the fileset should be a native library or a container for resources (such as a JAR or a ZIP file). All resources within a container are loadable and any other filetype specified will have no effect. |
For example,
<library id="someLibrary"> <!-- Location of XML and .properties files in the file system for easy editing --> <folder dir="${server.config.dir}/editableConfig" /> <!-- Location of some classes and resources in the file system --> <folder dir="${server.config.dir}/extraStuff" /> <!-- A zip file containing some resources --> <file name="${server.config.dir}/lib/someResources.zip" /> <!-- All the jar files in ther servers lib folder --> <fileset dir="${server.config.dir}/lib" includes="*.jar" scanInterval="5s" /> </library> <application location ="webStore.war"> <classloader commonLibraryRef="someLibrary" /> </application>
The configuration snippet in the example, allows all resources under the editableConfig directory to be loaded by the webStore application.
Global libraries
Global libraries can be used by any application. JAR files are placed in a global library directory, and then are specified in the class loader configuration for each application.
We can place global libraries in two locations:
- ${shared.config.dir}/lib/global
- ${server.config.dir}/lib/global
If there are files present in these locations at the time an application is started, and that application does not have a <classloader> element configured, the application uses these libraries. If a class loader configuration is present, these libraries are not used unless the global library is explicitly referenced.
See Providing global libraries for all Java EE applications.
Resource files
Within Liberty libraries, we can have resource files defined in the library element, as shown in the following server.xml file example<library> <folder dir="..." /> <file name="..." /> <fileset dir="..." includes="*.jar" scanInterval="5s" /> <folder dir="${server.config.dir}/mylibs" /> <file name="${server.config.dir}/otherlibs/my.jar" /> </library>
The folder setting in the example, allows all files under the mylibs directory to be available on the classpath. We can use this style of entry to have your .xml and .properties available.
Parent topic: Sharing a library across multiple Java EE applications
Related information