現(xiàn)代前端 + SpringBoot 結(jié)合開(kāi)發(fā)的 Maven 配置

好些日子之前就在網(wǎng)上看見(jiàn)一篇文章,說(shuō)一個(gè)小后端想用 Vue 作前端技術(shù)結(jié)合 SpringBoot 后端作開(kāi)發(fā),但又想方便點(diǎn)讓前端的工程能夠自己跑進(jìn) Jar 包里。很感興趣誒,于是就動(dòng)手跟著實(shí)現(xiàn)一遍。

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

原理實(shí)際是利用 frontend-maven-plugin 來(lái)調(diào)用 node ,不過(guò)這個(gè)插件有個(gè)好處,它是在工程的目錄下安裝 node,這樣能擺脫對(duì)本機(jī) node 的依賴,在很多地方進(jìn)行構(gòu)建。

起來(lái)先建一個(gè)普通的 SpringBoot 工程項(xiàng)目,然后普通地把 src/ 刪掉開(kāi)始建立子模塊。我是建立了 backend 和 frontend 兩個(gè)模塊。

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

根 pom :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.catten</groupId>
    <artifactId>springboot-vue</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>backend</module>
        <module>frontend</module>
    </modules>
    <packaging>pom</packaging>

    <name>project-management</name>
    <description>Team project progress management system</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
    </dependencies>
</project>

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-vue</artifactId>
        <groupId>net.catten</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>backend</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy Vue frontend content</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>src/main/resources/static</outputDirectory>
                            <overwrite>true</overwrite>
                            <resources>
                                <resource>
                                    <directory>
                                        ${project.parent.basedir}/frontend/dist
                                    </directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

對(duì)這個(gè)插件熟悉的就可以跳過(guò)下面說(shuō)明了。

其中 plugin 的 configuration 內(nèi),outputDirectory 是指輸出的地方, resource 指從哪里復(fù)制文件,復(fù)制的文件當(dāng)然就是 webpack 打包出來(lái)的了。

前端,配置 frontend-maven-plugin

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-vue</artifactId>
        <groupId>net.catten</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>frontend</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <!-- Install our node and npm version to run npm/node scripts-->
                    <execution>
                        <id>install node and npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                        <configuration>
                            <nodeVersion>v9.11.1</nodeVersion>
                        </configuration>
                    </execution>
                    <!-- Install all project dependencies -->
                    <execution>
                        <id>npm install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <!-- optional: default phase is "generate-resources" -->
                        <phase>generate-resources</phase>
                        <!-- Optional configuration which provides for running any npm command -->
                        <configuration>
                            <arguments>install</arguments>
                        </configuration>
                    </execution>
                    <!-- Build and minify static files -->
                    <execution>
                        <id>npm run build</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>run build</arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

這個(gè)基本上不用去動(dòng),如果對(duì) nodeVersion 有要求可以修改一下。這里的配置是會(huì)在 generate-resources 的時(shí)候調(diào)用 npm run build ,實(shí)際拷貝資源文件的操作還是 backend 內(nèi)的插件做的,所以拷貝的源文件路徑需要和 webpack 的配置配合,按需修改 backend 的 configurations。

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,253評(píng)論 6 342
  • 最近在工作期間,搭建了一個(gè)專(zhuān)項(xiàng)測(cè)試平臺(tái)。其中用到的一些web開(kāi)發(fā)技能。這里想記錄下。準(zhǔn)備來(lái)逐步介紹用到的框架,及開(kāi)...
    professorLea閱讀 12,873評(píng)論 7 38
  • 真正的愛(ài)情,應(yīng)該是你支持我的夢(mèng)想,理解我的努力;我也支持你的夢(mèng)想,理解你的努力。 然后,我們都愿意對(duì)方進(jìn)入我們的生...
    情海無(wú)涯閱讀 300評(píng)論 0 0
  • 同學(xué)聚會(huì)邀請(qǐng)函:但愿記得你我 獨(dú)山中學(xué) 親愛(ài)的同學(xué):同窗三載,溫馨如昨,依然常駐心頭;悲歡歲月,依稀如夢(mèng),但愿...
    阿儲(chǔ)閱讀 502評(píng)論 0 1

友情鏈接更多精彩內(nèi)容