Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot with Gradle and Webpack

I have an application written with Spring Boot and JS on client. All JS & CSS code is stored in src/main/webapp and compiled with Webpack to the same directory. It seems dirty to put compiled bundle there, but I wonder if there's a way to store it in build directory?

I've managed to add it to war file using the next code in build.gradle

from("${buildDir}") {
    include 'bundle.js'
}

But I have no sense how to achieve the same effect for bootRun task or in Intellij Idea.

Also, how I can exclude source files stored in src/main/webapp/js and src/main/webapp/scss? And what best practices exists for managing web assets?

like image 766
Kirill Voronin Avatar asked Oct 31 '22 14:10

Kirill Voronin


1 Answers

You should probably take a look at Jhipster (http://jhipster.github.io/)

BTW, for my projects, I clearly prefer to separate client code from server code. A 'project-client' directory for JS code, next to the 'project-server' directory with Java code.

The former one has obviously a "standard Java" files structure.

For the first one, JS has now its own standards. Ok, they're yet not very stable ;), but still. Your JS source code can be half - or more - of your source, its place is probably not in a subdirectory of your Java project...

Moreover, tools like Webpack are very powerful. Webpack can embed styles, images or other resources when it's relevant. The source files / resources files / build product files should be as clearly separated as in your Java project.

When developing, use webpack server. My index.html host page (deserved by the Java server) simply points to localhost:[webpack-port]/main.js

Then there's the question of the global build, which is easily resolved with Gradle. Gradle calls NPM (thanks to https://github.com/srs/gradle-node-plugin) to test and build the JS part, then it builds the Java part. It finally copies the final jar and the javascript files in a final and global build directory. The name of the JS files - which contains a hash, you should keep that powerful feature ! - are given to a conf file which is used by the Java application.

like image 163
mlorber Avatar answered Nov 09 '22 16:11

mlorber