javascript and java in harmony

Alli Grant Est. 3 minutes (439 words)
Part three of the series on Maven, Java, and Javascript together.

Part three of the series on Maven, Java, and Javascript together, and I’m happy to say I’ve got good news.

There’s been more updates to the frontend plugin, so as of version 1.3 you can most certainly do everything that I originally set out.

  1. Using node + webpack as part of a Maven build process instead of hideous combinations of wro and others
  2. Changes to JS and LESS files automatically recompile
  3. The compiled changes are picked up immediately by Spring for use
  4. Changing one file shouldn’t make everything a source of neverending delay and pain

The answer here had a few components, not all of which I’m sure people will agree with, but here’s the breakdown.

https://github.com/eirslett/frontend-maven-plugin is a wonderful Maven plugin that takes care of point number one. It’s still under fairly active development, but as of 1.3, it integrates pretty easily into a Spring Boot application.

For point number 2 and 3, we need to be a bit more clever than I had originally been with the webpack config. Using src/main/js and src/main/less as source directories plays nicely with maven and can be easily pulled into your IDE with a basic configuration of the maven resources plugin. In the webpack configuration, set those up to be the base input directory. If the base output directory happens to be target\classes\META-INF\assets\, Spring Boot can pick up everything on the classpath and serve it under assets automagically. Yes, I should have thought of this sooner.

I actually got that part working a few weeks ago, but then speed started to become a concern–a full build was still in the realm of 5 minutes, but any incremental Javascript changes would require a 20-30 second delay. Same with any stylesheet changes. Point 4’s (easy, almost no time required) solution is to split your Javascript and LESS/CSS out from your general maven frontend pom into dedicated poms for styling and code. I’m almost certain that there’s some simple way to get a webpack dev server playing nicely with all of this instead, but I’ve not had a chance to do so yet–some simple glancing seems to show that you can pretty easily get a maven build that never completes.

You can keep the benefits of frontend-maven-plugin by using a shared node folder between all your various frontend modules, so, it all maintains a single version of node that is shared and specific to your project. I’m going to keep playing with this, but the current setup I have is a far friendlier for getting things done (and spinning up new developers!) than anything else I’ve tried. In short?

frontend-maven-plugin + spring boot: love