Spring Boot gradle 打包瘦身减少jar包体积 含demo

news/2024/5/20 1:15:49 标签: Spring Boot, Gradle, bootRepackage, jar, bootJar

Spring Boot 1.5.x 打包

demo项目地址
https://gitee.com/youngboyvip/package-example

解决问题

Spring Boot 项目包太大上传服务器费时

优化原理

把占空间的 lib 目录移到外面去,打包时指定loaderPropertiesLauncher,启动时使用loader.path制定 lib 目录的位置

参考文档
bootReapckage 文档67.3 Packaging executable jar and war files 章节 点我
Spring Boot Loader 原理 E.1 Nested JARs 章节 点我

gradle task写法

Spring 1.5.x版本适用 2.x版本不适用

configurations {
    mycustomconfiguration //单独配置configuration
}

dependencies {
    //给mycustomconfiguration 配置依赖,如果你不配那lib/中就不会有jar包
    mycustomconfiguration "org.liquibase:liquibase-core" 
}

//复制依赖jar包的任务,会在build/libs 目录下面生成lib.zip文件
task copyJars(type: Zip) {
    from(configurations.compile) 
    destinationDir file("build/libs") 
    archiveName "lib.zip"
}

task clientJar(type: Jar) {
    appendix = 'slim'
    from sourceSets.main.output 
    exclude('.*/lib/**') 
    manifest {
        attributes 'Main-Class':'org.springframework.boot.loader.PropertiesLauncher'
    }
}

task bootJar(type: BootRepackage, dependsOn: clientJar) {
    springBoot.layout = 'zip'
    withJarTask = clientJar 
    customConfiguration = "mycustomconfiguration"
}

打包命令 执行bootJar 就ok了

启动方法

STEP1: 解压lib.zip 文件到lib文件夹

STEP2: 执行命令

java -Dloader.path="lib/,WEB-INF/classes" -jarjar包的名字

Spring Boot 2.x 打包

使用spring-boot-thin-launcher插件打包SpringBoot项目

github地址传送门

优化原理

使用 ThinJarWrapper 装载器从jarpom.xml/META-INF/mavenMETA-INF/thin.properties路径中解析出依赖的jar包,

然后从本地仓库或远程仓库拉取jar包,然后使用ThinJarLauncher 运行 Main 函数

使用说明

运行 thinJar 任务打包

运行 java -jar jar包名 启动

注意事项

别忘了配置 apply plugin: 'maven-publish' 插件,不然不会生成pom文件

文档参考

文档

maven 项目参考

使用maven插件即可
文档


http://www.niftyadmin.cn/n/747279.html

相关文章

Thymeleaf 添加 baseUrl 指定根路径

转载自 www.youngboy.vip 原理 修改{}对应的处理类StandardLinkBuilder重写computeContextPath方法 修改步骤 step1: 重写 StandardLinkBuilder public static class BaseUrlLinkBuilder extends StandardLinkBuilder {private String baseUrl;public void setBaseUrl(Stri…

Cloudera Manager Server软件安装时注意事项之一 (转载自Cloudera Manager 官方文档Cloudera Manager Installation Gui)

转载自www.cloudera.com 通过上述内容可以看出Cloudera Manager Server软件是需要一个数据库来支撑它的,可用其自己内嵌的postgre sql数据库,也可以自己安装一个数据库如:mysql供Cloudera Manager Server(cm server)来使用 具体的格式参数说明&#xff…

Java http长轮巡模拟扫码登录

demo 地址 https://gitee.com/youngboyvip/longpoll 实现原理 使用Servlet3异步接口实现非阻塞长连接接口, 异步上下文对象使用 ScheduledExecutorService 线程池定时调度 事件总线使用了 guava 中的 EventBus 实现 curl 执行顺序 //发起长链接请求 curl -X GET …

postgrel建表语句

CREATE TABLE public.user_roles ( id integer NOT NULL, username character(100), role_name character(10), CONSTRAINT user_roles_pkey PRIMARY KEY (id) )

impala创建一个外部文件表的例子和一个内部分区表的例子,向内部分区表插入数据例子

本例中以在 HDFS上的存储格式为TEXTFILE格式为例子 ———————————————————————————————————————— 用impala 创建一个内部分区表的例子 ——---------------------------------------------------------------------------------------…

Spring Cloud Sleuth中集成Zipkin

在Spring Cloud Sleuth中集成Zipkin非常的简单&#xff0c;只需要引入相应的依赖和做相关的配置即可 1、引入依赖 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></depende…

cloudera manager agent 传说中cm agent的作用

CMCloudera Manager Agent 即 cm 代理进程它是和cm server进程通信的一个对端进程&#xff0c;目的是让安装了cm agent进程的那台 hadoop环境主机或者是impalad主机 定期上报自己的相关hadoop组件进程或impalad进程的工作状态数据&#xff0c;也是cm server下发 安装hadoop生…

poi word 删除表格

poi 删除表格&#xff0c;docx 版 /*** 删除表格* param table 表格对象*/public static void deleteTable(XWPFTable table){List<XWPFTableRow> rows table.getRows();int rowLength rows.size();for (int i 0; i < rowLength; i) {table.removeRow(0);}}