如何部署节点。js和maven应用吗?

时间:2021-05-14 15:42:38

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.

我们的团队大部分由java开发人员组成,因此整个构建/部署/依赖管理系统构建在maven之上。我们使用CI,所以每一个构建过程都运行单元测试(前端的w. karma和phantomJS,后端的jasmine-node)。我已经为这个目的配置了一个karma maven插件。

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:

这并不能解决下载节点的问题。js包的依赖关系。在构建json。我需要部署我的节点。js / express app在现有环境下,最理想的场景是:

  1. pull from the repo (done automatically with maven build)
  2. 从repo中提取(使用maven构建自动完成)
  3. npm install (that is - downloading dependencies from node package registry)
  4. npm安装(即从节点包注册中心下载依赖项)
  5. running tests
  6. 运行测试

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.

我试图为maven找到一个nodejs包,但说实话——作为一个节点。js开发人员在选择正确的工具时,我并不是很自信,因为我不能区分一个糟糕的maven插件和一个好的maven插件。

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

也许使用shell插件并从终端调用npm安装是更好的选择?

What's your opinion?

你的意见是什么?

3 个解决方案

#1


22  

You've got two choices:

你有两个选择:

  • https://github.com/eirslett/frontend-maven-plugin to let maven download your npm modules from your package.json and let it automagically install node and npm all along

    使用https://github.com/eirslett/frontend-maven-plugin让maven从包中下载npm模块。让它自动安装node和npm

  • https://github.com/mulesoft/npm-maven-plugin to let maven download your npm packages that you have specified in the pom.xml

    这个插件可以让maven下载您在pom.xml中指定的npm包

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.

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

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)

所有方法都有优点和缺点,但frontend-maven-plugin似乎是最常用的方法,但它假设您的ci服务器可以从互联网上下载任意包,而“出租汽车司机”解决方案也应该工作,当你的ci服务器没有连接到互联网,(除了代理*maven回购)

#2


13  

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

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

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 :

我正在做一个web项目,客户端是用AngularJS制作的。不过,我认为部署进程可能部分地回答了你的问题:

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

砰的一声。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>
  • First part is the npm install task: downloading of dependencies from node package.

    第一部分是npm安装任务:从节点包下载依赖项。

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

    第二部分是鲍尔安装任务:查找鲍尔的其他依赖项(在我的例子中是AngularJS,但是您可能不需要这个部分)

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

    第三部分是咕哝释放部分:启动一个包含Karma单元测试的咕哝任务。

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

您可以在这里找到关于Grunt的文档。有许多可用的插件,如Karma单元测试。

I hope this helped you.

我希望这对你有帮助。

#3


7  

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.

我为我的AngularJS 2 + Spring引导应用程序做了npm进程工作。我不使用bower和grunt,但是你也可以用execl -maven-plugin来完成它,看一下上面的antrun例子。

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

下面是我的砰的一声。exec-maven-plugin xml示例。我的应用程序包。json和所有AngularJS .ts文件都在src/main/resources下,所以从路径运行npm。我为依赖项运行npm安装,npm为.ts转换为.js运行tsc

pom.xml

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.

使用Windows或Mac在eclipse上运行maven构建是一个小技巧,在使用linux的eclipse上运行maven构建是完美的,甚至在Windows命令窗口上也是完美的。当使用Windows在eclipse上运行build时,它无法理解npm并抱怨找不到文件。奇怪的是npm在Windows命令窗口中运行良好。所以我创建了npm。在系统路径下的bat文件。在我的例子中,nodejs和npm是安装在C:\程序文件\nodejs下的。放入此批处理文件后。一切工作正常。

npm.bat

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

对于Mac,我在eclipse上也遇到了同样的问题。nodejs和npm是在/usr/ local/bin下安装的。为了解决这个问题,我将符号链接/usr/local/bin/node和/usr/local/bin/npm设置为under /user/bin。但是/usr/bin在安全策略中受到保护,我是在从恢复磁盘引导之后这样做的

#1


22  

You've got two choices:

你有两个选择:

  • https://github.com/eirslett/frontend-maven-plugin to let maven download your npm modules from your package.json and let it automagically install node and npm all along

    使用https://github.com/eirslett/frontend-maven-plugin让maven从包中下载npm模块。让它自动安装node和npm

  • https://github.com/mulesoft/npm-maven-plugin to let maven download your npm packages that you have specified in the pom.xml

    这个插件可以让maven下载您在pom.xml中指定的npm包

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.

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

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)

所有方法都有优点和缺点,但frontend-maven-plugin似乎是最常用的方法,但它假设您的ci服务器可以从互联网上下载任意包,而“出租汽车司机”解决方案也应该工作,当你的ci服务器没有连接到互联网,(除了代理*maven回购)

#2


13  

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

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

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 :

我正在做一个web项目,客户端是用AngularJS制作的。不过,我认为部署进程可能部分地回答了你的问题:

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

砰的一声。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>
  • First part is the npm install task: downloading of dependencies from node package.

    第一部分是npm安装任务:从节点包下载依赖项。

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

    第二部分是鲍尔安装任务:查找鲍尔的其他依赖项(在我的例子中是AngularJS,但是您可能不需要这个部分)

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

    第三部分是咕哝释放部分:启动一个包含Karma单元测试的咕哝任务。

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

您可以在这里找到关于Grunt的文档。有许多可用的插件,如Karma单元测试。

I hope this helped you.

我希望这对你有帮助。

#3


7  

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.

我为我的AngularJS 2 + Spring引导应用程序做了npm进程工作。我不使用bower和grunt,但是你也可以用execl -maven-plugin来完成它,看一下上面的antrun例子。

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

下面是我的砰的一声。exec-maven-plugin xml示例。我的应用程序包。json和所有AngularJS .ts文件都在src/main/resources下,所以从路径运行npm。我为依赖项运行npm安装,npm为.ts转换为.js运行tsc

pom.xml

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.

使用Windows或Mac在eclipse上运行maven构建是一个小技巧,在使用linux的eclipse上运行maven构建是完美的,甚至在Windows命令窗口上也是完美的。当使用Windows在eclipse上运行build时,它无法理解npm并抱怨找不到文件。奇怪的是npm在Windows命令窗口中运行良好。所以我创建了npm。在系统路径下的bat文件。在我的例子中,nodejs和npm是安装在C:\程序文件\nodejs下的。放入此批处理文件后。一切工作正常。

npm.bat

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

对于Mac,我在eclipse上也遇到了同样的问题。nodejs和npm是在/usr/ local/bin下安装的。为了解决这个问题,我将符号链接/usr/local/bin/node和/usr/local/bin/npm设置为under /user/bin。但是/usr/bin在安全策略中受到保护,我是在从恢复磁盘引导之后这样做的