Search code examples
mavenshared-librarieswaroverlays

Maven Overlay vs ClassLoader Magic?


I'm reading up on Maven WAR Overlays and am trying to wrap my head around their use case.

According to this article, they are useful for when you have multiple WARs that need to share the same resources (CSS, JS, HTML, etc.).

However, why couldn't I just put all reusable code/resources into a shared JAR, and then at runtime, access CS/JS/HTML/etc. resources via typical ClassLoader#getResourceAsStream() magic?

Am I missing something here? Do Maven Overlays actually solve a unique problem here (if so, what is it?!?).


Solution

  • Overlays are not only about re-use. It gives you a neat way to yield a multitude of artifacts, all starting from a common code-base (war), but than tweaking either its configuration, implementation or static resources.

    To illustrate a case, for most of the projects we have Selenium tests running on CI. They all run against an overlay of our original WARs, where, for the purpose of the tests, we swapped all class implementation communicating with third party system (e.g. spring integration gateways), usually with custom web.xml adding some specifics related to running tests on the Jetty server, often tweaking static files (js, css) for various purposes (reproducing bugs, testing dynamic layouts)

    Prior to using overlays, we were heavily leaning on maven profiles, configuring webResources of the maven-war-plugin, using Spring profiles, and still failing short on some of our needs, and was much harder to maintain.