springboot的瘦身部署计划lib依赖包分离

星期二, 2020-06-30 | Author: Lee | JAVA-and-J2EE, linux | 736 views

1.springboot目前都趋向于使用jar部署,但是问题是每次打包的

fatjar都比较大,有50M左右,发布上传比较耗时,尤其是服务器在海外的情况更是感觉浪费时间

2.把依赖的lib包剥离出来,单独依赖减少每次的打包大小量

注意:若有新增加的依赖或者调整依赖库的版本需要重新调整lib,切记.

3.开始方法:正常打包 获取BOOT-INF下的lib依赖jar包

mvn clean install

POM文件如下:

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<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>

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

4.移除依赖包的POM 再打包

mvn clean install

POM文件如下:

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<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>

注意:

<layout>ZIP</layout>

layout要选用ZIP否则会ClassNotFoundException

5.启动命令如下
-Dloader.path为依赖加载

-Dspring.config.location 为外置配置文件

  java -Dloader.path=/ia/depend/pomelolee/lib/ -jar -Dspring.config.location=/ia/config/pomelolee/IA.properties /usr/local/webserver/serverless/ia-pomelolee.jar &

6.备注或者依赖包处理

 
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-dependency-plugin</artifactId>
		<executions>
			<execution>
				<id>copy-dependencies</id>
				<phase>package</phase>
				<goals>
					<goal>copy-dependencies</goal>
				</goals>
				<configuration>
					<type>jar</type>
					<includeTypes>jar</includeTypes>
					<outputDirectory>
						${project.build.directory}/dependencies/lib
					</outputDirectory>
				</configuration>
			</execution>
		</executions>
	</plugin>

7.如果lib有变化直接复制lib即可

Tags:

文章作者: Lee

本文地址: https://www.pomelolee.com/2084.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Meta