Maven使用assembly自定义打包

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

打包效果如下:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!如果您想使用 Mavenmaven-assembly-plugin 插件打包 Mapper,您可以按照以下步骤进行操作: 1. 首先,在您的 Maven 项目的 pom.xml 文件中,确保已经添加了 maven-assembly-plugin 插件的配置。示例如下: ```xml <build> <plugins> <!-- 其他插件配置... --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <!-- 插件的配置参数 --> <!-- 您可以根据您的需求进行配置 --> </configuration> <executions> <execution> <id>assemble</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2. 接下来,您需要在 pom.xml 文件中添加相关的依赖项。请确保您已经添加了与 Mapper 相关的依赖项,例如 MyBatis 或其他 ORM 框架。 3. 在配置 maven-assembly-plugin 插件时,您可以使用 `<configuration>` 标签来指定如何打包 Mapper。以下是一个示例配置,假设您的 Mapper 文件位于 `src/main/resources/mapper` 目录下: ```xml <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.MainClass</mainClass> </manifest> </archive> <appendAssemblyId>false</appendAssemblyId> <finalName>my-project</finalName> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> <outputDirectory>${project.build.directory}/assembly</outputDirectory> </configuration> ``` 请注意,上述示例中的 `assembly.xml` 是一个自定义的 XML 文件,用于指定要打包的文件和目录。您可以根据自己的需求进行配置。 4. 创建一个名为 `assembly.xml` 的 XML 文件,并将其放置在 `src/main/assembly` 目录下。在该文件中,您可以使用 `<fileSets>` 或其他相关标签来指定要打包的 Mapper 文件或目录。以下是一个示例配置: ```xml <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.2 http://maven.apache.org/xsd/assembly-2.1.2.xsd"> <id>assembly</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.outputDirectory}</directory> <outputDirectory>/</outputDirectory> </fileSet> <!-- 其他 fileSet 配置... --> </fileSets> </assembly> ``` 请注意,上述示例中的 `<directory>` 标签指定了 Mapper 文件所在的目录,`<outputDirectory>` 标签指定了打包后的存放位置。 5. 最后,在命令行中执行以下 Maven 命令来进行打包: ``` mvn clean package ``` 这将触发 maven-assembly-plugin 插件的执行,并生成包含 Mapper 的打包文件。 希望能对您有所帮助!如有任何疑问,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值