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:用来指定不同环境配置文件所在的目录,如下我这里:

properties:该节点中的节点是可作为参数传递给其他配置文件,如我这里的package-name节点值就可以在另外的assembly.xml或者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/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>
50 </goals>
51 </execution>
52 </executions>
53 </plugin>

值得注意的地方如下几点:

  • mainClass节点:用来指定启动main函数入口类路径,如这里的:com.sm.EurekaServerApplication
  • excludes节点:排除主jar包中配置等一些列后缀文件,因为我
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值