点击上方 Java后端,选择 设为星标
优质文章,及时送达
本篇和大家分享的是 Spring Boot 打包并结合 Shell 脚本命令部署,重点在分享一个shell 程序启动工具,希望能便利工作;
- profiles指定不同环境的配置
- maven-assembly-plugin打发布压缩包
- 分享shenniu_publish.sh程序启动工具
- linux上使用shenniu_publish.sh启动程序
- 通过application.yml中编码指定 profile.active=uat 方式指定
- 通过mvn中profiles来区分不同环境对应的配置文件夹,人工可以手动在idea勾选生成不同环境的包(推荐)
1 <profiles>
2 <profile>
3 <id>nodeid>
4 <properties>
5
6 <activeProfile>nodeactiveProfile>
7 <package-name>${scripts_packageName}package-name>
8 <boot-main>${scripts_bootMain}boot-main>
9 properties>
10 <activation>
11 <activeByDefault>trueactiveByDefault>
12 activation>
13 profile>
14 <profile>
15 <id>node1id>
16 <properties>
17 <activeProfile>node1activeProfile>
18 <package-name>${scripts_packageName}package-name>
19 <boot-main>${scripts_bootMain}boot-main>
20 properties>
21 profile>
22 <profile>
23 <id>node2id>
24 <properties>
25 <activeProfile>node2activeProfile>
26 <package-name>${scripts_packageName}package-name>
27 <boot-main>${scripts_bootMain}boot-main>
28 properties>
29 profile>
30 profiles>
节点粗解:
id
:用来指定不同环境配置文件所在的目录,如下我这里:
![27e5eef3ee7a18d1dfcd9370d2ccfbdb.png](https://img-blog.csdnimg.cn/img_convert/27e5eef3ee7a18d1dfcd9370d2ccfbdb.png)
![eccf1a6c4372e83cf18e14d412ee5203.png](https://img-blog.csdnimg.cn/img_convert/eccf1a6c4372e83cf18e14d412ee5203.png)
1 <plugin>
2 <groupId>org.apache.maven.pluginsgroupId>
3 <artifactId>maven-jar-pluginartifactId>
4 <version>2.6version>
5 <configuration>
6 <archive>
7 <addMavenDescriptor>falseaddMavenDescriptor>
8 <manifest>
9 <addClasspath>trueaddClasspath>
10 <classpathPrefix>lib/classpathPrefix>
11 <mainClass>${scripts_bootMain}mainClass>
12 manifest>
13 archive>
14
15 <excludes>
16 <exclude>**/*.ymlexclude>
17 <exclude>**/*.propertiesexclude>
18 <exclude>**/*.xmlexclude>
19 <exclude>**/*.shexclude>
20 excludes>
21 configuration>
22 <executions>
23 <execution>
24 <id>make-a-jarid>
25 <phase>compilephase>
26 <goals>
27 <goal>jargoal>
28 goals>
29 execution>
30 executions>
31 plugin>
32
33 <plugin>
34 <groupId>org.apache.maven.pluginsgroupId>
35 <artifactId>maven-assembly-pluginartifactId>
36 <version>2.4version>
37
38 <configuration>
39
40 <descriptors>
41 <descriptor>${project.basedir}/src/main/assembly/assembly.xmldescriptor>
42 descriptors>
43 configuration>
44 <executions>
45 <execution>
46 <id>make-assemblyid>
47 <phase>packagephase>
48 <goals>
49 <goal>singlegoal>
50 goals>
51 execution>
52 executions>
53 plugin>
值得注意的地方如下几点:
- mainClass节点:用来指定启动main函数入口类路径,如这里的:com.sm.EurekaServerApplication
- excludes节点:排除主jar包中配置等一些列后缀文件,因为我们要包这些配置文件放到主包外面
- descriptor节点:用来指定assembly插件对应的assembly.xml配置文件
<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><formats><format>zipformat>formats><includeBaseDirectory>falseincludeBaseDirectory><dependencySets><dependencySet><useProjectArtifact>falseuseProjectArtifact><outputDirectory>${package-name}-${activeProfile}/liboutputDirectory><unpack>falseunpack>dependencySet>dependencySets><fileSets><fileSet><directory>${project.basedir}/src/main/profiles/${activeProfile}directory><outputDirectory>${package-name}-${activeProfile}/confoutputDirectory><includes><include>**/*include>includes>fileSet><fileSet><directory>${project.basedir}/src/main/scriptsdirectory><outputDirectory>outputDirectory><includes><include>**/*include>includes><fileMode>777fileMode><directoryMode>777directoryMode><filtered>truefiltered>fileSet><fileSet><directory>${project.build.directory}directory><outputDirectory>${package-name}-${activeProfile}/outputDirectory><includes><include>*.jarinclude>includes>fileSet>fileSets>assembly>
重点节点介绍:
- formats节点:把配置文件和jar包等压缩成什么文件格式,这里可以有:zip,tar等
- fileMode节点:指定scripts目录下脚本文件(这里是:shenniu_publish.sh)在linux上文件权限为777
- filtered节点:脚本中参数变量为pom的profiles中properties的值(该配置,是把mvn中属性值映射生成到sh文件中,如:${package-name})
分享shenniu_publish.sh程序启动工具
上面步骤完成了zip格式的发布包,我们再分享下启动程序的shell脚本,该脚本具有的功能如:- 解压zip+启动jar包
- 启动jar包
- 停止对应jar运行
- 重启jar程序
- java -cp
- java -jar
#!/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
}#检测pidfunction 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} ]then
languageType=${read_languageType}fiecho "选择程序类型:${languageType}"#进入运行包目录cd ${baseDirPath}/${baseZipName}#分类启动if [ "${languageType}" == "javac" ]thenif [ ${mainclass} ]then
nohup 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" ]then
nohup 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}无任何操作"
;;esacelseecho "
command如下命令:
unzip:解压并启动
start:启动
stop:停止进程
restart:重启
示例命令如:./shenniu_publish start
"fi
正如上面小节说的,shell中的参数 package-name,activeProfile,boot-main 都是由mvn中profiles的properties中提供,是可变的参数,脚本代码本身不需要人工去修改,只需要变的是mvn的参数即可;其实在我们生成zip包的时候,shell中的参数就被替换了,可以看zip中shell文件内容如:
unzip -od eureka-server-0.0.1-node eureka-server-0.0.1-node.zip
其实shell脚本中包含有解压命令,但是我在打包时放在了zip中,所以只能通过手动解压了,当然可以调整;此时进入加压目录如此:
1 vim shenniu_publish.sh2 set ff=unix3 :wq
执行完后,再来运行脚本./shenniu_publish.sh,此时有如下提示:
文末送书
今天送 5 本《Java高并发编程指南》书籍,这本书非常经典,文末参与。
![19b8d96fc3a77fe93db314be7e0d02e2.png](https://img-blog.csdnimg.cn/img_convert/19b8d96fc3a77fe93db314be7e0d02e2.png)