2

I'm trying to migrate a Spring/Maven application to Spring Boot. It uses a maven plugin to generate some JavaScript (in my case handlebars-maven-plugin).

This plugin essentially produces generated JavaScript code that should be included in the deployment artifact. However, this "generated code" is not itself part of the source code. In the old style Maven web application this content was created in target directory as part of the web application folder.

I can imagine other similar situations where some part of the source code is processed as part of the build without the resultant files of this process being part of the source code itself (minification, aggregation, obfuscation, SASS, Less etc.).

I've read that in Spring Boot static content should be stored in somewhere in the classpath under /META-INF/resources/, /resources/, /static/, /public/. But where should generated content be output?

I can change the output directory of the plugin to "/target/classes/static" but when running mvn spring-boot:run I cannot locate the generated content. I presume the "mvn spring-boot:run" is reading the source code from "src" rather than "target".

How should generated JavaScript/CSS be dealt with in Spring Boot?

Mark McLaren
  • 11,470
  • 2
  • 48
  • 79
  • 1
    Are you sure that plain file, for example src/main/resources/static/hello.js is visible when started by `spring-boot:run`? – michaldo Mar 30 '17 at 12:23
  • Thanks @michaldo that helped. I think I've worked it out now. I will post my solution later. – Mark McLaren Mar 30 '17 at 13:03

1 Answers1

2

I am certain that my generated JavaScript/CSS files should not be written to the src directories. Generated content belongs only in the target directory - even if that generated code is included in the distribution artifact (war/jar file).

My confusion was due to two issues.

"mvn spring-boot:run" behaviour

My application builds a war file as I have a current requirement to install it on a remote Tomcat server. The behaviour of "mvn spring-boot:run" is different for jar and war packaging. From what I have read, if you have a war package "mvn spring-boot:run" will attempt to serve web content from the src/main/webapp directory. My generated content does not belong in the src directory - so somehow I need to get it served from the target directory.

Confusion about how Spring Boot serves up static content

I read that Spring Boot automatically serves up content from several classpath locations; /META-INF/resources/,/resources/,/static/,/public/.

Even when I updated my generated JavaScript plugin output path to target a classpath location I initially couldn't locate it.

I ascertained that WebMvcAutoConfiguration contains the code responsible for adding the static content paths and that WebMvcAutoConfiguration is triggered if your configuration extends WebMvcConfigurerAdapter.

However, I've seen comments to suggest that if you use @EnableWebMvc then this effectively disables theses default static content locations (I would value a authoritative reference for this - I found this now see Update below).

I used @EnableWebMvc so to get around this problem I extended WebMvcConfigurerAdapter to explicitly define the static locations to include the classpath folder where I have output my generated content.

I dare say that as I now extend WebMvcConfigurerAdapter that I probably don't need to use @EnableWebMvc anymore but I'm not yet 100% certain about this.

Now I have explicitly configured the generated content to be located (by overriding addResourceHandlers method of WebMvcConfigurerAdapter) I am now able to locate this content when using "mvn spring-boot:run".

Problem solved - if not completely understood!


Update

I found the section of the Spring Boot documentation that relates to why you disable the default static content locations when you use @EnableWebMvc, it says:

The easiest way to take complete control over MVC configuration is to provide your own @Configuration with the @EnableWebMvc annotation. This will leave all MVC configuration in your hands.

It is from the section entitled "Switch off the Default MVC configuration".

Community
  • 1
  • 1
Mark McLaren
  • 11,470
  • 2
  • 48
  • 79