使用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>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven-assembly-plugin是一个用于打包的插件,可以将项目中的多个模块合并成一个可执行的JAR包或者WAR包。然而,有时候我们可能需要对已经打包好的JAR或者WAR包进行进一步的处理或者外部打包。以下是关于maven-assembly-plugin外部打包的解答。 首先,我们需要确保已经正确配置了maven-assembly-plugin,并且在项目的pom.xml文件中已经定义好了自定义的assembly描述文件(assembly.xml)。 在进行外部打包之前,我们需要先将项目进行构建和打包。在项目根目录下通过命令行输入"mvn clean install"命令来构建项目,并且将项目打包成JAR或者WAR文件。 接下来,在项目根目录下创建一个新的文件夹(例如,external_package),用于存放外部打包的文件。 然后,可以通过以下命令来进行外部打包: mvn assembly:assembly -Ddescriptor=assembly.xml -DoutputDirectory=/path/to/external_package 在上面的命令中,-Ddescriptor参数用于指定assembly描述文件所在的路径以及文件名称,-DoutputDirectory参数用于指定外部打包文件存放的路径。 执行完上述命令后,maven-assembly-plugin会根据assembly.xml文件的定义,在指定的路径下生成外部打包文件。 通过以上步骤,我们就可以实现maven-assembly-plugin的外部打包功能。需要注意的是,我们在进行外部打包之前,需要先正确配置和打包项目,并且确保已经定义了正确的assembly描述文件。 外部打包可以用于将项目的可执行文件(如JAR或者WAR文件)与其它资源(如配置文件或者依赖库)合并打包成一个单独的压缩文件,方便部署和分发。它可以帮助我们将项目打包成一个易于使用和发布的形式,简化部署流程,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值