In the OTN WLS General forum someone asked a question regarding using Shared Libraries to share static web resources among applications. “Basically, we are trying to load jquery, css files, shared images, and what not into a shared library for access from other Web Applications.” Shared libraries will handle this use case well. I’ve put together a basic example using a simple image that is put into a shared library that is referenced by another web application. The image in the screen shot below is from a shared library. The web application that references it simply contains the JSP and a reference to the shared library in the weblogic.xml deployment descriptor.
Basic Steps
- Assemble the resources (images, css, etc) in a base directory, with subdirectories if desired
- In the main directory, create a META-INF/MANIFEST.MF file describing the library
- Package the base directory as a WAR file
- Deploy the WAR as a library to WLS
- Refer to the Shared Library in a web application’s WEB-INF/weblogic.xml
- Refer to the static resources from the library in the web application as if they are local to your application
Here is the link to the full documentation that describes all of the options for Shared Libraries in WebLogic.
Simple Example
I recommend taking a look at the shared libraries that ship with WebLogic Server to use as an example. In WLS 10.3.x they are located here: <MIDDLEWARE_HOME>\wlserver_10.3\common\deployable-libraries
Here is what my META-INF/MANIFEST.MF file looks like for my example, I simply copied an existing MANFIEST.MF from jsf-1.2.war and made edits:
Manifest-Version: 1.0
Specification-Title: Images
Specification-Version: 1.0
Implementation-Title: Images Implementation
Implementation-Version: 1.0
Implementation-Vendor: Oracle
Extension-Name: images
Here is what the packaging of the library looks like after zipping it up as a WAR file:
C:\temp\images>jar -tf images.war
META-INF/
META-INF/MANIFEST.MF
oralogo_small.gif
As you can see, it’s only a simple WAR file with an oracle image in the base directory with a META-INF/MANIFEST.MF file describing the library. To deploy this to the WLS, you can do that as normal other than noting that it is a library. After doing that it will show up like this in the console:
If you’re using Oracle Enterprise Pack for Eclipse and set your Windows->Preferences->Server->Runtime Environments correctly, then the existing shared libraries should be available in Preferences->WebLogic->Shared Libraries and you can add the library you just created to the list. This will allow you to edit the WEB-INF/weblogic.xml in the IDE with a dialog box, correctly specify the reference to the shared library, and deploy the shared library for you to the server if it has not been done already when you deploy the application.
weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
<wls:weblogic-version>10.3.2</wls:weblogic-version>
<wls:context-root>ImagesWeb</wls:context-root>
<wls:library-ref>
<wls:library-name>images</wls:library-name>
<wls:specification-version>1.0</wls:specification-version>
<wls:exact-match>true</wls:exact-match>
</wls:library-ref>
</wls:weblogic-web-app>
Now in the index.jsp, I can simply refer to the image as if it were in my base directory of the application like this:
<img src="oralogo_small.gif"/>
Conclusion
Shared libraries are usually used to share code such as jars and class files in WARs and EARs, but they can also be used to share static resources. So let’s say I wanted to share a jar file, I would have a WEB-INF/lib directory in my shared library, and put any jars that I wanted my application’s to have in there. If it is an EAR file, then I would use APP-INF/lib.
You can download this extremely simple example here. I tested this with WLS 10.3.2 also known as 11g PS1.