Maven 多配置文件的使用

文章目录

  • [1. 一般常见的打包方式如下(全量包FatJar)](#1. 一般常见的打包方式如下(全量包FatJar))
  • [2. 增量包(ThinJar)](#2. 增量包(ThinJar))

1. 一般常见的打包方式如下(全量包FatJar)

  • FatJar打包时,所有依赖的jar包会一并打包,并且打包好的jar可以直接使用java -jar执行,一般来说整个可执行jar包大约会有几十上百兆不等。
xml 复制代码
<build>
    <plugins>
        <!-- 默认的打包插件,用来打普通的project JAR包 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>default-jar</id>
                    <phase>package</phase>
                    <configuration>
                        <excludes>
                            <exclude>config/**</exclude>
                            <exclude>*.yml</exclude>
                            <exclude>logback-spring.xml</exclude>
                            <exclude>banner.txt</exclude>
                        </excludes>
                        <classifier>assembly</classifier>
                    </configuration>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
                <execution>
                    <id>full-jar</id>
                    <phase>package</phase>
                    <configuration>
                        <classifier>full</classifier>
                    </configuration>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- 将Spring Boot应用打包为可执行的jar或war文件 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>default</id>
                    <phase>package</phase>
                    <configuration>
                        <classifier>assembly</classifier>
                    </configuration>
                    <goals>
                        <goal>build-info</goal>
                        <goal>repackage</goal>
                    </goals>
                </execution>
                <execution>
                    <id>full-jar</id>
                    <phase>package</phase>
                    <configuration>
                        <classifier>full</classifier>
                    </configuration>
                    <goals>
                        <goal>build-info</goal>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- 支持自定义的打包结构,也可以定制依赖项等,该项目的对应配置文件在src/assembly下 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2. 增量包(ThinJar)

  • 相比于FatJar,ThinJar会将依赖的jar包拷贝到业务jar包外部,这样业务jar包的大小会非常小,并且支持jar包或文件级别的升级
xml 复制代码
<profiles>
  <profile>
	  <!--标识符,用于给这个 Maven Profile 命名-->
	  <id>fat-jar</id>
	  <properties>
		  <packaging.type>jar</packaging.type>
	  </properties>
	  <activation>
		  <!--Profile 在没有指定其它激活条件时,会自动激活,
		  Maven 会在构建时默认使用这个 Profile。
		  通常这种情况适用于应用程序的常规构建配置-->
		  <activeByDefault>true</activeByDefault>
	  </activation>

	  <build>
		  <plugins>
			  <!--这段配置主要是通过 maven-jar-plugin 插件生成 JAR 文件时的细节控制。
			  它定义了两个执行目标:default-jar 和 full-jar,分别生成不同类型的 JAR 文件:
			 1、default-jar 生成一个名为 myproject-assembly.jar 的 JAR 文件,并排除了特定文件(如 config/**、.yml 文件等)。
			 通常用于构建一个轻量级的 JAR 文件,不包含不必要的配置文件(如 .yml 文件等)
			 2、full-jar 生成一个名为 myproject-full.jar 的完整 JAR 文件,包含所有依赖和文件
			 用于构建一个完整的 JAR 文件,通常包含所有的依赖和资源文件,适用于需要完整打包的场景
			  -->
			  <plugin>
				  <!--Maven 用来创建 JAR 文件的官方插件-->
				  <groupId>org.apache.maven.plugins</groupId>
				  <artifactId>maven-jar-plugin</artifactId>
				  <configuration>
					  <!--配置 JAR 文件的归档行为,主要是关于 MANIFEST.MF 文件的配置-->
					  <archive>
						  <!--用于配置 JAR 文件的 MANIFEST.MF 文件,这个文件存储了 JAR 的元数据-->
						  <manifest>
							  <!--设置为 true 后,会自动将 JAR 的实现版本和实现标题等信息添加到 MANIFEST.MF 中-->
							  <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
							  <!--设置为 true 后,会将 JAR 的规范版本和规范标题等信息添加到 MANIFEST.MF 中-->
							  <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
						  </manifest>
					  </archive>
				  </configuration>
				  <!--执行配置-->
				  <executions>
					  <execution>
						  <!--执行步骤的唯一标识符-->
						  <id>default-jar</id>
						  <!--指定插件执行的生命周期阶段,这里是在 package 阶段执行-->
						  <phase>package</phase>
						  <!--配置该执行的参数-->
						  <configuration>
							  <!--配置哪些文件或目录不应该被包含在 JAR 文件中。这里排除了 config/** 目录、
							  所有 .yml 文件、logback-spring.xml 和 banner.txt 文件-->
							  <excludes>
								  <exclude>config/**</exclude>
								  <exclude>*.yml</exclude>
								  <exclude>logback-spring.xml</exclude>
								  <exclude>banner.txt</exclude>
							  </excludes>
							  <!--设置为 assembly,这意味着生成的 JAR 文件将被标记为
							   -assembly.jar。例如,构建过程中可能会生成一个名为 myproject-assembly.jar 的文件。-->
							  <classifier>assembly</classifier>
						  </configuration>
						  <!--指定该执行的目标,这里是 jar,表示生成 JAR 文件-->
						  <goals>
							  <goal>jar</goal>
						  </goals>
					  </execution>
					  <!--full-jar 执行配置-->
					  <execution>
						  <id>full-jar</id>
						  <phase>package</phase>
						  <!--设置为 full,这意味着生成的 JAR 文件将被标记为 -full.jar。
						  例如,构建过程中可能会生成一个名为 myproject-full.jar 的文件。-->
						  <configuration>
							  <classifier>full</classifier>
						  </configuration>
						  <!--目标仍然是 jar,表示该执行也会生成一个 JAR 文件-->
						  <goals>
							  <goal>jar</goal>
						  </goals>
					  </execution>
				  </executions>
			  </plugin>
			  <plugin>
				  <!--通过 default 和 full-jar 两个执行目标,生成了两种分类符不同的 JAR 文件(-assembly.jar 和 -full.jar)-->
				  <!--Spring Boot 提供的官方 Maven 插件,用于支持 Spring Boot 应用程序的构建、打包和相关任务-->
				  <groupId>org.springframework.boot</groupId>
				  <artifactId>spring-boot-maven-plugin</artifactId>
				  <executions>
					  <!--定义了两个执行目标,分别是 default 和 full-jar,均在 Maven 构建生命周期的 package 阶段执行-->
					  <execution>
						  <!--标识此执行目标-->
						  <id>default</id>
						  <!--在打包阶段运行该目标-->
						  <phase>package</phase>
						  <configuration>
							  <!--指定生成的 JAR 文件的分类符,这里为 assembly,因此生成的文件名形如 project-assembly.jar-->
							  <classifier>assembly</classifier>
						  </configuration>
						  <goals>
							  <!--生成构建信息文件(如 build-info.properties),包括版本、构建时间等-->
							  <goal>build-info</goal>
							  <!--将项目重新打包为可执行的 JAR 文件(fat JAR),包含所有依赖和资源-->
							  <goal>repackage</goal>
						  </goals>
					  </execution>
					  <execution>
						  <!--标识此执行目标-->
						  <id>full-jar</id>
						  <!--在打包阶段运行该目标-->
						  <phase>package</phase>
						  <configuration>
							  <!--设置分类符为 full,生成的文件名形如 project-full.jar-->
							  <classifier>full</classifier>
						  </configuration>
						  <goals>
							  <!--生成构建信息文件-->
							  <goal>build-info</goal>
							  <!--将项目重新打包为完整的可执行 JAR 文件-->
							  <goal>repackage</goal>
						  </goals>
					  </execution>
				  </executions>
			  </plugin>
			  <plugin>
				  <!--Maven 官方插件-->
				  <groupId>org.apache.maven.plugins</groupId>
				  <!--用于生成复杂的分发包,比如将 JAR 文件、配置文件、文档等资源打包成 ZIP 或 TAR 文件-->
				  <artifactId>maven-assembly-plugin</artifactId>
			  </plugin>
		  </plugins>
	  </build>
  </profile>

  <profile>
	  <id>thin-jar</id>
	  <properties>
		  <packaging.type>jar</packaging.type>
	  </properties>
	  <build>
		  <plugins>
			  <plugin>
				  <groupId>org.apache.maven.plugins</groupId>
				  <artifactId>maven-jar-plugin</artifactId>
				  <version>3.1.2</version>
				  <configuration>
					  <archive>
						  <manifest>
							  <!--会将实现和规范版本的信息添加到 MANIFEST.MF 文件中-->
							  <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
							  <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
						  </manifest>
					  </archive>
					  <excludes>
						  <!--排除了 config/**(配置目录)、.yml 文件、logback-spring.xml 和 banner.txt 文件-->
						  <exclude>config/**</exclude>
						  <exclude>*.yml</exclude>
						  <exclude>logback-spring.xml</exclude>
						  <exclude>banner.txt</exclude>
					  </excludes>
					  <!--设置为 bootapp,生成的 JAR 文件名会以 -bootapp.jar 结尾-->
					  <classifier>bootapp</classifier>
				  </configuration>

			  </plugin>

			  <plugin>
				  <!--将运行时类路径信息写入瘦 JAR 的 MANIFEST.MF 文件,属性名为 Xdr-Thin-Classpath-->
				  <groupId>org.apache.maven.plugins</groupId>
				  <artifactId>maven-antrun-plugin</artifactId>
				  <version>3.0.0</version>
				  <executions>
					  <execution>
						  <id>write-classpath</id>
						  <phase>package</phase>
						  <configuration>
							  <target>
								  <!--引用 Maven 的运行时类路径-->
								  <property name="runtime_classpath" refid="maven.runtime.classpath"/>
								  <!--目标文件为 -bootapp.jar-->
								  <jar destfile="${project.build.directory}/${project.artifactId}-${project.version}-bootapp.jar" update="true">
									  <manifest>
										  <attribute name="Xdr-Thin-Classpath" value="${runtime_classpath}"/>
									  </manifest>
								  </jar>
							  </target>
						  </configuration>
						  <goals>
							  <goal>run</goal>
						  </goals>
					  </execution>
				  </executions>
			  </plugin>
			  <plugin>
				  <groupId>org.springframework.boot</groupId>
				  <artifactId>spring-boot-maven-plugin</artifactId>
				  <configuration>
					  <!--指定无效的依赖项来避免打包实际的依赖文件-->
					  <includes>
						  <include>
							  <groupId>not.exists.groupId</groupId>
							  <artifactId>not.exists.artifactId</artifactId>
						  </include>
					  </includes>
				  </configuration>
				  <executions>
					  <execution>
						  <id>default</id>
						  <phase>package</phase>
						  <configuration>
							  <classifier>bootapp</classifier>
						  </configuration>
						  <goals>
							  <goal>build-info</goal>
							  <goal>repackage</goal>
						  </goals>
					  </execution>
				  </executions>
				  <dependencies>
					<!--引入自定义的打包格式-->
					<!--扩展spring boot maven plugin,重新制定可执行jar包的Launcher类-->
					<dependency>
						<groupId>com.xdr630</groupId>
						<artifactId>xdr-boot-thin-layout</artifactId>
						<version>0.0.1-SNAPSHOT</version>
					</dependency>
				  </dependencies>
			  </plugin>
			  <plugin>
				  <groupId>org.apache.maven.plugins</groupId>
				  <artifactId>maven-assembly-plugin</artifactId>
				  <version>3.1.1</version>
				  <executions>
					  <execution>
						  <phase>package</phase>
						  <goals>
							  <goal>single</goal>
						  </goals>
					  </execution>
				  </executions>
				  <configuration>
					  <descriptors>
						  <descriptor>src/assembly/assembly_thin.xml</descriptor>
					  </descriptors>
					  <outputDirectory>target</outputDirectory>
				  </configuration>
			  </plugin>
		  </plugins>
	  </build>
  </profile>

  <profile>
	  <id>war</id>
	  <properties>
		  <packaging.type>war</packaging.type>
	  </properties>

	  <dependencies>
		  <dependency>
			  <groupId>org.springframework.boot</groupId>
			  <artifactId>spring-boot-starter-tomcat</artifactId>
			  <scope>provided</scope>
		  </dependency>
	  </dependencies>

	  <build>
		  <finalName>myproject</finalName>

		  <plugins>
			  <plugin>
				  <groupId>org.apache.maven.plugins</groupId>
				  <artifactId>maven-war-plugin</artifactId>
				  <version>3.1.0</version>
				  <configuration>
					  <failOnMissingWebXml>false</failOnMissingWebXml>
					  <archive>
						  <manifestEntries>
							  <Dependencies>jdk.unsupported</Dependencies>
						  </manifestEntries>
					  </archive>
				  </configuration>
			  </plugin>

			  <plugin>
				  <groupId>org.springframework.boot</groupId>
				  <artifactId>spring-boot-maven-plugin</artifactId>
				  <executions>
					  <execution>
						  <id>default</id>
						  <phase>package</phase>
						  <goals>
							  <goal>build-info</goal>
							  <goal>repackage</goal>
						  </goals>
					  </execution>
				  </executions>
			  </plugin>
		  </plugins>
	  </build>
  </profile>
</profiles>

<distributionManagement>
  <repository>
	  <id>maven-release</id>
	  <name>Nexus Release Repository</name>
	  <url>http://xxx:8081/repository/maven-releases/</url>
  </repository>

  <snapshotRepository>
	  <id>maven-snapshots</id>
	  <name>Nexus Release Repository</name>
	  <url>http://xxx:8081/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>
  • 注意:这里一定要指定maven-assembly-plugin插件的版本为3.1.1+,否则会出现依赖包打包不正确的bug
xml 复制代码
<?xml version='1.0' encoding='UTF-8'?>
<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">

    <!-- 打包完成后文件名以"-bin"结尾 -->
    <id>bin</id>
    <formats>
        <!-- 压缩格式为tar.gz -->
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <!-- 文件拷贝 -->
    <fileSets>
        <!-- bin目录: 拷贝运维脚本(Windows) -->
        <fileSet>
            <directory>${project.basedir}/src/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*_thin.bat</include>
                <include>shutdown.bat</include>
            </includes>
            <lineEnding>dos</lineEnding>
        </fileSet>
        <!-- bin目录: 拷贝运维脚本(Linux)-->
        <fileSet>
            <directory>${project.basedir}/src/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*_thin.sh</include>
                <include>shutdown.sh</include>
            </includes>
            <lineEnding>unix</lineEnding>
            <fileMode>0755</fileMode>
        </fileSet>
        <!-- conf目录: 从编译目录拷贝配置文件 -->
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <!-- 若有漏掉的配置文件,可以在这里添加规则 -->
                <include>config/**</include>
                <include>*.yml</include>
                <include>logback-spring.xml</include>
                <include>banner.txt</include>
            </includes>
        </fileSet>
        <!-- lib/bootapp目录: 保存业务jar包,加载jar包时首先从bootapp目录加载 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>lib/bootapp</outputDirectory>
            <includes>
                <include>*-bootapp.jar</include>
            </includes>
        </fileSet>
    </fileSets>

    <!-- 依赖包拷贝 -->
    <dependencySets>
        <!-- lib/xdr目录: 拷贝xdr相关的包 -->
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib/xdr</outputDirectory>
            <outputFileNameMapping>${artifact.artifactId}-${artifact.baseVersion}.${artifact.extension}</outputFileNameMapping>
            <includes>
                <!-- groupId:artifactId,支持通配符 -->
              	<include>com.xdr630*:*</include>
            </includes>
        </dependencySet>
        <!-- lib/spring目录: 拷贝spring相关的包 -->
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib/spring</outputDirectory>
            <outputFileNameMapping>${artifact.artifactId}-${artifact.baseVersion}.${artifact.extension}</outputFileNameMapping>
            <includes>
                <include>org.springframework*:*</include>
            </includes>
        </dependencySet>
        <!-- lib/others目录: 拷贝无法归类的包相关的包 -->
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib/others</outputDirectory>
            <outputFileNameMapping>${artifact.artifactId}-${artifact.baseVersion}.${artifact.extension}</outputFileNameMapping>
            <excludes>
                <exclude>org.springframework*:*</exclude>
                <exclude>com.xdr630*:*</exclude>
            </excludes>
        </dependencySet>
        <!-- 如果还有自定义的分包规则,可以在这里添加 -->
    </dependencySets>
</assembly>
相关推荐
毕设源码-钟学长5 小时前
【开题答辩全过程】以 餐健一体化管理系统为例,包含答辩的问题和答案
java·eclipse
摇滚侠5 小时前
Spring Boot3零基础教程,整合 SSM,笔记52
java·spring boot·笔记
毕设源码-朱学姐6 小时前
【开题答辩全过程】以 查寝打卡系统为例,包含答辩的问题和答案
java·eclipse
QMY5205206 小时前
爬虫的意义
java·spring·tomcat·maven
lang201509287 小时前
Spring Boot Actuator深度解析与实战
java·spring boot·后端
懒羊羊不懒@7 小时前
Java—枚举类
java·开发语言·1024程序员节
zerolala7 小时前
Java容器常用方法
java·1024程序员节
m0_748240257 小时前
C++智能指针使用指南(auto_ptr, unique_ptr, shared_ptr, weak_ptr)
java·开发语言·c++
lang201509287 小时前
Spring注解配置全解析
java·后端·spring