|
1 | 1 | @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 |
8 | 5 |
|
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" |
13 | 8 |
|
14 | 9 | set "SRC=%ROOT%\source" |
15 | 10 | set "OUT=%ROOT%\out" |
16 | 11 | set "JAR_OUT=%ROOT%\nwe.jar" |
17 | 12 | set "MYSQL_JAR=%ROOT%\jars\mysql\mysql-connector-j-9.7.0.jar" |
18 | 13 | set "LANTERNA_JAR=%ROOT%\jars\lanterna-3.1.5.jar" |
19 | 14 | 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" |
21 | 18 |
|
| 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%" |
22 | 40 | echo === NitroWebExpress — JAR Builder (Windows) === |
23 | 41 | echo ROOT: %ROOT% |
24 | 42 | echo. |
25 | 43 |
|
26 | | -REM ── 1. Compile ────────────────────────────────────────────────────────────── |
27 | | -echo [1/3] Compiling sources... |
28 | 44 | 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" |
32 | 45 |
|
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 |
36 | 64 | echo Compiled. |
37 | 65 |
|
38 | | -REM ── 2. Assemble fat JAR ──────────────────────────────────────────────────── |
39 | 66 | echo [2/3] Assembling fat JAR... |
40 | 67 | if exist "%STAGING%" rmdir /S /Q "%STAGING%" |
41 | 68 | mkdir "%STAGING%" |
| 69 | +if errorlevel 1 exit /b 1 |
| 70 | +xcopy /E /I /Y "%OUT%\*" "%STAGING%\" >nul |
| 71 | +if errorlevel 1 exit /b 1 |
42 | 72 |
|
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 |
45 | 77 |
|
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 | + ) |
52 | 84 | ) |
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 |
66 | 88 | ) |
67 | 89 |
|
68 | | -REM Write manifest |
69 | 90 | if not exist "%STAGING%\META-INF" mkdir "%STAGING%\META-INF" |
70 | 91 | ( |
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 |
74 | 95 | ) > "%STAGING%\META-INF\MANIFEST.MF" |
75 | 96 |
|
76 | | -REM ── 3. Create JAR ────────────────────────────────────────────────────────── |
77 | 97 | echo [3/3] Creating %JAR_OUT%... |
78 | 98 | 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 | +) |
79 | 104 |
|
80 | | -REM Cleanup staging |
81 | 105 | rmdir /S /Q "%STAGING%" 2>nul |
82 | | - |
83 | 106 | for %%F in ("%JAR_OUT%") do echo Done. Size: %%~zF bytes |
84 | | -echo. |
85 | 107 | 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