/spring-boot-plus-front-end

A project demonstrating how to insert the static assets from a separate project retrievable via bower into a Spring Boot runnable jar.

spring-boot-plus-front-end

This is a Maven project demonstrating how to insert the static assets from a separate project retrievable via bower into a Spring Boot runnable jar.

Read more about the motivations at http://nblair.github.io/2016/04/21/combining-spring-boot-bower/.

Requirements

  1. Maven

The build process uses node, npm and bower, however it's not required that you have them installed locally; the frontend-maven-plugin will install them in a generated directory under the project for you.

Build

mvn package

The clean goal is configured to delete the bower_components and node_modules folders, but not the node folder.

How it works

  1. maven-dependency-plugin fires first, downloading the Spring Boot jar from our Artifact Repository. It then unpacks the jar to the specified <outputDirectory> (best to nest this under ${project.build.directory}).
  2. frontend-maven-plugin fires next. If it hasn't downloaded node for your system, it will (under ${basedir}/node/). Then it will run npm install (really just to get bower), then it will run bower install. bower.json declares a dependency on our front end project, and now we have a copy sitting in ${project.build.directory} next to the back-end project.
  3. Then, during the prepare-package phase, the maven-resources-plugin has 3 executions:
    1. Copy the bower_components (which was generated by the bower install in step 2) folder from the front-end site into META-INF/resources/bower_components in the outputDirectory of step 1.
    2. Copy the rest of the assets of the front end project site (excluding bower_components) into into META-INF/resources in the outputDirectory of step 1.
    3. Copy any beta specific configuration into the project (root of the classpath).
  4. Now sitting on the filesystem is an exploded Spring Boot jar with our assets from the front end site piled into it. The last plugin to fire is the maven-jar-plugin, with a few bits of custom configuration:
    • classesDirectory is set to be the root of our exploded Spring Boot jar.
    • A manifestFile - the original from the Spring Boot jar - is explicitly declared. If we don't do this, the maven-jar-plugin will create one on it's own that's missing the properties Spring Boot expects.
    • The compress flag MUST be false. Spring Boot jars contain jars (inside the lib directory), Spring Boot won't start properly if these jars get compressed.