windows 批处理自动部署tomcat 并替换配置文件

 部署tomcat每次都要停止,拷贝,替换配置件事情太多,一个还好,如果很多就比较繁琐了,后来想偷懒来着,写了一个bat脚本将这些繁琐的事情都做了,只要双击就可以了

@ECHO OFF
@REM 部署WEB系统到Tomcat
set date=%date:~0,10%
set time=%time:~0,8%
set currDate=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%
echo 开始运行将工作空间里面编译后的代码拷贝到Tomcat中...%currDate%

echo 参数初始化
rem WORK_SPACE 新文件地址
rem CATALINA_HOME tomcat路径
rem WEB_APP tomcat下webapps
rem BIN tomcat下bin
rem BACKUP 备份路径
rem homeUrl 启动后验证是否启动成功
rem config_path 配置文件路径
set WORK_SPACE=C:\shell\sp
set PRJ_NAME=sp
set PRJ_WAR=sp.war
set CATALINA_HOME=C:\apache-tomcat-8.0.33_19000
set WEB_APP=webapps
set WEB_WORK=work
set BIN=bin
set BACKUP=D:\bak\
set homeUrl=网址

set config_path=C:\替换配置文件
set port=15000

:: A:创建备份文件夹   1
:: B:停止tomcat       2
:: C:备份文件         3
:: D:删除tomcat中文件 4
:: E:拷贝新包         5 
:: F:启动tomcat       6
:: B:停止tomcat       7
:: G:替换文件         8
:: F:启动tomcat       9
set step=0
set sec=60

goto A

::判断备份文件夹是否存在
:A
echo ============================================================================
echo 创建备份文件夹
if exist %BACKUP% (
	echo 备份文件夹 %BACKUP% 存在
) else (
	rem 创建F:\backup\
	echo 创建备份文件夹 %BACKUP%
	md %BACKUP%
)

if exist %BACKUP%%currDate% (
	echo 备份文件夹 %BACKUP%%currDate% 存在
) else (
	echo 创建备份文件夹 %BACKUP%%currDate%
	md %BACKUP%%currDate%
)
::当前备份文件夹
set curr_backup=%BACKUP%%currDate%
set step=1
goto B


:B
echo ============================================================================
echo 停止tomcat
cd /d %CATALINA_HOME%\%BIN%
call shutdown.bat >nul
if errorlevel 0 (
	echo tomcat已关闭。
) else (
	echo tomcat关闭失败了!
	pause
	exit
)
ping 127.1 -n 5 >nul 
if %step%==1 (
	set step=2
	goto C
) else (
	rem 跳转 替换文件
	set step=7
	ping 127.1 -n 5 >nul 
	goto G
)

:C
echo ============================================================================
echo 备份文件
xcopy %CATALINA_HOME%\%WEB_APP% %curr_backup% /e /y >nul
if errorlevel 0 (
	echo 备份完成。
) else (
	echo 备份失败了!
	pause
	exit
)
set step=3
goto D

:D
echo ============================================================================
echo 删除tomcat中的旧文件
rd /q /s %CATALINA_HOME%\%WEB_APP%\ >nul
rd /q /s %CATALINA_HOME%\%WEB_WORK%\ >nul
if errorlevel 0 (
	echo 删除完成。
) else (
	echo 删除失败了!
	pause
	exit
)
md %CATALINA_HOME%\%WEB_APP%
md %CATALINA_HOME%\%WEB_WORK%
set step=4
goto E

:E
echo ============================================================================
echo 拷贝新包
xcopy %WORK_SPACE% %CATALINA_HOME%\%WEB_APP% /e /y >nul
if errorlevel 0 (
	echo 拷贝完成。
) else (
	echo 拷贝失败了!
	pause
	exit
)
set step=5
goto F

:F
echo ============================================================================
echo 启动tomcat
cd /d %CATALINA_HOME%\%BIN%
call startup.bat >nul
if errorlevel 0 (
	echo tomcat已启动。
) else (
	echo tomcat启动失败了!
	pause
	exit
)
if %step%==5 (
	curl -s %homeUrl% >%curr_backup%\check.txt
	ping 127.1 -n %sec% >nul 
	set step=6
	goto B
) else (
	set step=9
	exit
)

:G
echo ============================================================================
echo 替换配置文件
del %CATALINA_HOME%\%WEB_APP%\%PRJ_WAR%
xcopy %config_path% %CATALINA_HOME%\%WEB_APP% /e /y >nul
if errorlevel 0 (
	echo 替换配置文件完成。
) else (
	echo 替换配置文件失败了!
	pause
	exit
)
ping 127.1 -n 5 >nul 
set step=8
goto F

 

你可能感兴趣的:(部署,发布,批处理)