maven · 2020-09-18 0

使用maven-assembly-plugin打包

一、简介

maven-assembly-plugin 是用来帮助打包使用的插件

maven-assembly-plugin 有:

  1. 内置的Assembly DescriptorRefs
  2. 自定义Assembly Descriptors

支持的打包类型:

  1. zip
  2. tar
  3. tar.gz (or tgz)
  4. tar.bz2 (or tbz2)
  5. jar
  6. dir
  7. war

二、内置的Assembly DescriptorRefs

maven-assembly-plugin 有内置的DescriptorRef

  • bin: 类似于默认打包,会将bin目录下的文件打到包中
  • jar-with-dependencies: 会将所有依赖都解压到生成的包中
  • src: 只将源码目录下的文件打包
  • project: 将整个project资源打包
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

三、自定义Assembly Descriptors

1.pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>package-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <commons.version>2.6</commons.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>${commons.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <!-- 描述文件 -->
                        <descriptor>src/assembly/release.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <!-- 绑定到package生命周期 -->
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2.描述文件

release.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 标识符,添加到生成文件名的后缀符 -->
    <id>release</id>

    <!-- 指定打包格式 -->
    <formats>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>

    <!-- 指定打的包是否包含打包层目录 -->
    <includeBaseDirectory>true</includeBaseDirectory>

    <dependencySets>
        <!-- 项目的依赖包 -->
        <dependencySet>
            <!-- 依赖jar包放置的目录 -->
            <outputDirectory>/lib</outputDirectory>
            <!-- 当前项目的构件是否包含在这个依赖集合里 -->
            <useProjectArtifact>false</useProjectArtifact>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>

    <!-- 指定要包含的文件集 -->
    <fileSets>
        <!-- 程序运行的jar包 -->
        <fileSet>
            <!-- 需要打包的项目文件的目录 -->
            <directory>${project.build.directory}</directory>
            <!-- 打包后输出路径 -->
            <outputDirectory>/bin</outputDirectory>
            <includes>
                <include>**/*.jar</include>
            </includes>
            <!-- 文件执行权限 -->
            <fileMode>0755</fileMode>
        </fileSet>

        <!-- 配置文件 -->
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/conf</outputDirectory>
            <includes>
                <include>**/*.properties</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>

    <files>
        <file>
            <source>README.txt</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>

</assembly>