100 lines
2.2 KiB
Batchfile
100 lines
2.2 KiB
Batchfile
@echo off
|
||
chcp 65001 >nul
|
||
setlocal enabledelayedexpansion
|
||
|
||
:: 强制切换到脚本所在目录(解决管理员模式启动默认在System32的问题)
|
||
cd /d "%~dp0"
|
||
|
||
echo 正确盘符: %~d0
|
||
echo 正确路径: %cd%
|
||
|
||
:: 使用/d参数强制切换驱动器+目录
|
||
cd /d "D:\code\server" 2>nul
|
||
if %errorlevel% neq 0 (
|
||
echo [ERROR] Can not enter D:\code\server
|
||
echo 请检查:
|
||
echo 1. D盘是否存在
|
||
echo 2. code目录是否存在
|
||
echo 3. 是否有权限访问
|
||
exit /b
|
||
)
|
||
|
||
echo 扫描子目录中...
|
||
dir /ad /b
|
||
|
||
set "first_dir="
|
||
for /f "delims=" %%d in ('dir /ad /b 2^>nul') do (
|
||
set "first_dir=%%d"
|
||
goto :dir_found
|
||
)
|
||
|
||
:dir_found
|
||
if not defined first_dir (
|
||
echo [ERROR] No subdirectory found
|
||
exit /b
|
||
)
|
||
|
||
echo 进入子目录: !first_dir!
|
||
cd /d "!first_dir!"
|
||
echo 当前路径: %cd%
|
||
|
||
if not exist "target\" (
|
||
echo 执行Maven构建...
|
||
call mvn clean package
|
||
) else (
|
||
echo 检测到 target 目录,跳过Maven构建。
|
||
)
|
||
|
||
echo 进入target目录...
|
||
cd target
|
||
echo 目录内容:
|
||
dir /b
|
||
|
||
:: ========== 新增端口检查功能 ==========
|
||
echo 正在检查8080端口占用...
|
||
set "port_pid="
|
||
for /f "tokens=5" %%p in ('netstat -ano -p tcp ^| findstr ":8080" ^| findstr "LISTENING"') do (
|
||
set "port_pid=%%p"
|
||
echo 检测到端口占用PID: !port_pid!
|
||
taskkill /F /PID !port_pid! >nul 2>&1 && (
|
||
echo 成功终止进程: !port_pid!
|
||
) || (
|
||
echo [警告] 无法终止进程: !port_pid!
|
||
echo 可能需要管理员权限,请右键用管理员身份运行
|
||
)
|
||
)
|
||
|
||
if not defined port_pid (
|
||
echo 8080端口未被占用
|
||
)
|
||
|
||
:: ========== 端口检查结束 ==========
|
||
|
||
set "jar_file="
|
||
for /f "delims=" %%j in ('dir /b /a-d *.jar ^| findstr /v "original.jar"') do (
|
||
set "jar_file=%%j"
|
||
goto :jar_found
|
||
)
|
||
|
||
:jar_found
|
||
if not defined jar_file (
|
||
echo [ERROR] No valid jar found
|
||
echo 建议检查:
|
||
echo 1. Maven构建是否成功
|
||
echo 2. target目录内容
|
||
dir /b *.jar
|
||
exit /b
|
||
)
|
||
|
||
echo 启动Jar包: !jar_file!
|
||
java -jar "!jar_file!"
|
||
if %errorlevel% neq 0 (
|
||
echo [ERROR] Java启动失败 (code: %errorlevel%)
|
||
echo 可能原因:
|
||
echo 1. 缺少依赖
|
||
echo 2. 端口占用
|
||
echo 3. 配置错误
|
||
pause
|
||
)
|
||
|