如何配置mvn以下载npm依赖项?

时间:2017-07-16 08:17:37

标签: maven npm dependency-management

作为mvn构建过程的一部分,我确实想下载/安装一个作为npm包发布的javascript文件。

目前该文件已复制到war/scripts/foobar.js,这意味着如果将更新版本的foobar发布到npm,我们必须从那里手动下载并更新我们的代码。

是否无法以与java依赖项类似的方式集成此依赖项?

如果你能提供一个这样做的例子,那就太好了。

2 个答案:

答案 0 :(得分:1)

使用NPM资源,您可能会使用npm install以正确的方式获取它。我假设您已经这样做,然后复制资源。

如果是这种情况,你可以只使用maven-exec-plugin并在编译之前运行命令,例如:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>npm-install</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>exec</goal>
          </goals>
          <configuration>
            <executable>npm</executable>
            <arguments>
              <argument>install</argument>
            </arguments>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

答案 1 :(得分:0)

一种方法是使用npm,这涉及在构建期间合并节点的下载/运行和npm。另一种方法是使用maven本身,而不调用任何NodeJS进程。

对于较大的项目,这些项目具有很多npm依赖关系,并且在开发过程中可能会发生很大变化,因此使用package.json描述符和npm本身会更容易。

对于只需要存档的较小项目,我更喜欢使用简单的maven插件设置,例如:

        <plugins>
            <plugin>
                <groupId>com.googlecode.maven-download-plugin</groupId>
                <artifactId>download-maven-plugin</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <id>download-babel-js</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>
https://coporate.nexus.instance/repository/npm-proxy/babel-standalone/-/babel-standalone-6.26.0.tgz
                            </url>
                            <unpack>true</unpack>
                            <outputDirectory>${project.build.directory}/babel</outputDirectory>
                            <md5>ed714bb8253fc6056902d8d0afd6b106</md5>
                        </configuration>
                    </execution>

                    <execution>
                        <id>download-redux</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <url>https://coporate.nexus.instance/repository/unpkg/redux@4.0.5/dist/redux.min.js</url>
                            <unpack>false</unpack>
                            <outputDirectory>${project.build.directory}/${project.build.finalName}/js</outputDirectory>
                            <md5>8e679dc2c2ea96e12f8189d85485927b</md5>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

要增加趣味性,您可以使用以下纯java maven插件在脚本中添加缩小和babel预处理:

            <plugin>
                <groupId>com.jarslab.maven</groupId>
                <artifactId>babel-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>js-transpile</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>babel</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF-8</encoding>
                            <verbose>true</verbose>
                            <babelSrc>${project.basedir}/target/babel/package/babel.min.js</babelSrc>
                            <sourceDir>${project.basedir}/src/main/es6</sourceDir>
                            <targetDir>${project.build.directory}/js</targetDir>
                            <jsSourceIncludes>
                                <jsSourceInclude>*.js</jsSourceInclude>
                            </jsSourceIncludes>
                            <presets>es2015</presets>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.github.blutorange</groupId>
                <artifactId>closure-compiler-maven-plugin</artifactId>
                <version>2.12.0</version>
                <executions>
                    <execution>
                        <id>default-minify</id>
                        <phase>process-resources</phase>
                        <configuration>

                            <!-- These are the defaults -->
                            <encoding>UTF-8</encoding>
                            <baseSourceDir>${project.build.directory}</baseSourceDir>
                            <baseTargetDir>${project.build.directory}/${project.build.finalName}</baseTargetDir>
                            <sourceDir>js</sourceDir>
                            <targetDir>js</targetDir>

                            <!-- List of files to process. May use wildcards. -->
                            <includes>
                                <include>my-script.js</include>
                            </includes>
                            <outputFilename>my-script.min.js</outputFilename>

                        </configuration>

                        <goals>
                            <goal>minify</goal>
                        </goals>

                    </execution>
                </executions>
            </plugin>

在此,我要感谢出色的插件维护人员的出色工作!

  1. Mickaelothers用于Google下载插件:https://github.com/maven-download-plugin/maven-download-plugin
  2. Milosz for babel-maven-plugin:https://github.com/jarslab/babel-maven-plugin
  3. Andre(用于closure-compiler-maven-plugin):https://github.com/blutorange/closure-compiler-maven-plugin
相关问题