如何使用maven部署node.js应用程序?

我们的大部分团队都由java开发人员组成,因此整个构build/部署/依赖pipe理系统build立在maven之上。 我们使用CI,所以每个构build过程都运行unit testing(前端的业力和幻影,后端的茉莉节点)。 我设法为此configuration一个业力maven插件。

这并不能解决在build上从package.json下载node.js依赖关系的问题。 我需要在现有环境中部署我的node.js / express应用程序,所以完美的场景是:

  1. 从回购拉(使用maven构build自动完成)
  2. npm install (即 – 从节点包registry中下载依赖项)
  3. 运行testing

我试图为mavenfind一个nodejs包,但说实话 – 作为一个node.js开发人员,我不觉得在select正确的工具方面非常自信,因为我无法区分一个不好的maven插件一个体面的。

也许使用shell插件并从terminal调用npm install是一个更好的select?

你怎么看?

你有两个select:

作为一个hacky解决scheme,尽pipe仍然可行,但你可以像你提到的那样使用maven-antrun-plugin来实际执行npm。

所有的方法都有其优点和缺点,但前端Maven插件似乎是最常用的方法 – 但它假设您的ci服务器可以从互联网上下载任意包,而“哈克”解决scheme也应该工作,当你的ci服务器根本没有连接到互联网(除了代理中央maven回购)

我想你可以在Grunt和许多可用的插件中find答案。

实际上,我正在开发一个Web客户端,使用AngularJS 。 不过,我认为部署过程可能会部分回答您的问题:

在你的pom.xml ,你可以这样做:

 <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> 
  • 第一部分是npm install任务:从节点包下载依赖关系。

  • 第二部分是bower install任务:用bower降低其他依赖(在我的情况下, AngularJS ,但是你可能不需要这部分)

  • 第三部分是Grunt Release部分:发起包含Karmaunit testing的Grunt任务。

你可以在这里find有关Grunt文档。 有许多可用的插件,如Karmaunit testing。

我希望这对你有帮助。

我通过exec-maven-plugin为我的AngularJS 2 + Spring Boot应用程序创build了npm进程。 我不使用bower和grunt,但是看一下上面的Pear的antrun例子后,也可以使用exec-maven-plugin来工作。

下面是我的exec-maven-plugin的pom.xml示例。 我的app有package.json,所有的AngularJS .ts文件都在src / main / resources下,所以从path下运行npm。 我运行npm安装的依赖和npm运行tsc的.ts转换为.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> 

一个小黑客就是在Windows或Mac上运行Eclipse上的maven构build。 在eclipse上用linux甚至在Windows命令窗口上也可以很好。 在Windows上运行eclipse时,它不能理解npm,并抱怨找不到该文件。 奇怪的是npm在Windows命令窗口上工作正常。 所以解决这个问题我在系统path下创build了npm.bat文件。 在我的情况下,nodejs和npm安装在C:\ Program File \ nodejs下。 把这个batch file后。 一切正常。

npm.bat

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

对于Mac,我在eclipse上也遇到同样的问题。 事情是nodejs和npm安装在/ usr / local / bin下。 所以为了解决这个问题,我把/ usr / local / bin / node和/ usr / local / bin / npm这个符号链接放在/ user / bin下面。 但是/ usr / bin在安全策略中受到保护,从恢复盘启动后我做了这个