Search code examples
javamavenjarspring-bootstatic-resource

Spring Boot shared web modules (jar file overlay?)


Before for web development with .war output, there was option for modularization war overlays http://maven.apache.org/plugins/maven-war-plugin/overlays.html

Now with Spring Boot adopting jar packaging, how can make some shared web resources?

That is there is dependency on com.company.shared.web module, with ,say, image /com.company.shared.web/src/main/resources/static/images/background-image.png
How this image can be made to be available for running Spring boot app within main module? Maven standard dependency resolution will make only Java classes available.

P.S. Similar question, but for .war packaging was Spring Boot & Maven war overlay and not resolved https://github.com/spring-projects/spring-boot/issues/1030


Solution

  • Maven Dependency Plugin

    You can use this plugin to unpack jar dependencies and copy the files to another directory.

    Full Sample Project

    In this sample project you've got a Parent POM followed by two Maven Modules of Jar packaging.

    • Web-App - Spring Boot App, Executable Jar, Uses Web-Library as Depdenency & the Maven Depedency Plugin to extract static files from Web-Library and copy them to its own static directory
    • Web-Library - A Library Jar containing static assets such as css & javascript files that are to be shared with other modules such as Web-App

    Instructions:

    mvn clean package
    java -jar web-app/target/web-app-0.0.1-SNAPSHOT.jar
    

    Browse to localhost:8080 and notice that style.css & script.js are loaded which come from the Web-Library module.