使用ng2和Spring Boot集成一個項目
第一步 創(chuàng)建一個Spring Boot項目
在https://start.spring.io/上創(chuàng)建一個后臺的Spring Boot程序剧腻,以提供對應(yīng)的REST服務(wù)牡直。
第二步 分離前后臺模塊
將前后端分為對應(yīng)的不同maven模塊。
- 新建一個總的項目目錄艺沼,將后端程序拷貝進(jìn)去。
- 在總的目錄下新建一個前端路徑晶衷,如:
frontend\src\main
- 拷貝后端的pom文件沧烈,直接放在總的項目目錄下,并進(jìn)行修改喂击。
<?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>com.jdriven.ng2boot</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent</name>
<description>The ng2boot parent project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<modules>
<module>frontend</module>
<module>backend</module>
</modules>
</project>
- 修改后端的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>
<artifactId>backend</artifactId>
<name>backend</name>
<description>The ng2boot backend project</description>
<parent>
<groupId>com.jdriven.ng2boot</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 為前臺添加一個pom文件
<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>
<artifactId>frontend</artifactId>
<name>frontend</name>
<description>The ng2boot frontend project</description>
<parent>
<groupId>com.jdriven.ng2boot</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<plugins>
</plugins>
</build>
</project>
第三步 添加ng2程序到前端的main路徑
執(zhí)行命令: ng new --skip-git --directory frontend ng2boot
執(zhí)行項目路徑是當(dāng)前路徑下的frontend路徑下剂癌,創(chuàng)建一個名稱為ng2boot的項目(該名稱不在路徑上體現(xiàn))。
注意:不要創(chuàng)建git目錄翰绊,因為整體上已經(jīng)是一個maven項目佩谷,后續(xù)我們必然按照大項目來提交git。
第四步: 配置maven來build ng2的項目
使用frontend-maven-plugin插件來編譯ng2 的應(yīng)用程序监嗜。
- 添加插件到前端的pom文件的build/plugins部分谐檀。
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<nodeVersion>v6.10.1</nodeVersion>o
<npmVersion>4.4.1</npmVersion>
<workingDirectory>src/main/frontend</workingDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
</execution>
<execution>
<id>npm run build</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>