使用的 Maven Assembly 插件,它用于创建自定义的归档文件,通常用于创建包含依赖的可执行 JAR 文件
项目的工程目录以及如下assembly.xnl文件、pom.xml存放如下
pom文件中配置Maven打包插件
<plugin>
<!-- package all artifacts -->
<!-- 指定了要使用的 Maven Assembly 插件,它用于创建自定义的归档文件,通常用于创建包含依赖的可执行 JAR 文件-->
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- 指定是否将装配 ID 附加到最终的归档文件名中。在这里,设置为 false 表示不附加 -->
<appendAssemblyId>false</appendAssemblyId>
<!-- descriptors:指定了一个或多个描述符文件,用于定义如何构建归档文件 -->
<descriptors>
<!-- descriptor:这里指定了一个名为 assembly.xml 的描述符文件,用于定义如何打包归档文件 -->
<descriptor>assembly.xml</descriptor>
</descriptors>
<!-- 指定了输出目录,即最终生成的归档文件将被放置在 ${project.build.directory}(通常是 target 目录)中 -->
<outputDirectory>${project.build.directory}</outputDirectory>
<!-- 指定了最终生成的归档文件的名称。这里使用了 ${project.parent.artifactId},表示使用父项目的 artifactId 作为最终的文件名 -->
<finalName>${project.parent.artifactId}</finalName>
</configuration>
<!-- 这部分定义了插件的执行配置 -->
<executions>
<!--指定了一个执行阶段-->
<execution>
<!--指定了执行的 ID-->
<id>make-assembly</id>
<!-- 指定了插件的执行阶段,在 Maven 生命周期中,package 阶段用于打包项目 -->
<phase>package</phase>
<!--指定了要执行的目标-->
<goals>
<!-- 指定了要执行的目标为 single,表示只执行一次装配操作 -->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
assembly文件具体含义
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<!-- 可自定义,这里指定的是项目环境 -->
<!-- 定义了该装配的标识符。在这里,它被命名为 "assembly" -->
<id>assembly</id>
<!-- 打包的类型,如果有N个,将会打N个类型的包 -->
<!-- <format>tar.gz</format>-->
<formats>
<format>zip</format>
</formats>
<!-- 指定是否在创建的装配件中包含基本目录。在这里,它设置为 "true",表示要包含基本目录 -->
<includeBaseDirectory>true</includeBaseDirectory>
<!-- 定义了要包含在装配件中的文件集合。每个 <fileSet> 元素描述了一个文件集合 -->
<fileSets>
<!--
0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
0644->即用户具有读写权限,组用户和其它用户具有只读权限;-->
<!-- 将src/main/scripts目录下的所有文件输出到打包后的根目录中 -->
<fileSet>
<!--指定了要包含的文件所在的目录-->
<directory>src/main/scripts</directory>
<!--指定要包含的文件的通配符模式-->
<includes>
<include>*.sh</include>
</includes>
<!--指定了文件在装配件中的输出目录-->
<outputDirectory>/</outputDirectory>
<!--(可选)指定了文件在装配件中的权限模式-->
<fileMode>755</fileMode>
</fileSet>
<!-- 指定输出src/main/resources中的配置文件到conf目录中 -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>conf/</outputDirectory>
</fileSet>
<!-- 将项目启动jar打包到根目录中(adapter_boot-1.0.0.jar) -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<!--定义了要包含在装配件中的依赖项集合-->
<dependencySets>
<dependencySet>
<!--指定了依赖项在装配件中的输出目录-->
<outputDirectory>lib/</outputDirectory>
<!-- 指定了依赖项的作用范围-->
<scope>runtime</scope>
<!--(可选)指定了需要排除的依赖项-->
<excludes>
<exclude>${project.name}-${project.version}</exclude>
<exclude>${groupId}:${artifactId}</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
打包效果如下: