Linux 下搭建 SpringBoot 项目自动化部署环境

Linux 下搭建 SpringBoot 项目自动化部署环境

# 系统环境
Alibaba Cloud ESC Linux CentOS 7.9

准备文件夹

# 存放日志文件
/home/log
# 存放 maven 以及本地仓库
/home/ProgrammingTool
# 存放项目
/home/project

环境安装

JDK 安装配置

安装

# 下载 rpm 安装包
https://www.oracle.com/java/technologies/oracle-java-archive-downloads.html

# 安装指令
rpm -ivh jdk-8u261-linux-x64.rpm

检验安装

# 查看版本
java -version

GIT 安装配置

安装

# 直接下载安装
yum -y install git

检验安装

git

配置

git config --global user.name "wangxiang"
git config --global user.email "276644985@qq.com"

MAVEN 安装配置

安装

 # 下载 apache-maven-3.6.3-bin.tar.gz
 https://maven.apache.org/download.cgi
 
 # 或者
 wget https://mirror.bit.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
 
 #如果没有 wget 输入
 yum -y install wget

 # 解压 (../ProgrammingTool/ 是解压到指定目录)
 tar zxvf apache-maven-3.6.3-bin.tar.gz -C ../ProgrammingTool/

配置

# 打开文件配置文件 
/etc/profile

# 添加以下配置
export MAVEN_HOME=/home/ProgrammingTool/apache-maven-3.6.3
export MAVEN_HOME
export PATH=$PATH:$MAVEN_HOME/bin

# 生效改动配置
source /etc/profile

检验安装

mvn -version

修改 maven 配置文件

# 打开配置文件
/home/ProgrammingTool/apache-maven-3.6.3/conf/settings.xml

# 修改配置如下
<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
   
  <!-- 设置仓库目录 -->
  <localRepository>/home/ProgrammingTool/maven_repository</localRepository>

  <pluginGroups></pluginGroups>
  <proxies></proxies>
  <servers></servers>

  <!-- 设置中央仓库镜像地址 -->
  <mirrors>
    <mirror>
      <id>aliyunmaven</id>
      <mirrorOf>*</mirrorOf>
      <name>Alibaba Cloud Maven</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

    <mirror>
      <id>aliyunmaven</id>
      <mirrorOf>central</mirrorOf>
      <name>AliCloud public repository</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central1</mirrorOf>
    </mirror>

    <mirror>
      <id>net-cn</id> 
      <mirrorOf>central2</mirrorOf> 
      <name>Human Readable Name for this Mirror.</name> 
      <url>http://maven.net.cn/content/groups/public/</url> 
    </mirror>
    
  </mirrors>
  <profiles></profiles>

</settings>

拉取项目

cd /home/project
git clone https://gitee.com/Haidnor/AlibabaCloudLinuxProjetc.git

编写 shell 脚本

#!/bin/bash
echo ""
echo -e "\033[34m>>> SCRIPT START \033[0m"
sleep 0.3

PID=$(ps -ef | grep 'java' | awk '{print $2}')
if [ -n "$PID" ]; then
	for id in $PID
	do
		echo -e "\033[31mSTOP $(ps -ef | grep $id | awk '{print $2"  "$10}' ) \033[0m"
		kill -9 $id
	done
fi

projec_dir='/home/project/AlibabaCloudLinuxProjetc'
cd $projec_dir

git pull
mvn clean package -Dmaven.test.skip=true

cd /
nohup java -jar ./home/project/AlibabaCloudLinuxProjetc/target/linux-1.0.jar > /home/log/projet-log.out  2>&1 &

echo -e "\033[34m>>> SCRIPT END \033[0m"

更新部署项目

/home/project/project.sh

查看日志

cd /home/log
tail -fn 10 projet-log.out
posted @ 2021-01-15 17:03  Haidnor  阅读(666)  评论(0编辑  收藏  举报