Maven Assembly Plugin

maven-assembly-plugin

默认的Descriptor摆放位置

my-assembly-descriptor
    +-- src
    |   `-- main
    |       `-- resources
    |           `-- assemblies
    |               `-- pack-assembly.xml
    `-- pom.xml

Sharing Assembly Descriptors

内置的Assembly Descriptor

默认情况下,maven-assembly-plugin 内置了几个可以用的 assembly descriptorbinjar-with-dependenciesprojectsrc

这几个描述文件内容可以在 maven-assembly-plugin 的 jar 包里找到,

maven-assembly-plugin-3.1.0.jar
    +-- assemblies
    |   `-- bin.xml
    |   `-- jar-with-dependencies.xml
	|   `-- project.xml
    |   `-- src.xml
    `-- META-INF
    `-- org.apache.maven.plugins.assembly

可参考 内置的 Descriptor,自定义的 Descriptor,放到正确的位置即可,descriptorRef 是自定义的文件名。

示例

pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.1.0</version>
  <dependencies>
    <dependency>
      <groupId>${groupId}</groupId>
      <artifactId>${artifactId}</artifactId>
      <version>${version}</version>
    </dependency>
  </dependencies>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptorRefs>
          <!-- 类似于 maven-shade-plugin 的打包方式,所有依赖会被解压后,放入同一个jar -->
          <!-- <descriptorRef>jar-with-dependencies</descriptorRef>-->

          <!-- 把 src 下文源代码,压缩成 tar.bz2/tar.gz/zip 三种格式的文件 -->
          <!--<descriptorRef>src</descriptorRef>-->
          <!-- 把编译后的文件(target/class)目录,压缩成 tar.bz2/tar.gz/zip 三种格式的文件-->
          <!--<descriptorRef>bin</descriptorRef>-->
          <!-- 把整个项目,压缩成 tar.bz2/tar.gz/zip 三种格式的文件 -->
          <!--<descriptorRef>project</descriptorRef>-->

          <!-- 自定义 descriptor -->
          <descriptorRef>pack-assembly</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>xyz.kail.demo.Main</mainClass>
          </manifest>
        </archive>
      </configuration>
    </execution>
  </executions>
</plugin>

src/main/resources/assemblies/pack-assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

  <id>pack-assembly</id>

  <formats>
    <format>jar</format>
  </formats>

  <includeBaseDirectory>false</includeBaseDirectory>

  <dependencySets>

    <dependencySet>
      <unpack>false</unpack>
      <scope>runtime</scope>
      <outputDirectory>lib</outputDirectory>
      <excludes>
        <exclude>${groupId}:${artifactId}</exclude>
      </excludes>
    </dependencySet>

    <dependencySet>
      <unpack>true</unpack>
      <includes>
        <include>${groupId}:${artifactId}</include>
      </includes>
    </dependencySet>

  </dependencySets>

</assembly>

Read More