SpringBoot 的命令行启动

1. Windows CMD

主要是方便本地调试。

使用 spring-boot 插件

spring-boot-maven-plugin-2.2.2.RELEASE

@ECHO OFF
SETLOCAL

REM 请设置基本环境变量
SET JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_45
SET M2_HOME=C:\Program Files (x86)\apache-maven-3.6.3
SET DEBUG_PORT=8888
SET LOG_PREFIX=D:\log
SET CLEAN_CMD=

REM 自动检测当前目录
SET APP_HOME=%~dp0

REM 自动获取当前目录名
SET APP_NAME=
SET TMP_ARRAY=%APP_HOME:\= %
FOR %%a in (%TMP_ARRAY%) do SET APP_NAME=%%a

REM 日志目录
SET LOG_HOME=%LOG_PREFIX%\%APP_NAME%

REM 设置变量
SET JAVA_OPTS=-Xms1024M -Xmx1024M -Dlog.home=%LOG_HOME%
SET JAVA_DEBUG=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=%DEBUG_PORT%,server=y,suspend=n
SET jvmArguments="%JAVA_OPTS% %JAVA_DEBUG%"

ECHO=
ECHO JAVA_HOME=%JAVA_HOME%
ECHO M2_HOME=%M2_HOME%
ECHO APP_HOME=%APP_HOME%
ECHO APP_NAME=%APP_NAME%
ECHO LOG_HOME=%LOG_HOME%
ECHO DEBUG_PORT=%DEBUG_PORT%

IF "%1%"=="c" SET CLEAN_CMD=clean
ECHO CLEAN=%CLEAN_CMD%
IF "%1%"=="h" GOTO HELP

:MAIN
REM 执行程序
CD %APP_HOME% && CALL "%M2_HOME%\bin\mvn" -q -Dmaven.test.skip=true -Dspring-boot.run.noverify=true -Dspring-boot.run.jvmArguments=%jvmArguments% %CLEAN_CMD% spring-boot:run
GOTO END

:HELP
ECHO=
ECHO Usage:
ECHO     start.cmd      compile and start
ECHO     start.cmd c    clean, compile and start
ECHO     start.cmd h    show help
GOTO END

:END
ENDLOCAL

使用 java 命令

@ECHO OFF
SETLOCAL

REM 请设置基本环境变量
SET JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_45
SET M2_HOME=C:\Program Files (x86)\apache-maven-3.6.3
SET DEBUG_PORT=8888
SET LOG_PREFIX=D:\log
SET CLEAN_CMD=

REM 自动检测当前目录
SET APP_HOME=%~dp0
SET JAR_HOME=%APP_HOME%target
SET CFG_HOME=D:\wzctmp\java-demo\config

REM 自动获取当前目录名
SET APP_NAME=
SET TMP_ARRAY=%APP_HOME:\= %
FOR %%a in (%TMP_ARRAY%) DO SET APP_NAME=%%a

REM 日志目录
SET LOG_HOME=%LOG_PREFIX%\%APP_NAME%

REM 设置变量
SET JAVA_OPTS=-Xms1024M -Xmx1024M -Dlog.home=%LOG_HOME%
SET JAVA_DEBUG=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=%DEBUG_PORT%,server=y,suspend=n
SET jvmArguments="%JAVA_OPTS% %JAVA_DEBUG%"

ECHO=
ECHO JAVA_HOME=%JAVA_HOME%
ECHO M2_HOME=%M2_HOME%
ECHO APP_HOME=%APP_HOME%
ECHO APP_NAME=%APP_NAME%
ECHO JAR_HOME=%JAR_HOME%
ECHO CFG_HOME=%CFG_HOME%
ECHO LOG_HOME=%LOG_HOME%
ECHO DEBUG_PORT=%DEBUG_PORT%

IF "%1%"=="c" SET CLEAN_CMD=clean
ECHO CLEAN=%CLEAN_CMD%
IF "%1%"=="h" GOTO HELP

:MAIN
REM 打包
CD %APP_HOME% && CALL "%M2_HOME%\bin\mvn" -q -Dmaven.test.skip=true %CLEAN_CMD% package

REM 查找JAR_FILE
IF EXIST %JAR_HOME% CD %JAR_HOME% && FOR %%i in (*.jar) DO SET JAR_FILE=%%i
IF "%JAR_FILE%"=="" echo ****** COMPILE ERROR ****** && GOTO END
ECHO JAR_FILE=%JAR_FILE%

REM 设置CLASSPATH
SET CLASSPATH=%CFG_HOME%;%JAR_HOME%\%JAR_FILE%
ECHO CLASSPATH=%CLASSPATH%

REM 执行程序
CALL "%JAVA_HOME%\bin\java" %JAVA_OPTS% %JAVA_DEBUG% -cp %CLASSPATH% org.springframework.boot.loader.JarLauncher

GOTO END

:HELP
ECHO=
ECHO Usage:
ECHO     start.cmd      compile and start
ECHO     start.cmd c    clean, compile and start
ECHO     start.cmd h    show help
GOTO END

:END
ENDLOCAL

2. Linux Shell

TODO