How to configure mvn to download a npm dependency?

Viewed 3696

As part of the mvn build process, I do want to download/install a javascript file that is published as a npm package.

Currently that file was copied into war/scripts/foobar.js which means that if a newer version of the foobar is published to npm, we would have to manually download it from there and update our code.

Isn't possible to integrate this dependency in a similar way as java dependencies?

If you could provide me with one example of doing this it would be great.

2 Answers

One method is to use npm which involves incorporation of download/run of node and npm during your build. Other approach is to use maven itself and do not invoke any NodeJS processes.

For bigger projects where you have many npm dependencies which probably change a lot during development it's easier to use package.json descriptor and npm itself.

For smaller project where you just need on file I prefer to use simple maven plugin setup like this one for example:

        <plugins>
            <plugin>
                <groupId>com.googlecode.maven-download-plugin</groupId>
                <artifactId>download-maven-plugin</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <id>download-babel-js</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>
https://coporate.nexus.instance/repository/npm-proxy/babel-standalone/-/babel-standalone-6.26.0.tgz
                            </url>
                            <unpack>true</unpack>
                            <outputDirectory>${project.build.directory}/babel</outputDirectory>
                            <md5>ed714bb8253fc6056902d8d0afd6b106</md5>
                        </configuration>
                    </execution>

                    <execution>
                        <id>download-redux</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>https://coporate.nexus.instance/repository/unpkg/redux@4.0.5/dist/redux.min.js</url>
                            <unpack>false</unpack>
                            <outputDirectory>${project.build.directory}/${project.build.finalName}/js</outputDirectory>
                            <md5>8e679dc2c2ea96e12f8189d85485927b</md5>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

To spice things up you can add minification and babel preprocessing to your scripts with following pure java maven plugins:

            <plugin>
                <groupId>com.jarslab.maven</groupId>
                <artifactId>babel-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>js-transpile</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>babel</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF-8</encoding>
                            <verbose>true</verbose>
                            <babelSrc>${project.basedir}/target/babel/package/babel.min.js</babelSrc>
                            <sourceDir>${project.basedir}/src/main/es6</sourceDir>
                            <targetDir>${project.build.directory}/js</targetDir>
                            <jsSourceIncludes>
                                <jsSourceInclude>*.js</jsSourceInclude>
                            </jsSourceIncludes>
                            <presets>es2015</presets>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.github.blutorange</groupId>
                <artifactId>closure-compiler-maven-plugin</artifactId>
                <version>2.12.0</version>
                <executions>
                    <execution>
                        <id>default-minify</id>
                        <phase>process-resources</phase>
                        <configuration>

                            <!-- These are the defaults -->
                            <encoding>UTF-8</encoding>
                            <baseSourceDir>${project.build.directory}</baseSourceDir>
                            <baseTargetDir>${project.build.directory}/${project.build.finalName}</baseTargetDir>
                            <sourceDir>js</sourceDir>
                            <targetDir>js</targetDir>

                            <!-- List of files to process. May use wildcards. -->
                            <includes>
                                <include>my-script.js</include>
                            </includes>
                            <outputFilename>my-script.min.js</outputFilename>

                        </configuration>

                        <goals>
                            <goal>minify</goal>
                        </goals>

                    </execution>
                </executions>
            </plugin>

Hereby I would like to thank great plugin maintainers for their great job!

  1. Mickael and others from for google download plugin: https://github.com/maven-download-plugin/maven-download-plugin
  2. Milosz for babel-maven-plugin: https://github.com/jarslab/babel-maven-plugin
  3. Andre for closure-compiler-maven-plugin: https://github.com/blutorange/closure-compiler-maven-plugin
Related