Over a million developers have joined DZone.

Introducing the NPM Maven Plugin

· DevOps Zone

The DevOps zone is brought to you in partnership with Sonatype Nexus. The Nexus suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

This post comes from Alberto Pose at the MuleSoft blog.

Introduction

Suppose that you have a project and you want to download .js modules previously uploaded to NPM. One way of doing that without running node is by using the npm-maven-plugin. It allows the user to download the required Node modules without running node.js: It is completely implemented on the JVM.

Getting Started

First of all, you will need to add the Mule Maven repo to your pom.xml file:

<repository>
  <id>mulesoft-releases</id>
  <name>MuleSoft Repository</name>
  <url>https://repository.mulesoft.org/releases/</url> 
</repository>

After doing that, you will need to add the following to the build->plugin section of your pom.xml file:

<plugin>
    <groupId>org.mule.tools.javascript</groupId>
    <artifactId>npm-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>fetch-modules</goal>
            </goals>
            <configuration>
                <packages>
                    <package>colors:0.5.1</package>
                    <package>jshint:0.8.1</package>
                </packages>
            </configuration>
        </execution>
    </executions>
</plugin>

Then just execute:

mvn generate-sources
and that’s it!

One more thing…

By default, the modules can be found in src/main/resources/META-INF but that path can be changed setting the ‘outputDirectory’ parameter.

Also, module transitivity is taken into account. That means that it will download all the required dependencies before downloading the specified module.

Show me the code!

The source code can be found here. Feel free to fork the project and propose changes to it.

Happy (Node.js and Maven) hacking!





The DevOps zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:

Published at DZone with permission of Ross Mason, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}