简体   繁体   English

带Gradle和Webpack的Spring Boot

[英]Spring Boot with Gradle and Webpack

I have an application written with Spring Boot and JS on client. 我在客户端上使用Spring Boot和JS编写了一个应用程序。 All JS & CSS code is stored in src/main/webapp and compiled with Webpack to the same directory. 所有JS和CSS代码都存储在src/main/webapp并使用Webpack编译到同一目录。 It seems dirty to put compiled bundle there, but I wonder if there's a way to store it in build directory? 将编译的bundle放在那里似乎很脏,但我想知道是否有办法将它存储在构建目录中?

I've managed to add it to war file using the next code in build.gradle 我已经设法使用build.gradle的下一个代码将其添加到war文件中

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

But I have no sense how to achieve the same effect for bootRun task or in Intellij Idea. 但我不知道如何为bootRun任务或Intellij Idea实现相同的效果。

Also, how I can exclude source files stored in src/main/webapp/js and src/main/webapp/scss ? 另外,我如何排除存储在src/main/webapp/jssrc/main/webapp/scss源文件? And what best practices exists for managing web assets? 管理Web资产有哪些最佳实践?

You should probably take a look at Jhipster ( http://jhipster.github.io/ ) 你应该看看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. JS代码的“project-client”目录,位于带有Java代码的“project-server”目录旁边。

The former one has obviously a "standard Java" files structure. 前者显然具有“标准Java”文件结构。

For the first one, JS has now its own standards. 对于第一个,JS现在有自己的标准。 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... 您的JS源代码可能是源代码的一半或更多,它的位置可能不在Java项目的子目录中......

Moreover, tools like Webpack are very powerful. 而且,像Webpack这样的工具非常强大。 Webpack can embed styles, images or other resources when it's relevant. Webpack可以在相关时嵌入样式,图像或其他资源。 The source files / resources files / build product files should be as clearly separated as in your Java project. 源文件/资源​​文件/构建产品文件应与Java项目中的文件分开。

When developing, use webpack server. 开发时,请使用webpack服务器。 My index.html host page (deserved by the Java server) simply points to localhost:[webpack-port]/main.js 我的index.html主页(由Java服务器提供)只是指向localhost:[webpack-port] /main.js

Then there's the question of the global build, which is easily resolved with Gradle. 然后是全局构建的问题,可以使用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. Gradle调用NPM(感谢https://github.com/srs/gradle-node-plugin )来测试和构建JS部分,然后构建Java部分。 It finally copies the final jar and the javascript files in a final and global build directory. 它最终将最终jar和javascript文件复制到最终的全局构建目录中。 The name of the JS files - which contains a hash, you should keep that powerful feature ! JS文件的名称 - 包含哈希值,你应该保留这个强大的功能! - are given to a conf file which is used by the Java application. - 给予Java应用程序使用的conf文件。

I had struggled a lot to find a solution to exclude node_modules in my webapp. 我一直在努力寻找一种解决方案来排除我的webapp中的node_modules。 I added following block in my build.gradle 我在build.gradle中添加了以下块

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

   }
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM