javamavenstatic-resource

Static web resources in a Maven Jar?


Is it possible to package static resources (like Css, Js or images) in a Jar for use in a webapplication (war)?

I would like to seperate parts of the app into modules, but these don't only depend on Java classes, like controllers or models, but on views, templates and media. So I want to package all of that in a Maven module project and copy classes to the classpath and static files into any directory under the root of the application or WEB-INF.


Solution

  • Servlet 3.0 specification has come up with a specification JSR 315, in which you can specify web-fragments.xml and put your files(Even js, css files) under META-INF/resources folder. These files will be compiled into jar. Keep this jar in WEB-INF/lib folder. While deploying your app, container will deploy META-INF/resources files into respected location. Here is how structure should look like

    web-fragment.xml example

     <web-fragment metadata-complete="true" version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        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-fragment_3_0.xsd">
      <name>part1</name>
    
    </web-fragment>
    

    more about web fragments here : Shing Wai Chan's Oracle blog