仅安装一次节点,而不是每次都使用Maven构建

时间:2016-06-28 03:42:28

标签: maven gruntjs

我需要一个帮助。

我正在使用grunt来编译CSS / JS

我遇到一个问题,我的节点包是用每个构建创建的,并且它在Jenkins中占用了大量空间。我正在使用maven前端插件。

我希望节点包最初只创建一次,而不是每次创建maven一次。

 <id>grunt</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.github.eirslett</groupId>
                        <artifactId>frontend-maven-plugin</artifactId>
                        <version>0.0.26</version>
                        <!-- optional -->
                        <configuration>
                            <workingDirectory>DIR
                            </workingDirectory>
                            <nodeVersion>v4.2.1</nodeVersion>
                            <npmVersion>3.5.1</npmVersion>
                            <installDirectory>node</installDirectory>
                        </configuration>
                      <executions>
                            <execution>
                                <id>node and npm install</id>
                                <goals>
                                    <goal>install-node-and-npm</goal>
                                </goals>
                                <configuration>
                                    <arguments>install</arguments>
                                    <installDirectory>node</installDirectory>
                                </configuration>
                            </execution>
                            <execution>
                                <id>npm install</id>
                                <goals>
                                    <goal>npm</goal>
                                </goals>
                                <configuration>
                                    <arguments>install</arguments>
                                    <installDirectory>node</installDirectory>
                                </configuration>
                            </execution>
                            <execution>
                                <id>grunt build</id>
                                <goals>
                                    <goal>grunt</goal>
                                </goals>
                                <configuration>
                                    <arguments>build</arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>

我们正在使用maven -P grunt进行构建。它正在为每个maven构建创建和安装节点。

为了全局拥有节点,我正在尝试使用maven -P grunt -g,但它无法正常工作。

在GitHub小组中,我看到有人提到我们不能使用maven前端插件,所以我尝试了maven exec插件。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
      <execution>
        <id>prepare-dist-npm-runtime-dependency</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>exec</goal>
        </goals>
        <configuration>
          <executable>node/node</executable>
          <workingDirectory>dist</workingDirectory>
          <arguments>
            <argument>../node/npm/cli.js</argument>
            <argument>install</argument>
            <argument>--production</argument>
          </arguments>
        </configuration>
      </execution>
    </executions>
  </plugin>

但我无法看到它有效。任何人都可以帮助如何运行maven以使其适用于全局节点安装而不是每次构建都安装节点吗?

如果任何其他建议只安装一次节点而不是全局节点,将会感激不尽。

1 个答案:

答案 0 :(得分:3)

您无法使用Frontend maven plugin全局安装Node或NPM。如果我们看一下documentation,我们会看到插件的整个目的是能够在一个隔离的环境中安装Node和NPM,仅用于构建,而不会影响机器的其余部分。

  

Node / npm只会在本地“安装”到您的项目中。它不会   全局安装在整个系统上(它不会干扰   已经存在任何Node / npm安装。)

     

不是要取代开发人员版本的Node - 前端   开发人员仍然会在他们的笔记本电脑上安装Node,但是后端   开发人员可以运行干净的构建,甚至无需在其上安装Node   计算机。

     

不打算安装Node用于生产用途。节点用法是   打算作为前端构建的一部分,运行常见的JavaScript任务   如缩小,混淆,压缩,包装,测试   等

您可以尝试使用两个不同的配置文件运行该插件,一个用于安装,另一个用于安装后的日常使用。在下面的示例中,您应该能够通过运行:

来安装Node和NPM
mvn clean install -PinstallNode

然后每次构建:

mvn clean install -PnormalBuild

或者因为默认情况下normalBuild设置为有效,只需:

mvn clean install

通知安装目标不在日常配置文件中:

<profiles>
    <profile>
        <id>installNode</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>0.0.26</version>
                    <!-- optional -->
                    <configuration>
                        <workingDirectory>DIR</workingDirectory>
                        <nodeVersion>v4.2.1</nodeVersion>
                        <npmVersion>3.5.1</npmVersion>
                        <installDirectory>node</installDirectory>
                    </configuration>
                  <executions>
                        <execution>
                            <id>node and npm install</id>
                            <goals>
                                <goal>install-node-and-npm</goal>
                            </goals>
                            <configuration>
                                <arguments>install</arguments>
                                <installDirectory>node</installDirectory>
                            </configuration>
                        </execution>
                        <execution>
                            <id>npm install</id>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <configuration>
                                <arguments>install</arguments>
                                <installDirectory>node</installDirectory>
                            </configuration>
                        </execution>
                        <execution>
                            <id>grunt build</id>
                            <goals>
                                <goal>grunt</goal>
                            </goals>
                            <configuration>
                                <arguments>build</arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>normalBuild</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>0.0.26</version>
                    <!-- optional -->
                    <configuration>
                        <workingDirectory>DIR</workingDirectory>
                        <nodeVersion>v4.2.1</nodeVersion>
                        <npmVersion>3.5.1</npmVersion>
                        <installDirectory>node</installDirectory>
                    </configuration>
                    <executions>
                        <execution>
                            <id>grunt build</id>
                            <goals>
                                <goal>grunt</goal>
                            </goals>
                            <configuration>
                                <arguments>build</arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>