现代前端 + SpringBoot 结合开发的 Maven 配置

好些日子之前就在网上看见一篇文章,说一个小后端想用 Vue 作前端技术结合 SpringBoot 后端作开发,但又想方便点让前端的工程能够自己跑进 Jar 包里。很感兴趣诶,于是就动手跟着实现一遍。

原文:A Lovely Spring View: Spring Boot & Vue.js

原理实际是利用 frontend-maven-plugin 来调用 node ,不过这个插件有个好处,它是在工程的目录下安装 node,这样能摆脱对本机 node 的依赖,在很多地方进行构建。

起来先建一个普通的 SpringBoot 工程项目,然后普通地把 src/ 删掉开始建立子模块。我是建立了 backend 和 frontend 两个模块。

springboot-vue/
    |- frontend/
    |   |- src/
    |   |   |- ...
    |   |- pom.xml
    |- backend/
    |   |- src/
    |   |   |- ...
    |   |- pom.xml
    |- pom.xml

根 pom :



    4.0.0

    net.catten
    springboot-vue
    0.0.1-SNAPSHOT
    
        backend
        frontend
    
    pom

    project-management
    Team project progress management system

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-logging
        
    

后端,backend 需要配置一下 maven-resource-plugin



    
        springboot-vue
        net.catten
        0.0.1-SNAPSHOT
    
    4.0.0

    backend

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                maven-resources-plugin
                
                    
                        copy Vue frontend content
                        generate-resources
                        
                            copy-resources
                        
                        
                            src/main/resources/static
                            true
                            
                                
                                    
                                        ${project.parent.basedir}/frontend/dist
                                    
                                
                            
                        
                    
                
            
        
    

对这个插件熟悉的就可以跳过下面说明了。

其中 plugin 的 configuration 内,outputDirectory 是指输出的地方, resource 指从哪里复制文件,复制的文件当然就是 webpack 打包出来的了。

前端,配置 frontend-maven-plugin



    
        springboot-vue
        net.catten
        0.0.1-SNAPSHOT
    
    4.0.0

    frontend

    
        
            
                com.github.eirslett
                frontend-maven-plugin
                1.6
                
                    
                    
                        install node and npm
                        
                            install-node-and-npm
                        
                        
                            v9.11.1
                        
                    
                    
                    
                        npm install
                        
                            npm
                        
                        
                        generate-resources
                        
                        
                            install
                        
                    
                    
                    
                        npm run build
                        
                            npm
                        
                        
                            run build
                        
                    
                
            
        
    


这个基本上不用去动,如果对 nodeVersion 有要求可以修改一下。这里的配置是会在 generate-resources 的时候调用 npm run build ,实际拷贝资源文件的操作还是 backend 内的插件做的,所以拷贝的源文件路径需要和 webpack 的配置配合,按需修改 backend 的 configurations。

是一篇只知道怎么用不去深究喂到嘴边式快餐文(逃)

你可能感兴趣的:(现代前端 + SpringBoot 结合开发的 Maven 配置)