Skip to content

Commit 18866a0

Browse files
committed
Harden Windows JAR build script
1 parent b697a90 commit 18866a0

1 file changed

Lines changed: 82 additions & 50 deletions

File tree

scripts/build-jar.bat

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,118 @@
11
@echo off
2-
REM ═══════════════════════════════════════════════════════════════════════════════
3-
REM NitroWebExpress™ — Build Fat JAR (Windows)
4-
REM Compiles all sources and packages into nwe.jar.
5-
REM Usage: scripts\build-jar.bat
6-
REM ═══════════════════════════════════════════════════════════════════════════════
7-
setlocal enabledelayedexpansion
2+
REM NitroWebExpress — Build Fat JAR (Windows)
3+
REM Location-independent: safe to invoke from any working directory.
4+
setlocal EnableExtensions EnableDelayedExpansion
85

9-
set "ROOT=%~dp0.."
10-
pushd "%ROOT%"
11-
set "ROOT=%CD%"
12-
popd
6+
set "SCRIPT_DIR=%~dp0"
7+
for %%I in ("%SCRIPT_DIR%..") do set "ROOT=%%~fI"
138

149
set "SRC=%ROOT%\source"
1510
set "OUT=%ROOT%\out"
1611
set "JAR_OUT=%ROOT%\nwe.jar"
1712
set "MYSQL_JAR=%ROOT%\jars\mysql\mysql-connector-j-9.7.0.jar"
1813
set "LANTERNA_JAR=%ROOT%\jars\lanterna-3.1.5.jar"
1914
set "DJL_DIR=%ROOT%\jars\djl"
20-
set "STAGING=%TEMP%\nwe-jar-staging"
15+
set "JPCAP_DIR=%ROOT%\jars\jpcap"
16+
set "STAGING=%TEMP%\nwe-jar-staging-%RANDOM%"
17+
set "SOURCE_LIST=%TEMP%\nwe-sources-%RANDOM%.txt"
2118

19+
if not exist "%SRC%" (
20+
echo [FAIL] Source directory missing: %SRC%
21+
exit /b 1
22+
)
23+
if not exist "%MYSQL_JAR%" (
24+
echo [FAIL] Missing dependency: %MYSQL_JAR%
25+
exit /b 1
26+
)
27+
if not exist "%LANTERNA_JAR%" (
28+
echo [FAIL] Missing dependency: %LANTERNA_JAR%
29+
exit /b 1
30+
)
31+
where javac >nul 2>&1 || (echo [FAIL] javac not found. Install/use Java 21.& exit /b 1)
32+
where jar >nul 2>&1 || (echo [FAIL] jar not found. Install/use Java 21.& exit /b 1)
33+
where powershell >nul 2>&1 || (echo [FAIL] PowerShell is required for dependency extraction.& exit /b 1)
34+
35+
set "CP=%OUT%;%MYSQL_JAR%;%LANTERNA_JAR%"
36+
if exist "%DJL_DIR%" for /r "%DJL_DIR%" %%J in (*.jar) do set "CP=!CP!;%%J"
37+
if exist "%JPCAP_DIR%" for /r "%JPCAP_DIR%" %%J in (*.jar) do set "CP=!CP!;%%J"
38+
39+
set "TMP_ROOT=%TEMP%"
2240
echo === NitroWebExpress — JAR Builder (Windows) ===
2341
echo ROOT: %ROOT%
2442
echo.
2543

26-
REM ── 1. Compile ──────────────────────────────────────────────────────────────
27-
echo [1/3] Compiling sources...
2844
if not exist "%OUT%" mkdir "%OUT%"
29-
set "CP=%OUT%;%MYSQL_JAR%;%LANTERNA_JAR%"
30-
for /r "%DJL_DIR%" %%J in (*.jar) do set "CP=!CP!;%%J"
31-
for /r "%ROOT%\jars\jpcap" %%J in (*.jar) do set "CP=!CP!;%%J"
3245

33-
dir /s /b "%SRC%\*.java" > "%TEMP%\nwe-sources.txt" 2>nul
34-
javac --release 21 -cp "%CP%" -sourcepath "%SRC%" -d "%OUT%" @"%TEMP%\nwe-sources.txt" 2>&1
35-
del "%TEMP%\nwe-sources.txt" 2>nul
46+
echo [1/3] Compiling sources...
47+
dir /s /b "%SRC%\*.java" > "%SOURCE_LIST%" 2>nul
48+
if not exist "%SOURCE_LIST%" (
49+
echo [FAIL] No Java sources found under %SRC%
50+
exit /b 1
51+
)
52+
for %%A in ("%SOURCE_LIST%") do if %%~zA==0 (
53+
echo [FAIL] No Java sources found under %SRC%
54+
del "%SOURCE_LIST%" 2>nul
55+
exit /b 1
56+
)
57+
javac --release 21 -cp "%CP%" -sourcepath "%SRC%" -d "%OUT%" @"%SOURCE_LIST%"
58+
if errorlevel 1 (
59+
echo [FAIL] Java compilation failed.
60+
del "%SOURCE_LIST%" 2>nul
61+
exit /b 1
62+
)
63+
del "%SOURCE_LIST%" 2>nul
3664
echo Compiled.
3765

38-
REM ── 2. Assemble fat JAR ────────────────────────────────────────────────────
3966
echo [2/3] Assembling fat JAR...
4067
if exist "%STAGING%" rmdir /S /Q "%STAGING%"
4168
mkdir "%STAGING%"
69+
if errorlevel 1 exit /b 1
70+
xcopy /E /I /Y "%OUT%\*" "%STAGING%\" >nul
71+
if errorlevel 1 exit /b 1
4272

43-
REM Copy application classes
44-
xcopy /E /I /Y "%OUT%\*" "%STAGING%\" >nul 2>&1
73+
call :extract "%MYSQL_JAR%"
74+
if errorlevel 1 exit /b 1
75+
call :extract "%LANTERNA_JAR%"
76+
if errorlevel 1 exit /b 1
4577

46-
REM Extract dependency JARs
47-
if exist "%MYSQL_JAR%" (
48-
powershell -NoProfile -Command "Expand-Archive -Path '%MYSQL_JAR%' -DestinationPath '%STAGING%' -Force" 2>nul
49-
del "%STAGING%\META-INF\MANIFEST.MF" 2>nul
50-
del "%STAGING%\META-INF\*.SF" 2>nul
51-
del "%STAGING%\META-INF\*.RSA" 2>nul
78+
if exist "%DJL_DIR%" for /r "%DJL_DIR%" %%J in (*.jar) do (
79+
echo %%~nxJ | findstr /i "native" >nul
80+
if errorlevel 1 (
81+
call :extract "%%J"
82+
if errorlevel 1 exit /b 1
83+
)
5284
)
53-
if exist "%LANTERNA_JAR%" (
54-
powershell -NoProfile -Command "Expand-Archive -Path '%LANTERNA_JAR%' -DestinationPath '%STAGING%' -Force" 2>nul
55-
del "%STAGING%\META-INF\MANIFEST.MF" 2>nul
56-
del "%STAGING%\META-INF\*.SF" 2>nul
57-
)
58-
59-
REM DJL jars (skip native blobs)
60-
for /r "%DJL_DIR%" %%J in (*.jar) do (
61-
echo %%~nxJ | findstr /i "native" >nul || (
62-
powershell -NoProfile -Command "Expand-Archive -Path '%%J' -DestinationPath '%STAGING%' -Force" 2>nul
63-
del "%STAGING%\META-INF\MANIFEST.MF" 2>nul
64-
del "%STAGING%\META-INF\*.SF" 2>nul
65-
)
85+
if exist "%JPCAP_DIR%" for /r "%JPCAP_DIR%" %%J in (*.jar) do (
86+
call :extract "%%J"
87+
if errorlevel 1 exit /b 1
6688
)
6789

68-
REM Write manifest
6990
if not exist "%STAGING%\META-INF" mkdir "%STAGING%\META-INF"
7091
(
71-
echo Manifest-Version: 1.0
72-
echo Main-Class: Main
73-
echo Class-Path: jars/djl/pytorch-native-cpu-2.5.1-linux-x86_64.jar
92+
echo Manifest-Version: 1.0
93+
echo Main-Class: Main
94+
echo Class-Path: jars/djl/pytorch-native-cpu-2.5.1-linux-x86_64.jar
7495
) > "%STAGING%\META-INF\MANIFEST.MF"
7596

76-
REM ── 3. Create JAR ──────────────────────────────────────────────────────────
7797
echo [3/3] Creating %JAR_OUT%...
7898
jar cfm "%JAR_OUT%" "%STAGING%\META-INF\MANIFEST.MF" -C "%STAGING%" .
99+
if errorlevel 1 (
100+
echo [FAIL] JAR creation failed.
101+
rmdir /S /Q "%STAGING%" 2>nul
102+
exit /b 1
103+
)
79104

80-
REM Cleanup staging
81105
rmdir /S /Q "%STAGING%" 2>nul
82-
83106
for %%F in ("%JAR_OUT%") do echo Done. Size: %%~zF bytes
84-
echo.
85107
echo === Run with: java -jar nwe.jar ===
86-
endlocal
108+
exit /b 0
109+
110+
:extract
111+
if not exist "%~1" exit /b 0
112+
powershell -NoProfile -Command "$ErrorActionPreference='Stop'; Expand-Archive -LiteralPath '%~1' -DestinationPath '%STAGING%' -Force" >nul
113+
if errorlevel 1 exit /b 1
114+
del "%STAGING%\META-INF\MANIFEST.MF" 2>nul
115+
del "%STAGING%\META-INF\*.SF" 2>nul
116+
del "%STAGING%\META-INF\*.RSA" 2>nul
117+
del "%STAGING%\META-INF\*.DSA" 2>nul
118+
exit /b 0

0 commit comments

Comments
 (0)