深入探讨Maven打包:打造精致的Zip包

在部署Spring Boot生产项目时,首先需要使用Maven将其打包成一个JAR文件或者包含JAR文件的ZIP文件。随后,通过堡垒机将打包好的文件上传至服务器进行部署,或构建成Docker镜像进行发布。在这一过程中,我们需要将项目中的配置文件或静态资源放置在JAR包之外,这样在修改配置文件或静态资源时就无需重新打包。为了构建ZIP文件,我们使用了Maven Assembly Plugin插件,该插件用于创建项目分发包,通过一个XML描述文件定义打包内容。本文将详细介绍这一流程。

6fe38d46bf0f4555b05931b6c27c5793_3.png

maven-assembly-plugin简介

maven-assembly-plugin 是一个 Maven 插件,用于创建项目分发包。该插件可以帮助将项目的构建输出、依赖库等资源打包成一个归档文件,如 JAR、ZIP、TAR 等,方便项目的部署和发布。

以下是 maven-assembly-plugin 的一些主要特点和用途:

  • 灵活的配置:通过配置插件,可以定义不同的打包方式和内容,满足各种项目的需求。可以选择包含依赖库、特定文件、目录结构等。

  • 多种打包格式:支持生成不同格式的归档文件,如 JAR 包含所有依赖、ZIP 压缩文件、TAR 文件等,可以根据实际情况选择合适的打包格式。

  • 自定义描述符:除了预定义的描述符外,还可以编写自定义描述符文件,以更精细的方式控制打包过程,定义需要包含的文件、目录结构等。

  • 插件执行阶段:通常在 Maven 构建的某个阶段(如 package 阶段)触发执行,生成所需的分发包。

  • 简化部署流程:打包成归档文件后,可以方便地进行部署和发布,简化了项目的交付流程。

maven-assembly-plugin 配置

让我们来看一下 Maven Assembly Plugin 的配置文件 package.xml,这个文件定义了项目打包的内容和形式。在这个 XML 文件中,我们定义了项目打包的格式为 zip 和 dir,同时指定了一些文件和目录的打包规则。

package.xml


<?xml version="1.0" encoding="UTF-8"?>
<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>package</id>
	<formats>
		<format>zip</format>
    <format>dir</format>
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <excludes>
                <exclude>${groupId}:${artifactId}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
	<fileSets>
		<!-- 启动脚本文件打包 -->
		<fileSet>
			<directory>bin/</directory>
			<outputDirectory>./</outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
			<fileMode>0755</fileMode>
			<lineEnding>unix</lineEnding>
		</fileSet>
		<!--资源文件打包 -->
		<fileSet>
			<directory>src/main/resources</directory>
			<outputDirectory>./</outputDirectory>
		</fileSet>
		<!-- 启动jar文件打包 -->
		<fileSet>
			<directory>${project.build.directory}</directory>
			<outputDirectory>./</outputDirectory>
			<includes>
				<include>*.jar</include>
			</includes>
		</fileSet>
	</fileSets>
</assembly>

在 package.xml 中,我们还可以定义需要打包的依赖库、启动脚本文件、静态资源文件以及启动 jar 文件等内容。也可以在通过来排除一些我们不需要的资源, 通过这个 XML 文件,我们可以灵活地配置项目打包的内容和结构。

Maven Build 配置

在项目的POM文件中,我们还需要引入maven-assembly-plugin插件,并指定其配置文件。由于我们需要将配置文件和静态资源放置在JAR包之外,因此在打包过程中需要排除这些文件。Maven的配置示例如下:

	<build>
		<!--<finalName>${project.artifactId}-${project.version}</finalName>-->
		<sourceDirectory>src/main/java</sourceDirectory>
		<resources>
			<resource>
				<directory>${project.basedir}/src/main/resources</directory>
				<filtering>true</filtering>
				<excludes>
					<exclude>*</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>${project.basedir}/src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>3.1.0</version>
				<configuration>
					<encoding>UTF-8</encoding>
					<nonFilteredFileExtensions>
						<nonFilteredFileExtension>xls</nonFilteredFileExtension>
						<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
						<nonFilteredFileExtension>csv</nonFilteredFileExtension>
					</nonFilteredFileExtensions>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.1.0</version>
				<configuration>
					<archive>
						<!-- 添加index则不从mainfest中读取classpath,而是从Index.list中读取
                        <index>true</index> -->
						<manifest>
							<mainClass>cn.xj.snowflake.SnowflakeApplication</mainClass>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
							<packageName>${project.artifactId}-${project.version}</packageName>
						</manifest>
					</archive>
					<excludes>
						<exclude>config/**</exclude>
						<exclude>mapper/**</exclude>
						<exclude>mybatis/**</exclude>
						<exclude>static/**</exclude>
						<exclude>templates/**</exclude>
						<exclude>i18n/**</exclude>
						<exclude>lib/**</exclude>
						<exclude>vm/**</exclude>
						<exclude>*.txt</exclude>
						<exclude>*.xml</exclude>
						<exclude>*.properties</exclude>
						<exclude>*.yml</exclude>
						<exclude>*.properties</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy</id>
						<phase>compile</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib</outputDirectory>
							<overWriteReleases>false</overWriteReleases>
							<overWriteSnapshots>false</overWriteSnapshots>
							<overWriteIfNewer>true</overWriteIfNewer>
							<includeScope>compile</includeScope>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptors>
						<descriptor>conf/package.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

在 Maven Build 的配置中,我们引入了 maven-resources-plugin、maven-jar-plugin、maven-dependency-plugin 和 maven-assembly-plugin 这几个插件,并对它们进行了相应的配置。

总结

当我们执行 mvn package 命令进行打包时,Maven 会按照配置文件 package.xml 和插件的配置来生成最终的分发包。Maven Assembly Plugin 会根据配置文件定义的规则将项目的相关文件、依赖库等内容打包成指定的格式,并完成最终的分发包构建。

通过以上的配置和打包过程,我们可以很方便地使用 Maven 来管理项目的打包和发布,而不需要手动进行复杂的文件拷贝和整理工作。

  • 18
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修己xj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值