How to deploy a node.js app with maven?

Javanode.jsMavenContinuous IntegrationNpm

Java Problem Overview


Most of our team consists of java developers and therefore the whole build / deployment / dependency management system is built on top of maven. We use CI so every build process runs unit test (w. karma and phantomJS for the frontend, and jasmine-node for the backend). I've managed to configure a karma maven plugin for this purpose.

This does not solve the issue of downloading node.js dependencies from package.json on build. I need to deploy my node.js / express app in existing environment, so the perfect scenario would be:

  1. pull from the repo (done automatically with maven build)
  2. npm install (that is - downloading dependencies from node package registry)
  3. running tests

I was trying to find a nodejs package for maven, but to be honest - as a node.js developer I do not feel very confident when it comes to choosing the right tools, since I'm not able to distinguish a bad maven plugin from a decent one.

Maybe using a shell plugin and invoking npm install from the terminal is a better choice?

What's your opinion?

Java Solutions


Solution 1 - Java

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)

Solution 2 - Java

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.

Solution 3 - Java

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 off
set arg1=%1
set arg2=%2
C:\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

Solution 4 - Java

Since 2015, there is an alternative to the frontend-maven-plugin mentioned in Christian Ulbrich's excellent answer:

https://github.com/aseovic/npm-maven-plugin

Usage

Basically, all you have to do to use it is to put it into your POM as usual (and use "extensions:true"):

<build>
  <plugins>
    <plugin>
      <groupId>com.seovic.maven.plugins</groupId>
      <artifactId>npm-maven-plugin</artifactId>
      <version>1.0.4</version>
      <extensions>true</extensions>
    </plugin>
  [...]
  </plugins>
</build>

The plugin will then automatically bind to the Maven lifecycle. Then, you can put a script into your package.json, such as:

"scripts": 
  {
    "package": "npm pack",
  [...]
  }

and the npm script "package" will run automatically as part of the Maven build lifecycle phase "package".

Compared to frontend-maven-plugin

Just like frontend-maven-plugin, it will run npm scripts inside a maven project. There are two important differences:

  • frontend-maven-plugin will (and must) download and install npm itself. npm-maven-plugin uses (and requires) an installed version of npm.
  • frontend-maven-plugin requires you to describe every npm invocation in the POM (as an "execution" section). In contrast, npm-maven-plugin simply extends the Maven build lifecycle to automatically execute an npm script with the same name for each lifecycle phase (clean, install etc.). That means there is no npm-specific configuration in the POM - it's all taken from package.json.

Personally, I prefer the npm-maven-plugin's approach because it requires less configuration in the POM - POMs have a tendency to get bloated, and everything to counter that helps. Also, putting the npm invocations into package.json feels more natural and allows reusing them when invoking npm directly.

Admittedly, even with the frontend-maven-plugin you can [and probably should] define all npm invocations as scripts in package.json, and invoke these scripts from the POM, but there is still the temptation to put them directly into the POM.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRafal PastuszakView Question on Stackoverflow
Solution 1 - JavaChristian UlbrichView Answer on Stackoverflow
Solution 2 - JavaPearView Answer on Stackoverflow
Solution 3 - JavaSteve ParkView Answer on Stackoverflow
Solution 4 - JavasleskeView Answer on Stackoverflow