bat运行springcloud

第一步编写每一个项目的构建、启动、杀死脚本

@echo off

::默认PID,无需修改
set "PID=999999"
::记录当前目录,无需修改
set "CURRENT_PATH=%cd%"

::指定java_home路径,不能起名JAVA_HOME,会覆盖环境变量
set "JAVA_HOME_CUSTOM=D:\DevTool\jdk1.8.0_181"
::指定jre_home
set "JRE_HOME_CUSTOM=%JAVA_HOME_CUSTOM%\jre"
::指定maven_home,不能起名MAVEN_HOME,会覆盖环境变量
set "MAVEN_HOME_CUSTOM=D:\DevTool\apache-maven-3.5.4"


::指定程序工作路径
set "SERVICE_DIR=D:\xiaowei1\gitlabdep\chanpay-eureka\chanpay-eureka"
::指定程序包名
set "JARNAME=chanpay-eureka.jar"
::指定程序端口号
set "port=9080"
::指定程序启动日志名
set "LOG_FILE=chanpay-eureka.log"



::流程控制
if "%1"=="start" (
  call:START
) else (
  if "%1"=="stop" ( 
    call:STOP 
  ) else ( 
    if "%1"=="restart" (
	  call:RESTART 
	) else ( 
	  call:DEFAULT 
	)
  )
)

goto:eof


::启动jar包
:START
echo function "start" starting...
cd /d %SERVICE_DIR%
call %MAVEN_HOME_CUSTOM%\bin\mvn clean install
echo "%JRE_HOME_CUSTOM%\bin\java"
start /b "%JRE_HOME_CUSTOM%\bin\" java.exe -Xms128m -Xmx512m -jar %SERVICE_DIR%\target\%JARNAME% > %SERVICE_DIR%\logs\%LOG_FILE%
cd /d %CURRENT_PATH%
echo == service start success
goto:eof


::停止java程序运行
:STOP
echo function "stop" starting...
call:findPid
call:shutdown
echo == service stop success
goto:eof


::重启jar包
:RESTART
echo function "restart" starting...
call:STOP
call:sleep2
call:START
echo == service restart success
goto:eof


::执行默认方法--重启jar包
:DEFAULT
echo Now choose default item : restart
call:STOP
call:sleep2
call:START
echo == service restart success
goto:eof


::找到端口对应程序的pid
:findPid
echo function "findPid" start.
for /f "tokens=5" %%i in ('netstat -aon ^| findstr %port%') do (
    set "PID=%%i"
)
if "%PID%"=="999999" ( echo pid not find, skip stop . ) else ( echo pid is %PID%. )
goto:eof


::杀死pid对应的程序
:shutdown
if not "%PID%"=="999999" ( taskkill /f /pid %PID% )
goto:eof


::延时5秒
:sleep5
ping 127.0.0.1 -n 5 >nul
goto:eof


::延时2秒
:sleep2
ping 127.0.0.1 -n 2 >nul
goto:eof

第二步编写上一步写的脚本的调用脚本

@echo off

::其他bat工作路径
set "OTHER_BAT_WORK_SPACE=D:\xiaowei1\startBat"
::记录当前目录,无需修改
set "CURRENT_ROOT_PATH=%cd%"
::启动命令(start|stop|restart|其他默认重启)
set "PARAM1="



::流程控制
if "%1"=="start" (
  set "PARAM1=%1"
) else (
  if "%1"=="stop" ( 
    set "PARAM1=%1"
  ) else ( 
    if "%1"=="restart" (
	  set "PARAM1=%1"
	) else ( 
	  if "%1"=="" (
	    set "PARAM1=restart" 
	  ) else (
	    set "PARAM1=restart"
	  )
	)
  )
)

::项目主流程开始

echo facade.bat starting...
call %OTHER_BAT_WORK_SPACE%\facade.bat %PARAM1%

echo eureka.bat starting...
call %OTHER_BAT_WORK_SPACE%\eureka.bat %PARAM1%


echo service-base.bat starting...
call %OTHER_BAT_WORK_SPACE%\service-base.bat %PARAM1%

echo service-cache.bat starting...
call %OTHER_BAT_WORK_SPACE%\service-cache.bat %PARAM1%

echo service-monitor.bat starting...
call %OTHER_BAT_WORK_SPACE%\service-monitor.bat %PARAM1%

echo service-permission.bat starting...
call %OTHER_BAT_WORK_SPACE%\service-permission.bat %PARAM1%

echo hsm-keyou.bat starting...
call %OTHER_BAT_WORK_SPACE%\hsm-keyou.bat %PARAM1%

cd /d %CURRENT_ROOT_PATH%

echo ---all services have started successful---

goto:eof

至此大功告成。

ps:别忘了替换文件开始的配置

双击最后的脚本就会运行你所配置的项目了。当然,要停止的话,可以有两种方法。

1. 直接关闭命令行

2. 命令行执行 xxx.bat stop

你可能感兴趣的:(bat,java,springboot,批处理,windows,批处理脚本)