Files
workutils/scripts/build_release.bat
T
2026-08-03 15:37:06 +08:00

120 lines
3.7 KiB
Batchfile

@echo off
chcp 65001 >nul
setlocal EnableExtensions EnableDelayedExpansion
cd /d "%~dp0.."
set "PROJECT_DIR=%CD%"
set "TARGET=%PROJECT_DIR%\target"
set "DIST=%TARGET%\dist"
set "RELEASE_DIR=%DIST%\workutils"
set "BUILD_JDK="
echo.
echo ========== WorkUtils Windows portable release ==========
echo Project: %PROJECT_DIR%
call :try_jdk "%JDK17_HOME%"
call :try_jdk "%JAVA_HOME%"
call :try_jdk "C:\Program Files\Java\jdk-17"
call :try_jdk "C:\Program Files\Eclipse Adoptium\jdk-17"
if not defined BUILD_JDK (
echo [ERROR] A JDK 17 or newer with jlink.exe is required to build the release.
echo Set JAVA_HOME or JDK17_HOME to a valid JDK directory.
pause
exit /b 1
)
set "JAVA_HOME=%BUILD_JDK%"
set "PATH=%JAVA_HOME%\bin;%PATH%"
echo Build JDK: %JAVA_HOME%
echo.
echo [1/5] Build and test executable JAR...
call "%PROJECT_DIR%\mvnw.cmd" clean package
if errorlevel 1 goto :build_failed
echo.
echo [2/5] Read project version...
set "VERSION="
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 not defined VERSION (
echo [ERROR] Unable to read the project version from pom.xml.
pause
exit /b 1
)
set "JAR_NAME=workutils-%VERSION%.jar"
set "JAR_PATH=%TARGET%\%JAR_NAME%"
if not exist "%JAR_PATH%" (
echo [ERROR] Executable JAR not found: %JAR_PATH%
pause
exit /b 1
)
echo Version: %VERSION%
echo.
echo [3/5] Assemble release directory...
if exist "%DIST%" rd /s /q "%DIST%"
mkdir "%RELEASE_DIR%" || goto :build_failed
copy /y "%JAR_PATH%" "%RELEASE_DIR%\%JAR_NAME%" >nul || goto :build_failed
powershell -NoProfile -NonInteractive -Command ^
"$text = Get-Content -Raw -Encoding UTF8 '%PROJECT_DIR%\scripts\start-release.bat';" ^
"$text = $text.Replace('@APP_JAR@', '%JAR_NAME%');" ^
"[IO.File]::WriteAllText('%RELEASE_DIR%\start.bat', $text, [Text.UTF8Encoding]::new($false))"
if errorlevel 1 goto :build_failed
echo.
echo [4/5] Generate bundled Java runtime...
"%JAVA_HOME%\bin\jlink.exe" ^
--module-path "%JAVA_HOME%\jmods" ^
--add-modules ALL-MODULE-PATH ^
--bind-services ^
--strip-debug ^
--no-header-files ^
--no-man-pages ^
--compress=2 ^
--output "%RELEASE_DIR%\runtime"
if errorlevel 1 goto :build_failed
"%RELEASE_DIR%\runtime\bin\java.exe" -version
if errorlevel 1 goto :build_failed
echo.
echo [5/5] Create portable ZIP...
set "ZIP_PATH=%DIST%\workutils-v%VERSION%-windows.zip"
powershell -NoProfile -NonInteractive -Command ^
"Compress-Archive -Path '%RELEASE_DIR%' -DestinationPath '%ZIP_PATH%' -Force"
if errorlevel 1 goto :build_failed
echo.
echo ============================================
echo Build complete. The recipient needs no Java installation.
echo Directory: %RELEASE_DIR%
echo ZIP: %ZIP_PATH%
echo ============================================
exit /b 0
:try_jdk
if defined BUILD_JDK exit /b 0
set "CANDIDATE=%~1"
if not defined CANDIDATE exit /b 0
if not exist "!CANDIDATE!\bin\java.exe" exit /b 0
if not exist "!CANDIDATE!\bin\jlink.exe" exit /b 0
set "CANDIDATE_VERSION="
set "VERSION_FILE=%TEMP%\workutils-java-version-!RANDOM!.txt"
"!CANDIDATE!\bin\java.exe" -version > "!VERSION_FILE!" 2>&1
for /f "tokens=3" %%v in ('findstr /i "version" "!VERSION_FILE!"') do set "CANDIDATE_VERSION=%%~v"
del /q "!VERSION_FILE!" >nul 2>&1
set "CANDIDATE_MAJOR="
for /f "tokens=1 delims=." %%m in ("!CANDIDATE_VERSION!") do set "CANDIDATE_MAJOR=%%m"
if "!CANDIDATE_MAJOR!"=="1" for /f "tokens=2 delims=." %%m in ("!CANDIDATE_VERSION!") do set "CANDIDATE_MAJOR=%%m"
if not defined CANDIDATE_MAJOR exit /b 0
if !CANDIDATE_MAJOR! GEQ 17 set "BUILD_JDK=!CANDIDATE!"
exit /b 0
:build_failed
echo.
echo [ERROR] Windows portable release build failed.
pause
exit /b 1