SpringBoot项目打包+Shell脚本部署实践,太有用了!

SpringBoot项⽬打包+Shell脚本部署实践,太有⽤了!
点击蓝⾊“架构⽂摘”关注我哟
加个“星标”,每天上午 09:25,⼲货推送!
作者:神⽜003
原⽂:cnblogs/wangrudong003/p/10502043.html
本篇和⼤家分享的是 Spring Boot 打包并结合 Shell 脚本命令部署,重点在分享⼀个shell 程序启动⼯具,希望能便利⼯作;
profiles指定不同环境配置
maven-assembly-plugin打发布压缩包
分享shenniu_publish.sh程序启动⼯具
linux上使⽤shenniu_publish.sh启动程序
profiles指定不同环境的配置
通常⼀套程序分为了很多个部署环境:开发,测试,uat,线上 等,我们要想对这些环境区分配置⽂件,可以通过两种⽅式:通过l中编码指定 profile.active=uat ⽅式指定
通过mvn中profiles来区分不同环境对应的配置⽂件夹,⼈⼯可以⼿动在idea勾选⽣成不同环境的包(推荐)
这⾥我们要讲的是第⼆种,⾸先在mvn中配置如下内容:
1 <profiles>
发光棒2 <profile>
哑光玻璃3 <id>node</id>
4 <properties>
5 <!--传递给脚本的参数值-->
6 <activeProfile>node</activeProfile>
7 <package-name>${scripts_packageName}</package-name>
8 <boot-main>${scripts_bootMain}</boot-main>
9 </properties>
10 <activation>
11 <activeByDefault>true</activeByDefault>
12 </activation>
牌位架
13 </profile>
14 <profile>
15 <id>node1</id>
力矩限制器
16 <properties>
17 <activeProfile>node1</activeProfile>
18 <package-name>${scripts_packageName}</package-name>
19 <boot-main>${scripts_bootMain}</boot-main>
20 </properties>
21 </profile>
22 <profile>
23 <id>node2</id>
24 <properties>
25 <activeProfile>node2</activeProfile>
26 <package-name>${scripts_packageName}</package-name>
27 <boot-main>${scripts_bootMain}</boot-main>
28 </properties>
29 </profile>
30 </profiles>
节点粗解:
id:⽤来指定不同环境配置⽂件所在的⽬录,如下我这⾥:
properties:该节点中的节点是可作为参数传递给其他配置⽂件,如我这⾥的package-name节点值就可以在另外的l或者shell脚本⽂件中通过${package-name}获取到,如下:
activeByDefault:指定默认环境配置⽂件夹
maven-assembly-plugin打发布压缩包
对于springboot程序打包,可以分为jar和war,这⾥是jar包;有场景是咋们配置⽂件或者第三⽅等依赖包不想放到⼯程jar中,并且把这些⽂件压缩成⼀个zip包,⽅便上传到linux;此时通过maven-assembly-plugin和maven-jar-plugin就可以做到,mvn的配置如:
1 <plugin>
2 <groupId>org.apache.maven.plugins</groupId>
3 <artifactId>maven-jar-plugin</artifactId>
4 <version>2.6</version>
5 <configuration>
6 <archive>
7 <addMavenDescriptor>false</addMavenDescriptor>
8 <manifest>
9 <addClasspath>true</addClasspath>
10 <classpathPrefix>lib/</classpathPrefix>
11 <mainClass>${scripts_bootMain}</mainClass>
12 </manifest>
13 </archive>
14 <!--打包排除项-->
15 <excludes>
16 <exclude>**/*.yml</exclude>
17 <exclude>**/*.properties</exclude>
18 <exclude>**/*.xml</exclude>
19 <exclude>**/*.sh</exclude>
20 </excludes>
21 </configuration>
22 <executions>
23 <execution>
24 <id>make-a-jar</id>
25 <phase>compile</phase>
26 <goals>
27 <goal>jar</goal>
28 </goals>
29 </execution>
30 </executions>
31 </plugin>
32
33 <plugin>
34 <groupId>org.apache.maven.plugins</groupId>
35 <artifactId>maven-assembly-plugin</artifactId>
36 <version>2.4</version>
37 <!-- The configuration of the plugin -->
38 <configuration>
39 <!-- Specifies the configuration file of the assembly plugin -->
40 <descriptors>
41 <descriptor>${project.basedir}/src/main/l</descriptor>
42 </descriptors>
43 </configuration>
44 <executions>
45 <execution>
46 <id>make-assembly</id>
47 <phase>package</phase>
48 <goals>
49 <goal>single</goal>
50 </goals>
51 </execution>
52 </executions>
53 </plugin>
值得注意的地⽅如下⼏点:
mainClass节点:⽤来指定启动main函数⼊⼝类路径,如这⾥的:com.sm.EurekaServerApplication excludes节点:排除主jar包中配置等⼀些列后缀⽂件,因为我们要包这些配置⽂件放到主包外⾯
descriptor节点:⽤来指定assembly插件对应的l配置⽂件
有了上⾯mvn配置,我们还需要l的配置,这⾥提取了结合shell脚本发布程序的配置:
<assembly xmlns="/ASSEMBLY/2.0.0" xmlns:xsi="/2001/XMLSchema-instance"          xsi:schemaLocation="/ASSEMBLY/2.0.0 /xsd/assembly-2.0.0.xsd /ASSEMBLY/2.0.0 ">
<id>${activeProfile}</id>
<!--打包成⼀个⽤于发布的zip⽂件-->
<formats>
<format>zip</format>
</formats>
<!--true:zip中⽣成⼀级⽬录(此处屏蔽,配合脚本需要profiles后缀)-->
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<!--打包进zip⽂件的lib⽬录-->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>${package-name}-${activeProfile}/lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<!-- 配置⽂件打包进zip⽂件的conf⽬录 -->
<fileSet>
<directory>${project.basedir}/src/main/profiles/${activeProfile}</directory>
<outputDirectory>${package-name}-${activeProfile}/conf</outputDirectory>
<includes>
<include>**/*</include>
<!--<include>*.xml</include>-->
<!--<include>*.properties</include>-->
<!--<include>*.yml</include>-->
</includes>
</fileSet>
<!--启动脚本打包进zip⽂件-->
<fileSet>
<directory>${project.basedir}/src/main/scripts</directory>
<outputDirectory></outputDirectory>
<includes>
<include>**/*</include>屋面拉条
</includes>
<!-- ⽂件⽂件权限为777 -->
<fileMode>777</fileMode>
<!-- ⽬录权限为777 -->
<directoryMode>777</directoryMode>
<!--脚本中参数变量为pom中的值关键-->
<filtered>true</filtered>
</fileSet>
<!-- 项⽬编译出来的jar打包进zip⽂件 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>${package-name}-${activeProfile}/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
重点节点介绍:
formats节点:把配置⽂件和jar包等压缩成什么⽂件格式,这⾥可以有:zip,tar等
fileMode节点:指定scripts⽬录下脚本⽂件(这⾥是:shenniu_publish.sh)在linux上⽂件权限为777
filtered节点:脚本中参数变量为pom的profiles中properties的值(该配置,是把mvn中属性值映射⽣成到sh⽂件中,如:${package-name})
完成上⾯配置后,此时我们可以通过idea上勾选切换不同环境来打zip包,如图:
分享shenniu_publish.sh程序启动⼯具
上⾯步骤完成了zip格式的发布包,我们再分享下启动程序的shell脚本,该脚本具有的功能如:
解压zip+启动jar包
ad2组合启动jar包
停⽌对应jar运⾏
重启jar程序
⽬前该shell中封装了两种启动jar命令的⽅式:
java -cp
java -jar
如图命令格式:
来看全部的shell代码:
#!/usr/bin/env bash
#可变参数变量
languageType="javac" #⽀持 java,javac,netcore 发布
#参数值由pom⽂件传递
baseZipName="${package-name}-${activeProfile}" #压缩包名称 publish-test.zip的publish
packageName="${package-name}" #命令启动包名 xx.jar的xx
mainclass="${boot-main}" #java -cp启动时,指定main⼊⼝类;命令:java -cp conf;lib\*.jar;${packageName}.jar ${mainclass}
#例⼦
# baseZipName="publish-test" #压缩包名称 publish-test.zip的publish
# packageName="publish" #命令启动包名 publish.jar的xx
#固定变量
basePath=$(cd `dirname $0`/; pwd)
baseZipPath="${basePath}/${baseZipName}.zip"  #压缩包路径
baseDirPath="${basePath}" #解压部署磁盘路径
pid= #进程pid
#解压
function shenniu_unzip()

本文发布于:2024-09-25 13:16:15,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/178285.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:配置   脚本   环境   启动   指定
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议