4

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

Community
  • 1
  • 1
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • If the resources are packaged in the JAR, then they are also available if the JAR is a dependency of the Spring Boot application. – dunni Jun 16 '16 at 15:27

1 Answers1

6

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.

Kyle Anderson
  • 6,801
  • 1
  • 29
  • 41
  • 1
    Perfect answer. I wonder will it work during development? https://github.com/kanderson450/stackoverflow-q37862552/issues/1 – Paul Verest Jun 22 '16 at 14:43
  • @PaulVerest If you are referring to a Live Reload development workflow then you'll likely need to leverage another tool such as Gulp to watch for changes in Web-Library (css, javascript, etc.) and then copy them to Web-App/target/classes/ See this for more detail: https://www.npmjs.com/package/gulp-watch – Kyle Anderson Jun 22 '16 at 15:30