How to deploy a node.js app with maven? How to deploy a node.js app with maven? java java

How to deploy a node.js app with maven?


You've got two choices:

As a hacky solution, though still feasible you could as you've mentioned yourself, use something like maven-antrun-plugin to actually execute npm with maven.

All approaches have their pros and cons, but frontend-maven-plugin seems to be the most often used approach - but it assumes that your ci server can download from the internet arbitrary packages, whereas the "hacky" solution should also work, when your ci server has no connection to the internet at all (besides proxying the central maven repo)


I think you can find the answer in Grunt and the many available plugins.

I'm actually working on a web project where the client-side is made with AngularJS. Nevertheless, I think the deployement process may partially answer to your question :

In your pom.xml, you can do something like that:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-antrun-plugin</artifactId>    <version>1.5</version>    <executions>        <execution>            <id>exec-gen-sources</id>            <phase>generate-sources</phase>            <configuration>                <target name="Build Web">                    <exec executable="cmd" dir="${project.basedir}"                        failonerror="true" osfamily="windows">                        <arg line="/c npm install" />                    </exec>                    <exec executable="cmd" dir="${project.basedir}"                        failonerror="true" osfamily="windows">                        <arg line="/c bower install --no-color" />                    </exec>                    <exec executable="cmd" dir="${project.basedir}"                        failonerror="true" osfamily="windows">                        <arg line="/c grunt release --no-color --force" />                    </exec>                </target>            </configuration>            <goals>                <goal>run</goal>            </goals>        </execution>    </executions></plugin>
  • First part is the npm install task: downloading of dependencies from node package.

  • Second part is the bower install task: downoading of other dependencies with bower (in my case, AngularJS, but you might not need this part)

  • Third part is the Grunt Release part: launching a Grunt task that includes Karma unit testing.

You can find documentation about Grunt here. There are many available plugins like Karma unit testing.

I hope this helped you.


I made npm process work for my AngularJS 2 + Spring Boot application by exec-maven-plugin. I don't use bower and grunt, but think you can make it work by exec-maven-plugin too, after look at the antrun example above from Pear.

Below is my pom.xml example for exec-maven-plugin. My app has package.json and all the AngularJS .ts files are under src/main/resources, so run npm from the path. I run npm install for dependencies and npm run tsc for .ts conversion to .js

pom.xml

        <plugin>            <groupId>org.codehaus.mojo</groupId>            <artifactId>exec-maven-plugin</artifactId>            <executions>                <execution>                    <id>exec-npm-install</id>                    <phase>generate-sources</phase>                    <configuration>                        <workingDirectory>${project.basedir}/src/main/resources</workingDirectory>                        <executable>npm</executable>                        <arguments>                            <argument>install</argument>                        </arguments>                    </configuration>                    <goals>                        <goal>exec</goal>                    </goals>                </execution>                <execution>                    <id>exec-npm-run-tsc</id>                    <phase>generate-sources</phase>                    <configuration>                        <workingDirectory>${project.basedir}/src/main/resources</workingDirectory>                        <executable>npm</executable>                        <arguments>                            <argument>run</argument>                            <argument>tsc</argument>                        </arguments>                    </configuration>                    <goals>                        <goal>exec</goal>                    </goals>                </execution>            </executions>        </plugin>

One little hack on this is running maven build on eclipse with Windows or Mac. It perfectly fine on eclipse with linux or even also fine on Windows command window though. When run build on eclipse with Windows, it fail to understand npm and complain about not find the file. Weird thing is npm is working fine on Windows command window. So solving the hack I create npm.bat file under system path. In my case nodejs and npm are installed under C:\Program File\nodejs. After putting this batch file. everything works fine.

npm.bat

@echo offset arg1=%1set arg2=%2C:\Progra~1\nodejs\npm.cmd %arg1% %arg2%

For Mac, I got same issue on eclipse. The thing is nodejs and npm are installed under /usr/local/bin. So to solve the issue, I make symbolic link /usr/local/bin/node and /usr/local/bin/npm to under /user/bin. However /usr/bin is protected in security policy, I done that after booting from recovery disk