好些日子之前就在網(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。
是一篇只知道怎么用不去深究喂到嘴邊式快餐文(逃)