Copy artifacts generated by ng build in outdir to another folder

Viewed 1332

I have an angular project. when we run ng build command the build artifacts are created in dist folder as we have set "outDir": "dist", in angular-cli.json. After this I have to manually copy these files from dist to Webcontent folder of java to generate a WAR file.

Is there any way to automate the process coping the artifacts. If I set the path of WebContent as outDir value "outDir": "../server/WebContent", the build command is cleaning up the existing java files before generating build artifacts.

Any suggestions will help..

1 Answers

Consider the following solution:

  1. Add a npm script named build in your projects package.json file to execute your ng build command.
  2. Also add a post hook npm script named postbuild to copy your files.

The following describes how to achieve this on a *Nix platform, and also provides a cross-platform solution.


*Nix (Linux, macOS, ... )

On *nix platforms npm utilizes sh as the default shell for running npm scripts. Therefore add the following to the scripts section of your projects package.json:

package,json

"scripts": {
  "build": "ng build",
  "postbuild": "cp -r dist/ ../server/WebContent"
}

This postbuild npm script utilizes the shells cp command to recursively copy artifacts from the dist directory to the ../server/WebContent directory. The postbuild script will automatically run when the build script successfully completes.

Running:

Via your command line run the following command instead of ng build:

npm run build

Cross-platform (Linux, macOS, Windows, ... )

For a cross-platform solution firstly install shx - it’s a wrapper around ShellJS Unix commands:

  1. cd to your project directory.

  2. Then run the following command:

    npm install -D shx
    

Define the scripts section of your projects package.json as follows:

package.json

"scripts": {
  "build": "ng build",
  "postbuild": "shx cp -r \"dist/*\" \"../server/WebContent\""
}

Running:

Via your command line run the following command instead of ng build:

npm run build
Related