【问题标题】:Copy artifacts generated by ng build in outdir to another folder将 ng build in outdir 生成的工件复制到另一个文件夹
【发布时间】:2020-12-15 23:03:24
【问题描述】:
我有一个角度项目。当我们运行 ng build 命令时,会在 dist 文件夹中创建构建工件,因为我们在 angular-cli.json 中设置了"outDir": "dist",。在此之后,我必须手动将这些文件从 dist 复制到 java 的 Webcontent 文件夹以生成 WAR 文件。
有没有办法自动化处理工件的过程。如果我将 WebContent 的路径设置为 outDir 值 "outDir": "../server/WebContent", ,则构建命令会在生成构建工件之前清理现有的 java 文件。
任何建议都会有所帮助..
【问题讨论】:
标签:
node.js
angular
npm
angular-cli
ng-build
【解决方案1】:
考虑以下解决方案:
- 在您的项目 package.json 文件中添加一个名为
build 的 npm script 以执行您的 ng build 命令。
- 另外添加一个名为
postbuild 的 post hook npm 脚本来复制您的文件。
下面描述了如何在一个*Nix平台上实现这一点,同时也提供了一个跨平台的解决方案。
*Nix (Linux, macOS, ...)
在 *nix 平台上,npm 使用 sh 作为运行 npm 脚本的默认 shell。因此,将以下内容添加到您的项目 package.json 的 scripts 部分:
包,json
"scripts": {
"build": "ng build",
"postbuild": "cp -r dist/ ../server/WebContent"
}
这个 postbuild npm 脚本利用 shells cp 命令递归地将工件从 dist 目录复制到 ../server/WebContent 目录。 postbuild 脚本将在build 脚本成功完成后自动运行。
跑步:
通过您的命令行运行以下命令而不是 ng build:
npm run build
跨平台(Linux、macOS、Windows...)
对于跨平台解决方案,首先安装 shx - 它是 ShellJS Unix 命令的包装器:
-
cd 到您的项目目录。
-
然后运行以下命令:
npm install -D shx
如下定义项目package.json的scripts部分:
package.json
"scripts": {
"build": "ng build",
"postbuild": "shx cp -r \"dist/*\" \"../server/WebContent\""
}
跑步:
通过您的命令行运行以下命令而不是 ng build:
npm run build