Set a web project to use shared libraries
If we have a utility project defined as a shared library, we can associate defined shared libraries with a web project.
A shared library is an external Java archive (JAR) file used by one or more applications. Using shared libraries enables multiple application published on a server to use a single library, rather than use multiple copies of the same library. After you associate shared libraries with an application or project, the application or module class loader loads classes in the shared libraries and make those classes available to the application or module.
- To set a web project to use shared libraries:
- In the Project Explorer view, right-click the web project to associate shared libraries.
- Select Properties > Liberty Profile > Shared Libraries.
- When we click the Add button, a list of shared library IDs will appear. In the IDs field, specify one or more shared library identifiers we want the project to reference. To specify multiple identifiers, use a comma-separated list. For example: ID1, ID2, ID3
The shared library identifier is the value specified in the Shared Libary ID field from the Defining a utility project as a shared library task.
- We might want to add its associating utility projects to the class path for compilation-purpose:
- In the Project Explorer view, right-click the project that we are associating shared libraries.
- Select Properties > Java Build Path.
- Select the Projects tab.
- Click Add.
- Select the utility projects that the project references.
- Develop the artifacts in the web project. For example, we can add a servlet in a web project that references classes in the shared libraries.
- Add the web project to the server. For more details see Add and run an application on the Liberty profile using developer tools topic.
Results
Here is an example entry added to the server configuration (server.xml) file:<application type="war" id="web" name="web" location="web.war"> <classloader commonLibraryRef="libid"/> </application>
Parent topic: Defining a utility project as a shared libraryTasks:
Configure class loaders and libraries for Java EE applications Share a library across multiple Java EE applications Overriding a provided API with an alternative version