Angular CLI gives so much flexibility when we are developing Angular applications. So much of boiler plate code gets auto generated if you are using CLI.
What if we require Angular app to get integrated with existing Maven project?
Angular CLI and its command run in the Node environment. The Node environment is very much different from Maven environment.
In Maven, each module describes itself in a pom.xml including its dependencies and the build plugins. Build plugins attach to the project’s lifecycle and execute required tasks and commands. A module may have multiple sub modules and, obviously, a sub module references its parent module. Dependencies are installed to a local Maven repository on the user’s machine.
In Node environment, whether the package manager is npm, yarn or pnpm—dependencies are listed in a package.json. Running the package manager’s install command, e.g. yarn install, resolves dependencies, extracts tarballs from a registry and copies a flattened dependency tree to a local node_modules folder.
The major difference that is quite evident is, a local node_modules folder is on a per-package basis. On the other side, a local Maven repository is on a per-user basis and stores resolvable dependencies.
We have a Frontend Maven Plugin which enables to run Node and related package managers through Maven. The important thing here is that it makes sharing of one node_modules folder across several Maven modules.
In order to make use of this plugin, we need to update our parent pom to have this frontend maven plugin. A sample pom.xml making use of this plugin would be like below. In the similar manner we can have executables for yarn, grunt etc.
<id>install node and npm</id>
<!-- Optional configuration which provides for running any npm command -->
<id>npm run build</id>
The plugin has been designed to use a local installation of node. But in my case, even though I had the node installed on my machine, on the first run of ‘install’ it did download executable node. On subsequent runs, as the executable node was already present in the dependencies, it used that one. But I am wondering why it had do download the executable on the first run of install, as my system already had Node installed and configured.
Please let me know in case you know why the plugin behaved this way.