maven springboot如何将jar包打包到指定目录
作者:烤鸭的世界我们不懂 发布时间:2022-12-09 00:40:25
如何将jar包打包到指定目录
今天分享一下springboot将jar包打包到指定目录下。
由于之前上线都是一个打包到一个jar,由于服务多了,1个包100多M,哪怕是小版本上线都需要重新上传jar包。
1.目的
将不常用的比如spring,druid等不常用打包到lib目录,这样每次上线不需要上传这些。第三方或者常改动的还打包到本身的jar包内,每次上线都会新打包。
这样原来的100多M的jar包,可以变成2、3M。
如图所示:
原来的打包方式
改后的方式:
2.修改pom
简单解释一下,includes标签内就是打入jar包第三方jar。将上面的2M的包解压缩后,就是includes的包。
如图所示。
excludeGroupIds和excludeArtifactIds 是配置不在lib目录里的包,由于java启动加载的机制是优先加载jar包,再加载外部目录,如果jar包都存在两个地方,这样配置就没有意义了,每次还是得重新发布lib目录,所以将includes中的包,再在excludeGroupIds 和 excludeArtifactIds 配置上。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.5.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
<include>
<groupId>com.etc</groupId>
<artifactId>etc-manage-api</artifactId>
</include>
<include>
<groupId>com.etc</groupId>
<artifactId>etc-manage-core</artifactId>
</include>
<include>
<groupId>com.etc</groupId>
<artifactId>etc-manage-rpc-api</artifactId>
</include>
<include>
<groupId>com.sinoiov.etc.apollo</groupId>
<artifactId>apollo-spring-boot-starter</artifactId>
</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<excludeGroupIds>
com.sinoiov.etc.apollo
</excludeGroupIds>
<excludeArtifactIds>
etc-manage-api,etc-manage-core,etc-manage-rpc-api
</excludeArtifactIds>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.修改启动脚本
原脚本
java -jar etc-manage-service-basic-2.2.0.jar
现脚本 (如果相对目录不好用,就用绝对目录试试)
java Dloader.path=../lib -jar etc-manage-service-basic-2.2.0.jar
jar包外指定配置文件及原理
解决方案
修改maven的pom.xml文件
不拷贝资源文件
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
修改打包方式
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
</configuration>
</plugin>
运行
假设application.properties和application-{profile}.properties都在/tmp/temp/config,jar文件在/tmp/temp
java -Dloader.path=file:///tmp/temp/config,demo-1.0.jar -jar demo-1.0.jar
原理
对比jar包中MANIFEST.MF文件在`ZIP配置前后的区别
配置前:
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.chinaunicom.gateway.GatewayApplication
配置后:
Main-Class: org.springframework.boot.loader.PropertiesLauncher
Start-Class: com.chinaunicom.gateway.GatewayApplication
发现是类加载器变了,查看org.springframework.boot.loader包下所有加载器实现:
查看五个类描述:官方文档
JarLauncher
Launcher for JAR based archives. This launcher assumes that dependency jars are included inside a /BOOT-INF/lib directory and that application classes are included inside a /BOOT-INF/classes directory.
WarLauncher
Launcher for WAR based archives. This launcher for standard WAR archives. Supports dependencies in WEB-INF/lib as well as WEB-INF/lib-provided, classes are loaded from WEB-INF/classes.
PropertiesLauncher
Launcher for archives with user-configured classpath and main class via a properties file. This model is often more flexible and more amenable to creating well-behaved OS-level services than a model based on executable jars.
Looks in various places for a properties file to extract loader settings, defaulting to application.properties either on the current classpath or in the current working directory. The name of the properties file can be changed by setting a System property loader.config.name (e.g. -Dloader.config.name=foo will look for foo.properties. If that file doesn't exist then tries loader.config.location (with allowed prefixes classpath: and file: or any valid URL). Once that file is located turns it into Properties and extracts optional values (which can also be provided overridden as System properties in case the file doesn't exist):
loader.path: a comma-separated list of directories (containing file resources and/or nested archives in .jar or .zip or archives) or archives to append to the classpath. BOOT-INF/classes,BOOT-INF/lib in the application archive are always used
loader.main: the main method to delegate execution to once the class loader is set up. No default, but will fall back to looking for a Start-Class in a MANIFEST.MF, if there is one in ${loader.home}/META-INF.
spring-boot-maven-plugin帮助
对zip格式说明
对jar和war说明
来源:https://blog.csdn.net/Angry_Mills/article/details/105024664


猜你喜欢
- Java关于Map的四种取值方式map的主要作用是什么?可以通过创建一个map的实现类 来存放 数据 值 和值的描述 也可以通过描述去取得数
- 一、异步模型的基本概述异步编程的核心是 Task 和 Task<T> 对象,这两个对象对异步操作建模。 它们受关键字 async
- 首先分析一下问题:其实这个红框不是android的bug,把编译模式从eng改成user就可以了,红框只是eng模式debug的时候提示你系
- 本文实例讲述了C#多线程学习之使用线程池进行多线程的自动管理。分享给大家供大家参考。具体如下:在多线程的程序中,经常会出现两种情况:一种情况
- 有时候,我们需要制作一个Word模板文档,然后发给用户填写,但我们希望用户只能在指定位置填写内容,其他内容不允许编辑和修改。这时候我们就可以
- IoC的概念介绍控制反转(IOC)模式(又称DI:Dependency Injection)就是Inversion of Control,控
- 调取钉钉考勤接口的功能公司需要做一个钉钉考勤的页面,让我去写这个功能。结果却比我想象的要麻烦一些!具体是怎么个麻烦呢下面直入正题首先我们找到
- java中String、StringBuffer、StringBuilder是编程中经常使用的字符串类,他们之间的区别也是经常在
- 我们来讲一下自定义组合控件,相信大家也接触过自定义组合控件吧,话不多说,直接干(哈~哈~):大家看到这个觉得这不是很简单的吗,这不就是写个布
- 活锁与死锁活锁活锁同样会发生在多个相互协作的线程间,当他们为了彼此间的响应而相互礼让,使得没有一个线程能够继续前进,那么就发生了活锁。同死锁
- 一、可空类型修饰符(?)C#2.0里面实现了Nullable数据类型//A.比如下面一句,直接定义int为null是错误的,错误提示为无法将
- 基于 springboot+vue 的测试平台(练手项目)开发继续更新。在接口编辑页中点击发送接口请求,除了显示响应体外,还可以显示响应头等
- C#Windows server2016服务器搭建NFS共享文件夹与C#上传图片到共享文件夹nfs共享文件夹实现步骤基于:Windows s
- 本例是利用C#中的性能计数器(PerformanceCounter)监控网络的状态。并能够直观的展现出来涉及到的知识点:Performanc
- 本文为大家分享了Unity实现粒子光效导出成png序列帧的具体代码,供大家参考,具体内容如下这个功能并不是很实用,不过美术同学有这样的需求,
- private void deletefileOrDic(System.IO.DirectoryInfo path)
- 文章目录 简介增量构建自定义inputs和outputs运行时API隐式依赖输入校验自定义缓存方法输入归一化其他使用技巧简介在我们使用的各种
- 概述动态SQL:SQL语句会随着用户输入或外部条件的变化而变化 。例如:我们在做多条件查询的时候,编写SQL语句的查询操作,我们并不知道用户
- 前提最近发现各个频道推荐了很多ULID相关文章,这里对ULID的规范文件进行解读,并且基于Java语言自行实现ULID,通过此实现过程展示U
- 本文基于jdk1.8进行分析关于HashMap的简介,可以参考这篇文章https://www.jb51.net/article/154177