Files
workutils/scripts/build_release.bat
T
2026-06-19 17:25:47 +08:00

181 lines
5.7 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@echo off
chcp 65001 >nul
setlocal
:: ============================================
:: WorkUtils 生产打包脚本(免安装版)
:: 输出:target\dist\workutils-v{version}.zip
:: (含 exe + 内嵌JRE + jar + 启动脚本)
:: ============================================
::: 切换到项目根目录
cd /d "%~dp0.."
set "PROJECT_DIR=%CD%"
set "TARGET=%PROJECT_DIR%\target"
set "DIST=%TARGET%\dist"
set "VERSION="
echo.
echo ========== WorkUtils 生产构建 ==========
echo 项目目录: %PROJECT_DIR%
echo.
::: ============================================================
::: 步骤 1:清理残留进程
::: ============================================================
echo [1/5] Clean stale processes...
taskkill /f /im java.exe 2>nul
taskkill /f /im workutils.exe 2>nul
::: ============================================================
::: 步骤 2:Maven 打包
::: ============================================================
echo.
echo [2/5] Maven build...
call "%PROJECT_DIR%\mvnw.cmd" clean package -DskipTests -q
if %errorlevel% neq 0 (
echo [ERROR] Maven build failed!
pause
exit /b 1
)
::: ============================================================
::: 步骤 3:提取版本号(PowerShell 解析 pom.xml)
::: ============================================================
echo.
echo [3/5] Read version from pom.xml...
for /f "usebackq delims=" %%a in (`powershell -NoProfile -NonInteractive -Command "([xml](Get-Content -Encoding UTF8 '%PROJECT_DIR%\pom.xml')).project.version"`) do set "VERSION=%%a"
if "%VERSION%"=="" set "VERSION=0.0.1-SNAPSHOT"
echo 版本: %VERSION%
::: ============================================================
::: 步骤 4:生成精简 JRE(jlink 内嵌 Java 17 + JavaFX)
::: ============================================================
echo.
echo [4/5] Generate bundled JRE...
::: 4.1 查找 JDK 17(环境变量 JDK17_HOME > 默认路径)
if not defined JDK17_HOME (
for %%d in (
"C:\Program Files\Java\jdk-17"
"C:\Program Files\Eclipse Adoptium\jdk-17.0.0.35-hotspot"
"C:\Program Files\Java\jdk-17.0.14"
"%JAVA_HOME%"
) do (
if exist "%%~d\bin\jlink.exe" (
set "JDK17_HOME=%%~d"
goto :jdk17_found
)
)
echo [ERROR] Cannot find JDK 17 with jlink.exe
echo Please set JDK17_HOME environment variable
pause & exit /b 1
)
:jdk17_found
::: 4.2 查找 JavaFX SDK(环境变量 JFX_SDK_HOME > 默认路径)
if not defined JFX_SDK_HOME (
for %%d in (
"C:\Program Files\Java\javafx-sdk-17.0.15"
"C:\Program Files\Java\javafx-sdk-17.0.14"
"C:\Program Files\Java\javafx-sdk-17"
) do (
if exist "%%~d\lib\javafx.base.jar" (
set "JFX_SDK_HOME=%%~d"
goto :jfx_found
)
)
echo [ERROR] Cannot find JavaFX SDK
echo Please set JFX_SDK_HOME environment variable
pause & exit /b 1
)
:jfx_found
echo JDK : %JDK17_HOME%
echo JavaFX: %JFX_SDK_HOME%
::: 4.3 清理旧 JRE
if exist "%TARGET%\jre" rd /s /q "%TARGET%\jre"
::: 4.4 执行 jlink
echo Running jlink, please wait...
"%JDK17_HOME%\bin\jlink.exe" ^
--module-path "%JFX_SDK_HOME%\lib;%JDK17_HOME%\jmods" ^
--add-modules java.base,java.desktop,java.logging,java.management,java.naming,java.sql,java.xml,java.xml.crypto,java.instrument,java.security.jgss,java.security.sasl,jdk.unsupported,jdk.management,java.scripting,jdk.crypto.ec,java.net.http,javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.web,javafx.swing ^
--output "%TARGET%\jre" ^
--strip-debug --no-header-files --no-man-pages --compress=2
if %errorlevel% neq 0 (
echo [ERROR] jlink failed!
pause
exit /b 1
)
echo [OK] JRE generated : %TARGET%\jre
::: ============================================================
::: 步骤 5:组装发布目录 & 打包 ZIP
::: ============================================================
echo.
echo [5/5] Assemble distribution...
::: 清理旧发布目录
if exist "%DIST%" rd /s /q "%DIST%"
mkdir "%DIST%\workutils" 2>nul
::: 复制 EXE
if exist "%TARGET%\workutils.exe" (
copy /y "%TARGET%\workutils.exe" "%DIST%\workutils\" >nul
echo [OK] workutils.exe
) else (
echo [WARN] workutils.exe not found!
)
::: 复制 JAR
for %%j in ("%TARGET%\workutils-*.jar") do (
copy /y "%%j" "%DIST%\workutils\" >nul
echo [OK] %%~nxj
)
::: 复制内嵌 JRE
if exist "%TARGET%\jre\bin\java.exe" (
xcopy /e /i /q "%TARGET%\jre" "%DIST%\workutils\jre\" >nul
echo [OK] jre/ ^(bundled Java 17 + JavaFX^)
) else (
echo [WARN] jre not found in target!
)
::: ============================================================
::: 生成 start.bat(备选启动方式)
::: ============================================================
(
echo @echo off
echo chcp 65001 ^>nul
echo title WorkUtils
echo java -jar "%%~dp0workutils-%VERSION%.jar" %%*
echo pause
) > "%DIST%\workutils\start.bat"
echo [OK] start.bat
::: ============================================================
::: 打包 ZIP
::: ============================================================
set "ZIPNAME=workutils-v%VERSION%"
echo 正在压缩 %ZIPNAME%.zip ...
powershell -Command ^
"Compress-Archive -Path '%DIST%\workutils' -DestinationPath '%DIST%\%ZIPNAME%.zip' -Force"
echo [OK] %ZIPNAME%.zip
::: ============================================================
::: 完成
::: ============================================================
echo.
echo ============================================
echo Build Complete ^! v%VERSION%
echo ============================================
echo Output:
echo %DIST%\workutils\
echo %DIST%\%ZIPNAME%.zip
echo ============================================
echo.
pause