> 文章列表 > Python工程师Java之路(t)使用Shell脚本部署SpringBoot

Python工程师Java之路(t)使用Shell脚本部署SpringBoot

Python工程师Java之路(t)使用Shell脚本部署SpringBoot

文章目录

  • 1、概述
  • 2、在服务器上安装Maven
  • 2、在服务器上安装Git
  • 3、Shell脚本
  • 4、SpringBoot部署测试

1、概述

  1. 代码从开发环境上传到Git仓库
  2. 服务器从Git仓库拉取代码
  3. 在服务器上进行编译、打包、重启

2、在服务器上安装Maven

2.1、下载

wget https://archive.apache.org/dist/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz

2.2、解压

tar -zxf apache-maven-3.5.4-bin.tar.gz
mv apache-maven-3.5.4 /opt/module/
/opt/module/apache-maven-3.5.4/bin/mvn -version

2.3、修改配置

vim /opt/module/apache-maven-3.5.4/conf/settings.xml
<!-- 本地仓库存放路径 -->
<localRepository>/opt/module/mavenRepository</localRepository>
<!-- 配置国内镜像,使下载更快 -->
<mirror><id>nexus-aliyun</id><mirrorOf>central</mirrorOf><name>Nexus aliyun</name><url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

2、在服务器上安装Git

2.1、安装git

yum install -y git

2.2、创建仓库地址的目录

mkdir /opt/module/git_repository
chown -R hjw:hjw /opt/module/git_repository

2.3、创建代码仓库(名为hello

git init --bare /opt/module/git_repository/hello

2.4、免密配置

https://blog.csdn.net/Yellow_python/article/details/127570500

3、Shell脚本

cd /opt/module/projects
touch SpringBootRestart.sh
chmod 777 SpringBootRestart.sh
vim SpringBootRestart.sh
#!/bin/sh# artifactId
ARTIFACT=hello
# 版本
VERSION=-0.0.1-SNAPSHOT
# Maven路径
MVN=/opt/module/apache-maven-3.5.4/bin/mvn
# 代码位置
LOC=/opt/module/projects/$ARTIFACT
# jar名
JAR=${ARTIFACT}${VERSION}.jarpid=`ps -ef|grep ${JAR}|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${pid} ]; thenecho '停止进程'kill -15 $pid
fi
sleep 3
pid=`ps -ef|grep ${JAR}|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${pid} ]; thenecho '杀进程'kill -9 $pid
elseecho '停止进程成功'
fiecho '从Git仓库拉取最新代码'
cd $LOC
git pullecho '打包'
output=`$MVN clean package -Dmaven.test.skip=true`echo '启动项目'
cd target
nohup java -jar ${JAR} &> ${ARTIFACT}.log &# 查看进程状态
sleep 2
echo `ps -ef|grep ${JAR}|grep -v grep`

4、SpringBoot部署测试

4.1、使用IDEA创建工程(下面2种方法)

方法1:先创建SpringBoot工程,再去关联Git仓库
Python工程师Java之路(t)使用Shell脚本部署SpringBoot
Python工程师Java之路(t)使用Shell脚本部署SpringBoot

方法2:直接创建关联Git仓库的工程
Python工程师Java之路(t)使用Shell脚本部署SpringBoot

4.2、服务器从仓库下载代码

mkdir -p /opt/module/projects
cd /opt/module/projects
git clone 用户名@主机地址:/opt/module/git_repository/hello

4.3、执行脚本

/opt/module/projects/SpringBootRestart.sh

Python工程师Java之路(t)使用Shell脚本部署SpringBoot