admin管理员组

文章数量:1431788

I have an application written with Spring Boot and JS on client. All JS & CSS code is stored in src/main/webapp and piled with Webpack to the same directory. It seems dirty to put piled 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?

I have an application written with Spring Boot and JS on client. All JS & CSS code is stored in src/main/webapp and piled with Webpack to the same directory. It seems dirty to put piled 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?

Share Improve this question edited Dec 5, 2015 at 14:17 Tomasz Jakub Rup 10.7k7 gold badges51 silver badges49 bronze badges asked Dec 5, 2015 at 14:08 Kirill VoroninKirill Voronin 1662 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

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./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.

I had struggled a lot to find a solution to exclude node_modules in my webapp. I added following block in my build.gradle

sourceSets {
main {
    resources {
        exclude '**/node_modules'
    }

   }
}

Hope it helps.

本文标签: javascriptSpring Boot with Gradle and WebpackStack Overflow