該文件需要指定下列參數(shù):
(1) node版本
(2) npm版本
(3) working directory
(4) 加入3個<execution>:
+ 下載node和npm到node和node_modules路徑下唇兑。
+ npm下載該項目的依賴包
+ npm 進行項目的build
默認情況下拯刁,angular-cli會把最終的ng2應用放在src\main\frontend\dist
路徑下,可以通過修改angular-cli.json
文件將最終生成的路徑修改成我們需要打包放置的位置上达布。
{
"root": "src",
"outDir": "../../../target/frontend",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"styles.css"
],
"scripts": [],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
]
第五步: 使spring boot打包時候能聯(lián)動angular2.
由于maven打包時候,會默認把resource路徑下的東西拷貝到jar包中纬向,因此,可以將打包后的ng2程序添加到maven的resource路徑下。
<dependency>
<groupId>com.jdriven.ng2boot</groupId>
<artifactId>frontend</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
···
<resources>
<resource>
<directory>target/frontend</directory>
<targetPath>static</targetPath>
</resource>
</resources>
第六步: 執(zhí)行打包和測試
在主目錄下執(zhí)行下列的命令:
mvn clean install
cd backend
mvn spring-boot:run
從瀏覽器中進行測試:
http://localhost:8080
多說一句
在ng2中要實現(xiàn)服務端不同端口的通信加缘,需要在前端這樣實現(xiàn)熊楼。
- 修改package.json代碼霹娄,添加如下:
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.conf.json",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
}
修改proxy.conf.json
文件,添加如下:
{
"/api": {
"target": "http://localhost:8080",
"secure": false
}
}