探索win10下,bat脚本实现自动化部署

服务器配置

1.安装ftp服务:yum install -y vsftpd
启动ftp服务:service vsftpd restart
查看是否启动成功:service vsftpd status

2.开放端口21

3.直接使用root的用户上传文件,会出现错误530 Permission denied
vi /etc/vsftpd/user_listroot给注释了
vi /etc/vsftpd/ftpusersroot给注释了
重启ftp服务:service vsftpd restart

bat脚本实现文件上传

把下面这段代码当到一个bat脚本里,改一改,即可

@echo off
echo open ip 21 >> temp.txt
echo user root 12345678 >> temp.txt
echo bin >> temp.txt
echo put "G:\Desktop\测试\自动化脚本部署\1.txt" "/1" >> temp.txt
echo bye >> temp.txt
ftp -n -s:"temp.txt"
del /q temp.txt
pause

ip ftp的ip地址
21 端口
username 用户名
password 密码
temp.txt 保存ftp信息的临时文件,上传后删除
G:\Desktop\测试\自动化脚本部署\1.txt 上传到/目录下的1文件

bat脚本实现maven项目打包

@echo off
call cd /d F:\ideaWebProject源码\spring-boot-07-mybaties
call mvn clean package -Dmaven.test.skip=true  --settings G:\.m2\settings-sxw.xml
echo "打包完成!"
pause

cd /d 表示直接转换到后面的路径,否则如果切换盘符,就需要再输入盘符才能切换路径

实现自动化部署maven项目

@echo off
call cd /d F:\ideaWebProject源码\spring-boot-07-mybaties
call mvn clean package -Dmaven.test.skip=true  --settings G:\.m2\settings-sxw.xml
echo "打包完成!"
echo open ip 21 >> temp.txt
echo user root 12345678 >> temp.txt
echo bin >> temp.txt
echo put "F:\ideaWebProject源码\spring-boot-07-mybaties\target\spring-boot-07-mybaties-0.0.1-SNAPSHOT.jar" "/my.jar" >> temp.txt
echo bye >> temp.txt
ftp -n -s:"temp.txt"
del /q temp.txt
pause

注意:有中文要把bat文件另存为:
在这里插入图片描述

另一种方式实现自动化部署,比较好用

https://www.cnblogs.com/suruozhong/p/12100535.html

下载pscpplinkhttps://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html,下载完后,把这两exe放到C:\Windows\System32中。

@echo off
rem 打印的中文不乱码,默认是gbk,改为utf8
chcp 65001
rem 拷贝文件完整路径
set folder=F:\ideaWebProject源码\spring-boot-07-mybaties\target\spring-boot-07-mybaties-0.0.1-SNAPSHOT.jar
rem 服务器IP
set ip=39.107.xxx.xxx
rem 服务器密码
set password=xxxxxx
echo "开始打包...................."
e:
cd /d F:\ideaWebProject源码\spring-boot-07-mybaties
call mvn clean package -Dmaven.test.skip=true  --settings G:\.m2\settings-sxw.xml
echo "传输到linux服务器home目录下..."
pscp -pw %password% %folder% root@%ip%:/home/project/my.jar
echo "启动,远程调用linux服务器上的sh脚本..."
plink -batch -pw %password% root@%ip%     source /etc/profile ; /home/project/shutdown.sh; /home/project/start.sh
@cmd.exe
exist

说明:把文件另存为utf-8的编码。
rem: 表示注释
set:设置变量
echo:打印
cd /d 表示直接转换到后面的路径,否则如果切换盘符,就需要再输入盘符才能切换路径
call:批处理
pscp -pw %password% %folder% root@%ip%:/home/project/my.jar 上传文件
plink -batch -pw %password% root@%ip% source /etc/profile ; /home/project/shutdown.sh; /home/project/start.sh 登录到linux上并执行三个命令,涉及到环境的还要先执行source /etc/profile,比如:这里执行start.sh会在日志中打印中文,如果不执行那个命令,中文会乱码

start.sh:这里输出的日志要写绝对路径/home/project/logs/admin.log

nohup java -Xmx250M -Xms250M -XX:MetaspaceSize=250M  -XX:MaxMetaspaceSize=250M -Xss256k -jar /home/project/my.jar  >/home/project/logs/admin.log 2>&1 &

shutdown.sh

#!/usr/bin/env bash
#/bin/bash
#======================================================================
# 项目启动shell脚本
# bin目录: 启动脚本
# config目录: 配置文件目录
# lib目录: 应用程序
# logs目录: 项目运行日志目录
# nohup后台运行
#
# author: liuzhihui
# date: 2020-5-29
# xxxxxx@qq.com
#======================================================================

PID=`pgrep -f my`

if [ -z "${PID}" ];
then
        echo 'Cannot find service process.'
else
        kill -9 $PID
        echo 'service has been shutdown successfully.'
fi

项目中的实际应用

运行这个bat脚本,整个项目打包,然后上传到linux,先停了之前的服务,再重启服务。满足需求了。

@echo off
rem 打印的中文不乱码,默认是gbk,改为utf8
chcp 65001
echo "开始打包...................."
e:
cd /d F:\ideaWebProject\guide-survey
call mvn clean package -Dmaven.test.skip=true  --settings G:\.m2\settings-sxw.xml
echo "传输到linux服务器home目录下..."
pscp -pw  密码  F:\ideaWebProject\guide-survey\guide-survey-admin\target\guide-survey-admin-slot.jar  用户名@ip:/home/guidesurvey/admin/guide-survey-admin-slot.jar
pscp -pw  密码  F:\ideaWebProject\guide-survey\guide-survey-service\target\guide-survey-service-slot.jar  用户名@ip:/home/guidesurvey/service/guide-survey-service-slot.jar
echo "启动,远程调用linux服务器上的sh脚本..."
plink -batch -pw  密码  用户名@ip     source /etc/profile ; /home/guidesurvey/service/shutdown.sh; /home/guidesurvey/service/start.sh;/home/guidesurvey/admin/shutdown.sh; /home/guidesurvey/admin/start.sh;
@cmd.exe
exist

附录:

https://blog.csdn.net/qq_35368565/article/details/82258351
https://www.jb51.net/article/85324.htm
https://www.cnblogs.com/xiaowangba/p/6314227.html
https://blog.csdn.net/bamuta/article/details/7821607
https://zhuanlan.zhihu.com/p/125033413

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值