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

sor*_*rin 6 dependency-management maven npm

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

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

是否可以像java依赖项一样集成这个依赖项?

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

Nie*_*sen 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>
Run Code Online (Sandbox Code Playgroud)