如何创建 Maven javascript 项目的压缩工件

how to create a zipped artifact of a maven javascript project

我有一个 Maven JavaScript NodeJS 项目。以下是项目结构

-- Project
  -- dist
  -- node_modules
  -- src
  -- target
  Gruntfile.js
  gulpfile.js
  package.json
  pom.xml

有没有办法配置 pom,以便它构建 dist 文件夹的压缩文件并将其保存在输出目标目录中?

这可以使用 maven-assembly-plugin。这是一个非常通用的插件,可用于创建项目的自定义程序集。

它是通过 assembly.xml 文件配置的。对于您的情况,配置为:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  <id>dist</id>
  <formats>
    <format>zip</format> <!-- create a zip archive -->
  </formats>
  <fileSets>
    <fileSet>
      <directory>dist</directory> <!-- source is the "dist" folder -->
      <outputDirectory>/</outputDirectory> <!-- target is the root of the archive -->
    </fileSet>
  </fileSets>
</assembly>

此文件的典型位置是 src/main/assembly/assembly.xml。然后,POM 将包含:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>assembly-dist</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

调用 mvn clean package 后,target 文件夹将包含此插件生成的 zip 文件。