点击上方蓝色“方志朋”,选择“设为星标”

回复“666”获取独家整理的学习资料!


本篇和大家分享的是 Spring Boot 打包并结合 Shell 脚本命令部署,重点在分享一个shell 程序启动工具,希望能便利工作;

  • profiles指定不同环境的配置

  • maven-assembly-plugin打发布压缩包

  • 分享shenniu_publish.sh程序启动工具

  • linux上使用shenniu_publish.sh启动程序

我把 Spring Boot 相关的技术文章整理成了 PDF,关注微信关注号 Java后端,回复 666 下载这一本技术栈手册。

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包中配置等一些列后缀文件,因为我们要包这些配置文件放到主包外面

  • descriptor节点:用来指定assembly插件对应的assembly.xml配置文件

有了上面mvn配置,我们还需要assembly.xml的配置,这里提取了结合shell脚本发布程序的配置:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd
http://maven.apache.org/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包

  • 启动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()
{echo "解压---------------------------------------------"echo "压缩包路径:${baseZipPath}"if [ ! `find ${baseZipPath}` ]thenecho "不存在压缩包:${baseZipPath}"elseecho "解压磁盘路径:${baseDirPath}/${baseZipName}"echo "开始解压..."#解压命令unzip -od ${baseDirPath}/${baseZipName} ${baseZipPath}#设置执行权限chmod +x ${baseDirPath}/${baseZipName}/${packageName}echo "解压完成。"fi
}#检测pid
function getPid()
{echo "检测状态---------------------------------------------"pid=`ps -ef | grep -n ${packageName} | grep -v grep | awk '{print $2}'`if [ ${pid} ]thenecho "运行pid:${pid}"elseecho "未运行"fi
}#启动程序
function start()
{#启动前,先停止之前的stopif [ ${pid} ]thenecho "停止程序失败,无法启动"elseecho "启动程序---------------------------------------------"#选择语言类型read -p "输入程序类型(java,javac,netcore),下一步按回车键(默认:${languageType}):" read_languageTypeif [ ${read_languageType} ]thenlanguageType=${read_languageType}fiecho "选择程序类型:${languageType}"#进入运行包目录cd ${baseDirPath}/${baseZipName}#分类启动if [ "${languageType}" == "javac" ]thenif [ ${mainclass} ]thennohup java -cp conf:lib\*.jar:${packageName}.jar ${mainclass} >${baseDirPath}/${packageName}.out 2>&1 &#nohup java -cp conf:lib\*.jar:${packageName}.jar ${mainclass} >/dev/null 2>&1 &fielif [ "${languageType}" == "java" ]thennohup java -jar ${baseDirPath}/${baseZipName}/${packageName}.jar >/dev/null 2>&1 &# java -jar ${baseDirPath}/${baseZipName}/${packageName}.jarelif [ "${languageType}" == "netcore" ]then#nohup dotnet run ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 &nohup ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 &fi#查询是否有启动进程getPidif [ ${pid} ]thenecho "已启动"#nohup日志tail -n 50 -f ${baseDirPath}/${packageName}.outelseecho "启动失败"fifi
}#停止程序
function stop()
{getPidif [ ${pid} ]thenecho "停止程序---------------------------------------------"kill -9 ${pid}getPidif [ ${pid} ]then#stopecho "停止失败"elseecho "停止成功"fifi
}#启动时带参数,根据参数执行
if [ ${#} -ge 1 ]
thencase ${1} in"start")start;;"restart")start;;"stop")stop;;"unzip")#执行解压shenniu_unzip#执行启动start;;*)echo "${1}无任何操作";;esac
elseecho "command如下命令:unzip:解压并启动start:启动stop:停止进程restart:重启示例命令如:./shenniu_publish start"
fi

正如上面小节说的,shell中的参数 package-name,activeProfile,boot-main 都是由mvn中profiles的properties中提供,是可变的参数,脚本代码本身不需要人工去修改,只需要变的是mvn的参数即可;其实在我们生成zip包的时候,shell中的参数就被替换了,可以看zip中shell文件内容如:

把生成的zip上传到linux上,通过命令解压:

1 unzip -od eureka-server-0.0.1-node eureka-server-0.0.1-node.zip

其实shell脚本中包含有解压命令,但是我在打包时放在了zip中,所以只能通过手动解压了,当然可以调整;此时进入加压目录如此:

注:这里第一次执行./shenniu_publish.sh脚本时候,提示了错误信息;是由于我是在windows上编辑的这个脚本,其空格等和linux上不一样,所以运行会有问题,要解决可以使用vim命令在linux把该文件转成linux格式,如下命令:

1 vim shenniu_publish.sh
2 set ff=unix
3 :wq

执行完后,再来运行脚本./shenniu_publish.sh,此时有如下提示:

此刻我们文件是解压状态,因此只需要start命令启动程序即可:

到这里shenniu_publish.sh脚本使用就完成了,只要脚本没有提示错误,基本都能启动jar服务;其他restart和stop命令也如此执行就行:

可以去研究下shell代码,希望该脚本能给你带来效率和好的学习思路,下面是测试用例git地址,脚本在eureka-server项目中:https://github.com/shenniubuxing3/springcloud-Finchley.SR2

链接:cnblogs.com/wangrudong003/p/10502043.html

热门内容:
  • 写了个牛逼的日志切面,甩锅更方便了!

  • 看看人家那后端API接口写得,那叫一个优雅!

  • Spring Boot 监听 Redis Key 失效事件实现定时任务

最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。
明天见(。・ω・。)ノ♡

Spring Boot 打包不同环境配置与 Shell 脚本部署相关推荐

  1. java 打包xml脚本_springboot打包不同环境配置与shell脚本部署

    本篇和大家分享的是springboot打包并结合shell脚本命令部署,重点在分享一个shell程序启动工具,希望能便利工作: profiles指定不同环境的配置 maven-assembly-plu ...

  2. springboot打包不同环境配置与shell脚本部署

    可以去研究下shell代码,希望该脚本能给你带来效率和好的学习思路,下面是测试用例git地址,脚本在eureka-server项目中:https://github.com/shenniubuxing3 ...

  3. Spring Boot - Profile不同环境配置

    Profile是什么 Profile我也找不出合适的中文来定义,简单来说,Profile就是Spring Boot可以对不同环境或者指令来读取不同的配置文件. Profile使用 假如有开发.测试.生 ...

  4. Spring Boot 2.0 的配置详解(图文教程)

    本文来自作者 泥瓦匠 @ bysocket.com 在 GitChat 上分享 「Spring Boot 2.0 的配置详解(图文教程)」 编辑 | 哈比 Spring Boot 配置,包括自动配置和 ...

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

    欢迎关注方志朋的博客,回复"666"获面试宝典 本篇和大家分享的是 Spring Boot 打包并结合 Shell 脚本命令部署,重点在分享一个shell 程序启动工具,希望能便利 ...

  6. 后端技术:Spring Boot 项目打包 + Shell 脚本部署实践,太有用了!

    本篇和大家分享的是 Spring Boot 打包并结合 Shell 脚本命令部署,重点在分享一个shell 程序启动工具,希望能便利工作: profiles指定不同环境的配置 maven-assemb ...

  7. Spring Boot 项目部署方案 /打包 + Shell 脚本部署详解,稳的一批

    本篇和大家分享的是 Spring Boot 打包并结合 Shell 脚本命令部署,重点在分享一个shell 程序启动工具,希望能便利工作: profiles指定不同环境的配置 maven-assemb ...

  8. Springboot使用Maven Profile和Spring Profile进行多环境配置

    Springboot使用Maven Profile和Spring Profile进行多环境配置 目的 在实际的项目上,一般会分三种环境dev.test.prod来方便我们的开发和部署,要求我们在开发的 ...

  9. 从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建

    从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建 本文简介 为什么使用Spring Boot 搭建怎样一个环境 开发环境 导入快速启动 ...

最新文章

  1. 第七章 右左法则----复杂指针解析
  2. 详解如何实现在线聊天系统中的实时消息获取
  3. 用postman在CSDN上创建博客
  4. 谈谈你对php的收获和不足,我的收获与不足
  5. jQuery -- 光阴似箭(五):AJAX 方法
  6. php未定义常量破解,PHP未定义的常量错误没有意义
  7. spring中的bean
  8. 给本地Git配置账号信息
  9. python小游戏之圣诞树
  10. 陕西省单招计算机应用考什么,陕西省对口单招计算机应用基础模拟试题四
  11. matlab中信号叠加高斯噪声代码
  12. Android隐藏app桌面图标
  13. HTTP代理与DNS
  14. 租赁风控模型之决策树
  15. ERROR: failed to establish dependency between database sgerp5 and diskgroup resource ora.DATA.dg
  16. cad面积计算机,用cad计算多个面积的方法步骤
  17. rust多行字符串字面量
  18. 原创 | 2020年数据科学与大数据技术专业填报指南(附院校及专业介绍)
  19. @EnableCaching
  20. Windows将鼠标单击转换为双击的原理

热门文章

  1. Window环境下,Qt中文出现乱码解决办法
  2. 洛谷 P1966 火柴排队
  3. Docker-Compose搭建单体SkyWalking 6.2
  4. Hadoop学习之路(三)Hadoop-2.7.5在CentOS-6.7上的编译
  5. 基于wsimport生成代码的客户端
  6. 1.lamp网站构建
  7. Linux-TCP/IP TIME_WAIT状态原理
  8. bzoj1562[NOI2009]变换序列——2016——3——12
  9. Jzzhu and Chocolate
  10. 将XML转为HTML