SpringBoot项目瘦身不包含依赖包

1、问题说明

SpringBoot 部署起来虽然简单,如果服务器部署在公司内网,速度还行,但是如果部署在公网(阿里云等云服务器上),部署起来实在头疼:编译出来的 Jar 包很大,如果工程引入了许多开源组件(SpringCloud 等),那就更大了。这个时候如果想要对线上运行工程有一些微调,则非常麻烦。

2、瘦身前的 Jar 包

Tomcat 在部署 Web 工程的时候,可以进行增量更新,SpringBoot 也是可以的~

SpringBoot 编译出来的 Jar 包中,磁盘占用大的,是一些外部依赖库(jar 包),例如:

进入项目工程根目录,执行 mvn clean install 命令,得到的 Jar 包,用压缩软件打开,目录结构如下:

整个 Jar 包 16.2 MB, 但是 BOOT-INF/lib 就占用了将近 16.1 MB:

3、解决方法

3.1、步骤 1: 正常编译 JAR 包,解压出 lib 文件夹

POM 文件如下:

<build>
    <plugins>
        <plugin>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-maven-plugin</artifactId>
	    <configuration>
	        <mainClass>com.myron.demo.DemoApplication</mainClass>
		<layout>ZIP</layout>
	    </configuration>
	    <executions>
	        <execution>
		    <goals>
			<goal>repackage</goal>
		    </goals>
		</execution>
	    </executions>
	</plugin>
    </plugins>
</build>

进入项目根目录,执行命令:mvn clean install

将编译后的 Jar 包解压,拷贝 BOOT-INF 目录下的 lib 文件夹 到目标路径;

3.2、步骤 2: 修改 pom.xml 配置,编译出不带 lib 文件夹的 Jar 包

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.myron.App</mainClass>
                <layout>ZIP</layout>
                <includes> 
                    <include>
                        <groupId>nothing</groupId>
                        <artifactId>nothing</artifactId>
                    </include>  
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
     <plugins>
<build>

配置完成后,再次执行编译:mvn clean install

生成的 Jar 包体积明显变小,如下所示, 外部的 jar 包已经不会被引入了:

3.3、步骤 3: 运行编译后的 Jar 包

将 步骤 1 解压出来的 lib 文件夹、步骤 2 编译的 jar 包放在同一个目录, 运行下面命令:

// pub1:/home/myron/springboot-helloworld % java -jar -Dloader.path=/home/myron/springboot-helloworld/lib /home/myron/springboot-helloworld/springboot-helloworld-0.0.1-SNAPSHOT.jar 

或者在 maven 中输入一下命令导出需要用到的 jar 包

mvn dependency:copy-dependencies -DoutputDirectory=C:\Users\Administrator\Desktop\lib  -DincludeScope=runtime

备注:

将  /home/myron/springboot-helloworld/ 改成实际的路径。
-Dloader.path=lib 文件夹路径
最终目录文件结构是:

├── lib   #lib文件夹
└── springboot-helloworld-0.0.1-SNAPSHOT.jar

3.4、运行结果

1)代码:

2)pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.myron</groupId>
	<artifactId>springboot-helloworld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

	<dependencies>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<!-- <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build> -->
	
	<!-- 步骤 1: 正常编译 JAR 包,解压出 lib 文件夹 -->
	<!-- <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<mainClass>com.myron.demo.DemoApplication</mainClass>
					<layout>ZIP</layout>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build> -->
	
	<!-- 步骤 2: 修改 pom.xml 配置,编译出不带 lib 文件夹的 Jar 包 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<mainClass>com.myron.demo.DemoApplication</mainClass>
					<layout>ZIP</layout>
					<includes>
						<include>
							<groupId>nothing</groupId>
							<artifactId>nothing</artifactId>
						</include>
					</includes>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>

3)发送测试请求:

4)后台日志:

说明

  • 1、通常,一个工程项目架构确定后,引入的 jar 包基本上不会变,改变的大部分是业务逻辑;
  • 2、后面如果需要变更业务逻辑,只需要轻量地编译工程,大大提高项目部署的效率。

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
适合初学 springboot 的同学 --------------------------- maven配置:pom.xml --------------------------- <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> --------------------------- src/main/resources/application.yml --------------------------- spring: # 指定静态资源的路径 resources: static-locations: classpath:/static/,classpath:/views/ thymeleaf: prefix: classpath:/templates/ server: port:8080 context-path:"/" --------------------------- DemoApplication 运行 main 方法即可启动 springboot --------------------------- package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.example.*"}) //指定扫描路径 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } --------------------------- HelloWorldController --------------------------- package com.example.controller; import java.util.HashMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; // @RestController返回的是json @RestController public class HelloWorldController { // http://localhost:8080/hello 返回的是文本"Hello World" @RequestMapping("/hello") public String index() { return "Hello World"; } /** * 本地访问内容地址 :http://localhost:8080/hello3 ;返回的是文本 */ @RequestMapping("/hello3") public String helloHtml(HashMap<String, Object> map) { map.put("hello", "欢迎进入HTML页面"); return "/index"; } } --------------------------- HelloWorldController --------------------------- package com.example.controller; import java.util.HashMap; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class SpringBootController { /** * 本地访问内容地址 :http://localhost:8080/hello2 ; 可访问到 * src/main/resources/views/index.html */ @RequestMapping("/hello2") public String helloHtml(HashMap<String, Object> map) { // 传参数hello到html页面 map.put("hello", "欢迎进入HTML页面"); return "/index"; } } --------------------------- src/main/resources/views/index.html --------------------------- <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> aaaaaaaaaaaaaaaaaaaaaaacccccccccccccc <p th:text="${hello}">dddd</p> </body> </html> --------------------------- 直接访问静态页面 --------------------------- http://localhost:8080/index.html 可直接访问到 src/main/resources/templates/index.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值