https://github.com/szgabsz91/npm-maven-extension
A Maven extension and a Maven plugin to download NPM packages into the local Maven repository and extract them into an arbitrary folder
https://github.com/szgabsz91/npm-maven-extension
maven maven-extension maven-plugin npm
Last synced: 8 months ago
JSON representation
A Maven extension and a Maven plugin to download NPM packages into the local Maven repository and extract them into an arbitrary folder
- Host: GitHub
- URL: https://github.com/szgabsz91/npm-maven-extension
- Owner: szgabsz91
- License: mit
- Created: 2019-05-19T07:34:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-11-14T17:51:54.000Z (about 2 years ago)
- Last Synced: 2025-01-29T13:29:16.512Z (10 months ago)
- Topics: maven, maven-extension, maven-plugin, npm
- Language: Java
- Size: 881 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# npm-maven-extension
[](https://circleci.com/gh/szgabsz91/workflows/npm-maven-extension)
[](https://central.sonatype.com/artifact/com.github.szgabsz91/npm-maven-extension-parent)
[](https://github.com/szgabsz91/npm-maven-extension/blob/master/LICENSE)
This is an experimental project that demonstrates an alternative way of downloading NPM packages during a Maven build.
The most mainstream way of doing this would be to use [webjars](https://www.webjars.org/). This project contains another
way that I don't recommend using in production, but had much fun implementing it.
The main building blocks are the following:
* [npm-maven-extension](extension): downloads the NPM packages added to the pom.xml from an arbitrary NPM repository,
with or without authentication. The downloaded tar.gz file will be stored in the
local Maven repository.
* [npm-maven-plugin](plugin): during the Maven build,
the [extract](plugin/src/main/java/com/github/szgabsz91/maven/plugins/npm/ExtractMojo.java)
goal can extract the downloaded tar.gz file into an arbitrary, configurable folder. The
extracted resources can be referenced by your client web application.
* [npm-maven-extension-demo](demo): a Spring Boot demo application that contains a single HTML page that displays a
Bootstrap based welcome page. Bootstrap is downloaded using the custom extension.
The site documentation of this project can be found on [Github Pages](https://szgabsz91.github.io/npm-maven-extension/introduction.html).
## Requirements
### settings.xml
The `~/.m2/settings.xml` file must at least contain an NPM server and a repository that uses *npm-http* or *npm-https*
as its protocol. The URL can point to the public npmjs.org server or any other private NPM repositories. The server node
can contain authentication information, too.
A sample settings.xml file looks like this:
```xml
npm
${optional_username}
${optional_password}
npm
npm
npm-https://registry.npmjs.org
never
ignore
never
ignore
npm
```
### pom.xml
In the pom.xml file of your client project, you must reference the NPM dependencies with the *npm* type property, and
define the extension, as well as the plugin.
A sample pom.xml file looks like this:
```xml
4.0.0
com.demo
demo
1.0.0-SNAPSHOT
5.2.3
<_angular_router.version>15.0.4
1.0.1
npm
bootstrap
${bootstrap.version}
npm
npm
_angular_router
${_angular_router.version}
npm
com.github.szgabsz91
npm-maven-extension
${npm-maven-extension.version}
com.github.szgabsz91
npm-maven-plugin
${npm-maven-extension.version}
extract
${project.basedir}/src/main/resources/static/npm
```
This project downloads [bootstrap@5.2.3](https://www.npmjs.com/package/bootstrap) and
[@angular/router@15.0.4](https://www.npmjs.com/package/@angular/router) into the `src/main/resources/static/npm` folder.
For a working example, see the [demo application](demo).
## Testing the Project
You can test the project with the following commands:
```bash
./mvnw clean site install
cd demo
./mvnw clean install
```
After that, if you open the `demo/src/main/resources/static/npm` folder, you'll see
* angular
* router
* package
* ...
* bootstrap
* package
* ...
Also, you can build a Docker image from this simple war project and run it:
```bash
./mvnw clean install -Pdocker
docker run -p 8080:8080 npm-maven-extension-demo
```
After that, opening `http://localhost:8080/npm-maven-extension-demo`, you'll see a Bootstrap based welcome page: