springboot 精简Jar包 部署到Linux服务器运行,并创建启动脚本

本文介绍了如何通过调整pom.xml配置,过滤静态资源并打包去除第三方依赖,实现SpringBoot项目的轻量化jar包部署。还提供了脚本示例,用于备份旧jar、更新部署、项目停止与重启,以及检查启动状态。

springboot 精简Jar包 部署到Linux服务器运行,并创建启动脚本

精简 springboot Jar包

springboot项目如果在本地打包,不是使用jenkins等工具线上直接打包部署,直接打成的jar包,文件会比较大,不便于上传部署,对于前后端分离系统,假如前端项目不是单独开发,如果不作处理,静态资源文件也会包含在jar包中,造成jar包臃肿。处理的办法可以只打包项目文件,第三方所需jar包可以单独上次服务器,部署项目时直接引用;打包时过滤静态文件,静态文件单独上传到服务器。具体操作过程如下:

pom.xml 打包相关配置

1、静态资源过滤
前后端分离系统, 如果前端项目不是单独开发,打包时可以过滤静态资源文件:

<resources>           
       <resource>
             <filtering>true</filtering> 
            <directory>src/main/resources</directory>
            <targetPath>BOOT-INF/classes/</targetPath>
            <excludes>
				<exclude>**/*.css</exclude>
				<exclude>**/*.map</exclude>
				<exclude>**/*.js</exclude>
				<exclude>**/*.jpg</exclude>
				<exclude>**/*.png</exclude>
				<exclude>**/*.gif</exclude>
				<exclude>**/*.html</exclude>	
				<exclude>**/*.txt</exclude>	
				<exclude>**/*.eot</exclude>	
				<exclude>**/*.svg</exclude>	
				<exclude>**/*.less</exclude>	
				<exclude>**/*.ttf</exclude>	
				<exclude>**/*.otf</exclude>
				<exclude>**/*.woff</exclude>
				<exclude>**/*.woff2</exclude>	
				<exclude>**/*.calendar</exclude>	
				<exclude>**/*.zip</exclude>	
				<exclude>**/*.json</exclude>	
				<exclude>**/*.exe</exclude> 
			</excludes> 
        </resource>
        <resource>
             <directory>src/main/resources</directory>
                <excludes>
				<exclude>**/*.css</exclude>
				<exclude>**/*.map</exclude>
				<exclude>**/*.js</exclude>
				<exclude>**/*.jpg</exclude>
				<exclude>**/*.png</exclude>
				<exclude>**/*.gif</exclude>
				<exclude>**/*.html</exclude>	
				<exclude>**/*.txt</exclude>	
				<exclude>**/*.eot</exclude>	
				<exclude>**/*.svg</exclude>	
				<exclude>**/*.less</exclude>	
				<exclude>**/*.ttf</exclude>	
				<exclude>**/*.otf</exclude>
				<exclude>**/*.woff</exclude>
				<exclude>**/*.woff2</exclude>	
				<exclude>**/*.calendar</exclude>	
				<exclude>**/*.zip</exclude>	
				<exclude>**/*.json</exclude>	
				<exclude>**/*.exe</exclude> 
			</excludes> 
       </resource>          
 </resources>

2、打包时去除第三方依赖

	 <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>
          <include>
              <groupId>com.chain</groupId>
              <artifactId>sys-common</artifactId>
          </include>
        </includes>
      </configuration>
    </plugin> 

sys-common 为项目依赖jar包,有时会变动,所以也做了打包处理,类似有变动的jar也可以这样处理

初次打包,可拷贝第三方依赖文件到指定目录:

 <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>
		            <outputDirectory>target/lib</outputDirectory>
		            <excludeTransitive>false</excludeTransitive>
		            <stripVersion>false</stripVersion>
		            <includeScope>runtime</includeScope>
		          </configuration>
		        </execution>
		      </executions>
 </plugin>

某些证书(如支付证书)打包时会被处理,导致不能使用,可以过滤证书文件

	<plugin> 
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-resources-plugin</artifactId>
		 <version>2.6</version>
		<configuration>
			<encoding>UTF-8</encoding>
			<nonFilteredFileExtensions>
				<nonFilteredFileExtension>pem</nonFilteredFileExtension>
				<nonFilteredFileExtension>pfx</nonFilteredFileExtension>
				<nonFilteredFileExtension>p12</nonFilteredFileExtension>
				<nonFilteredFileExtension>exe</nonFilteredFileExtension>
			</nonFilteredFileExtensions>
		</configuration>
    </plugin>

jar包上次到服务器了,可以通过脚本快速部署

1、备份原jar 包 ,移动新jar包

#!/bin/bash
#!打包后jar包名称
r_name=dms-dzy-admin.jar
#!部署项目路径
file_path=/opt/web/dzy/admin
#!上传项目路径
rs_path=/opt/web/dzy/admin/rs
#!项目备份路径
bak_path=/opt/web/dzy/admin/bak

file=${file_path}/${r_name} 
if [ -f "$file" ]
then
mv ${file}  ${bak_path}/${r_name}.`date +%Y%m%d%H%M%S`
fi
 
#!将现有的jar备份后,将新的jar包替换
file=${file_path}/${r_name} 
if [ -f "$file" ]
then
mv ${file}  ${bak_path}/${r_name}.`date +%Y%m%d%H%M%S`
fi
cp ${rs_path}/${r_name}  ${file_path}
echo "replace ${r_name}"

2、项目停止重启

#!/bin/bash
RESOURCE_NAME=dms-dzy-admin.jar
 
tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo "Stop Process-->>dms-dzy-admin $tpid..." 
kill -15 $tpid
fi
sleep 5

tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo "Kill Process-->>dms-dzy-admin $tpid"
kill -9 $tpid
else
echo "Stop Success-->>dms-dzy-admin"
fi
 
tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo "Admin -->>dms-dzy-admin is running."
else
    echo "Admin -->>dms-dzy-admin is NOT running."
fi
 
rm -f dms-dzy-admin_tpid
nohup java -Dserver.port=8082 -Dspring.profiles.active=prod  -Dloader.path=/opt/web/dzy/admin/lib  -jar /opt/web/dzy/admin/$RESOURCE_NAME > /opt/web/dzy/admin/consoleMsg.log 2>&1 &
echo "Start Success-->>dms-dzy-admin $!"

-Dloader.path=/opt/web/dzy/admin/lib 引入已经上传的第三方jar包

3、查看启动状态:
#!/bin/bash
tail -f -n 600 /opt/web/dzy/admin/consoleMsg.log

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值