Set a web project to use shared libraries


Overvview

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. Use shared libraries enables multiple applications published on a server to use a single library, rather than use multiple copies of the same library. After associating 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.


Steps

  1. To set a web project to use shared libraries:

    1. In the Project Explorer view

        Web project (right-click) > Properties > Liberty > Shared Libraries

    2. When you click the Add button, a list of shared library IDs appears. In the IDs field, specify one or more shared library identifiers that we want the project to reference. To specify multiple identifiers, use a comma-separated list. For example: ID1, ID2, ID3.

      Tip: The shared library identifier is the value specified in the Shared Libary ID field

  2. We might want to add its associating utility projects to the class path for compilation-purpose:

    1. In the Project Explorer view...

        Project (right-click) > Properties > Java Build Path > Select the Projects > Add

    2. Select the utility projects that the project references.

  3. Optional: Set the API visibility.

    1. Select...

        web project (Right-click) Liberty > Shared Libraries

    2. Select from the following check boxes to set the API visibility: API, IBM API, Spec, and Third party.

      Default visibility is API, IBM API, and Spec.

  4. 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.

  5. Add the web project to the server.

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: Define a utility project as a shared library


Related tasks