MAVEN构建分离依赖JAR

MAVEN构建分离依赖JAR

1. 背景说明

在Springboot项目中,项目构建时,默认打包成一个可以执行的jar包.导致单一jar过大.项目部署过程中,需要把依赖的jar包和配置文件都单独存放到指定的文件夹中.

2. 插件配置

  1. maven-compiler-plugin 用于编译java代码
  2. maven-jar-plugin 用于构建jar包
  3. spring-boot-maven-plugin 用于构建Springboot项目
  4. maven-assembly-plugin 用于打包项目

3. 编译配置

xml 复制代码
<!--小游戏 地心侠士 -->
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <configuration>
     <annotationProcessorPaths>
      	<path>
      		<groupId>org.projectlombok</groupId>
      		<artifactId>lombok</artifactId>
      		<version>${lombok.version}</version>
      </path>
     </annotationProcessorPaths>
   </configuration>
</plugin>

4. 构建jar配置

xml 复制代码
<!--小游戏 地心侠士 -->
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <configuration>
      <archive>
        <manifest>
      	 <useUniqueVersions>false</useUniqueVersions>
         <mainClass>com.herbert.Application</mainClass>
        </manifest>
      </archive>
      <excludes>
      	<exclude>*.properties</exclude>
      	<exclude>*.yml</exclude>
      	<exclude>**/mapper/*.xml</exclude>
      </excludes>
   </configuration>
</plugin>

5. 构建springboot项目配置

其中主要配置 <layout>ZIP\</layout> 为关键配置,打包后的jar包对应MANIFEST.MF配置的启动类为 Main-Class: org.springframework.boot.loader.PropertiesLauncher

xml 复制代码
<!--小游戏 地心侠士 公众号:小满小慢 QQ:464884492-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
	<include>
		<groupId>non-exists</groupId>
		<artifactId>non-exists</artifactId>
	</include>
</includes>
<excludes>
	<exclude>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
	</exclude>
</excludes>
</configuration>
</plugin>

5. 项目整体打包配置

xml 复制代码
<!--小游戏 地心侠士 公众号:小满小慢 QQ:464884492-->
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-assembly-plugin</artifactId>
   <configuration>
      <appendAssemblyId>false</appendAssemblyId>
      <descriptors>
      	<descriptor>./package.xml</descriptor>
      </descriptors>
      <outputDirectory>${project.basedir}/dist</outputDirectory>
   </configuration>
   <executions>
      <execution>
      	<id>make-assembly</id>
      	<phase>package</phase>
      	<goals>
      		<goal>single</goal>
      	</goals>
      </execution>
   </executions>
</plugin>

其中 package.xml文件内容如下

xml 复制代码
<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>distribution</id>
	<formats>
		<format>zip</format>
	</formats>
    <!--小游戏 地心侠士 公众号:小满小慢 QQ:464884492-->
	<includeBaseDirectory>false</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>src/main/resources/</directory>
			<outputDirectory>/resources</outputDirectory>
			<filtered>true</filtered>
		</fileSet>
		<fileSet>
			<directory>${project.basedir}/script</directory>
			<outputDirectory>/</outputDirectory>
			<filtered>true</filtered>
		</fileSet>
		 <fileSet>
		    <directory>${project.basedir}/web/</directory>
			<outputDirectory>/resources/static</outputDirectory>
		 </fileSet>
	</fileSets>
	<dependencySets>
	  <dependencySet>
			<outputDirectory>/lib</outputDirectory>
			<scope>runtime</scope>
			<excludes>
				<exclude>${project.groupId}:${project.artifactId}</exclude>
			</excludes>
		</dependencySet>
		<dependencySet>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>${project.groupId}:${project.artifactId}</include>
			</includes>
		</dependencySet>
	</dependencySets>
</assembly>

fileSets/fileSet/filtered 属性为true时,表示在maven复制文件时,可以把maven的占位符替换成对应的值,比如需要打包文件中包含一个startup.bat文件,项目打包的源文件配置如下:

bat 复制代码
@echo off
title ${project.artifactId}-${project.version}-%date%-%time%-%cd%
java -Dloader.debug=true -jar  ${project.artifactId}-${project.version}.jar

项目打包后,输出文件内容会改变成如下:

bat 复制代码
rem 小游戏 地心侠士 by herbert
@echo off
title herbert.1.0.0-%date%-%time%-%cd%
java -Dloader.debug=true -jar herbert-1.0.0.jar

7. 项目启动问题

根据以上配置后,需要动态指定PropertiesLauncher的loader.path来加载分离后的lib,以及其他资源文件.这里提供两个实用的方法

  1. 通过 java -D指定参数

    java -jar -Dloader.path=resources,lib,plugin hebert-1.0.0-SNAPSHOT.jar

  2. 在jar包同级目录下创建loader.properties文件,并添加如下内容

    loader.path=resources,lib,plugin

相关推荐
Terio_my2 小时前
Spring Boot 缓存集成实践
spring boot·后端·缓存
karry_k2 小时前
JMM与Volatitle
后端
数字化顾问2 小时前
“AMQP协议深度解析:消息队列背后的通信魔法”之核心概念与SpringBoot落地实战
开发语言·后端·ruby
武子康3 小时前
大数据-115 - Flink DataStream Transformation Map、FlatMap、Filter 到 Window 的全面讲解
大数据·后端·flink
用户4099322502123 小时前
转账不翻车、并发不干扰,PostgreSQL的ACID特性到底有啥魔法?
后端·ai编程·trae
程序新视界3 小时前
三种常见的MySQL数据库设计最佳实践
数据库·后端·mysql
LunarCod4 小时前
Hexo搭建/部署个人博客教程
后端·hexo·个人博客·vercel
IT_陈寒5 小时前
Vue 3.4 实战:这7个Composition API技巧让我的开发效率飙升50%
前端·人工智能·后端
风雨同舟的代码笔记6 小时前
ThreadLocal的使用以及源码分析
后端
brzhang6 小时前
把网页的“好句子”都装进侧边栏:我做了个叫 Markbox 的收藏器,开源!
前端·后端·架构