#### # Windows 部署脚本

只需要把 jar 和 yml 跟批处理放在同一目录下即可点击快速启动。启动后再次点击会关闭上次启动的窗口并重新启动。

拷贝以下代码放入 txt 文本，然后改为 start.bat

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63

    @ECHO OFF
    setlocal enabledelayedexpansion

    REM 关闭上次进程
    SET "pidFile=pid.txt"
    if exist "%pidFile%" (
    	for /f "usebackq" %%a in ("pid.txt") do (
    		set PID=%%a
    	)
    	if not "!PID!"=="" (
    		taskkill /F /T /PID !pid!
    		del pid.txt
    	)
    )

    REM 存储当前进程
    for /f %%i in ('wmic process where "name='cmd.exe' and CommandLine like '%%<scriptname>.bat%%'" get ParentProcessId ^| findstr /r "[0-9]"') do set pid=%%i
    echo %PID% > pid.txt

    REM 设置title
    for /f "tokens=2" %%i in ('chcp') do set codepage=%%i
    chcp 65001 > nul
    title 我的SpringBoot项目
    chcp %codepage% > nul

    cd %~dp0

    REM 获取jar
    set "jarFile="
    for %%i in (*.jar) do (
        if not defined jarFile (
            set "jarFile=%%i"
        )
    )

    if not defined jarFile (
        echo not find jar
        pause
        exit
    )

    SET JAVA_OPTS=-Djava.security.egd=file:/dev/./urandom -Dfile.encoding=UTF-8
    set JAVA_OPTS=%JAVA_OPTS% -Dsun.jnu.encoding=UTF8 -Xms512m -Xmx1024m
    set JAVA_OPTS=%JAVA_OPTS% -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5007
    set JAVA_OPTS=%JAVA_OPTS% -Dpid.path=./temp

    REM 获取yml
    set "ymlFile="
    for %%i in (*.yml) do (
        if not defined ymlFile (
            set "ymlFile=%%i"
        )
    )

    if defined ymlFile (
    	set JAVA_OPTS=%JAVA_OPTS% -Dspring.config.additional-location=!ymlFile!
    ) else (
    	echo not find yml
    )

    REM 启动服务
    java %JAVA_OPTS% -jar !jarFile!
    pause

拷贝以下代码放入 txt 文本，然后改为 stop.bat

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    @ECHO OFF
    setlocal enabledelayedexpansion

    REM 关闭上次进程
    SET "pidFile=pid.txt"
    if exist "%pidFile%" (
    	for /f "usebackq" %%a in ("pid.txt") do (
    		set PID=%%a
    	)
    	if not "!PID!"=="" (
    		taskkill /F /T /PID !pid!
    		del pid.txt
    	)
    )

    exit

SpringBoot 快速部署 Bat 批处理