我司Spring Boot 项目打包 + Shell 脚本部署详细总结,太有用了!

博观而约取,厚积而薄发。这篇文章主要讲述我司Spring Boot 项目打包 + Shell 脚本部署详细总结,太有用了!相关的知识,希望能为你提供帮助。
本篇和大家分享的是 Spring Boot 打包并结合 Shell 脚本命令部署,重点在分享一个shell 程序启动工具,希望能便利工作;


  • profiles指定不同环境的配置
  • maven-assembly-plugin打发布压缩包
  • 分享shenniu_publish.sh程序启动工具
  • linux上使用shenniu_publish.sh启动程序
profiles指定不同环境的配置
通常一套程序分为了很多个部署环境:开发,测试,uat,线上 等,我们要想对这些环境区分配置文件,可以通过两种方式:
  • 通过application.yml中编码指定 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:用来指定不同环境配置文件所在的目录,如下我这里:
我司Spring Boot 项目打包 + Shell 脚本部署详细总结,太有用了!

文章图片
img
properties:该节点中的节点是可作为参数传递给其他配置文件,如我这里的package-name节点值就可以在另外的assembly.xml或者shell脚本文件中通过$package-name获取到,如下:
我司Spring Boot 项目打包 + Shell 脚本部署详细总结,太有用了!

文章图片
img
activeByDefault:指定默认环境配置文件夹
maven-assembly-plugin打发布压缩包
【我司Spring Boot 项目打包 + Shell 脚本部署详细总结,太有用了!】对于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/assembly/assembly.xml< /descriptor>
42 < /descriptors>
43 < /configuration>
44 < executions>
45 < execution>
46 < id> make-assembly< /id>
47 < phase> package< /phase>
48 < goals>
49 < goal> single< /goal

    推荐阅读