Issue
I'm aware of similar questions here and here. And yet IMHO the issue is not yet fully covered, or at least it's still not working smoothly for me :(
What is the need:
I'd like to build a JAR packaged library, that will be used like a shared component in several WebApps, bringing within it the server logic as well as the relevant UI (web-components). Think of a UserService
providing servlet/resource for login
and signup
calls from the server side perspective, while having UI components for end-to-end solution for any WebApp in the whole eco system from the client side.
So far:
Seems like this requirement exactly is addressed by a feature in Servlet 3.0 spec dealing with servlet web resources found in JARs under the path META-INF/resources
.
I, seemingly, did exactly as it said, structuring the ui-components.jar
having META-INF/resources/ui-commons/...
under the root and configuring the following in the web.xml
:
<servlet>
<servlet-name>ui-commons</servlet-name>
<servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
<init-param>
<param-name>relativeResourceBase</param-name>
<param-value>/WEB-INF/lib/ui-components.jar!META-NF/resources</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ui-commons</servlet-name>
<url-pattern>/ui-commons/*</url-pattern>
</servlet-mapping>
This definition comes before the default servlet definition serving the rest of the regular WebApp resources found in WAR as usual.
And yet, any request for the resources in that JAR ends up with 404.
Servlet version is 3.1
.
Jetty 9.4
.
It is NOT embedded Jetty, nothing fancy, but please pay attention to the notice below regarding the maven plugin.
Notice:
It might somehow be related to the fact that I'm currently trying to run this whole setup with jetty-maven-plugin
, which is serving resources from the sources.
- I've tried to drop the said JAR into the
src/main/resources/WEB-INF/lib...
manually - no success
Solution
Okay, I've missed something and Joakim's answer pointed me to the right direction to find it.
In order to get resources from JARs to be served by Jetty, they first and foremost should be scanned by Jetty server.
By default, all the JARs in webapp's WEB-INF/lib
are scanned. Yet, it is advised to fine tune this behavior and filter out all of the non relevant JARs to speed up the start time as per Jetty's documentation here.
So, first - be sure to review your WebInfIncludeJarPattern
attribute configuration in WebAppContext
setup and include the JAR that contains an additional static resources.
The rest of the setup for a 'foreign' JAR's resources (again, as Joakim pointed out) is identical to the regular webapp's resources, no need for any special path into the JAR as I've wrongly tried to do in my initial setup.
Answered By - GullerYA