summaryrefslogtreecommitdiffstats
path: root/build.bat
blob: eb6b46087fe6995cf0b9f5e29f5593fbc54f02d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@echo off
setlocal enabledelayedexpansion

echo === 1. Cleaning Workspace ===
if exist bin   rmdir /s /q bin
if exist stage rmdir /s /q stage
if exist dist  rmdir /s /q dist
mkdir bin\classes
mkdir bin\test-classes
mkdir stage
mkdir dist

echo === 2. Compiling Production Code ===
dir /s /b src\main\java\*.java > sources.txt
javac -d bin\classes -cp "lib/*" @sources.txt
if %errorlevel% neq 0 exit /b %errorlevel%

echo === 3. Compiling Test Code ===
dir /s /b src\test\java\*.java > test-sources.txt
javac -d bin\test-classes -cp "bin\classes;lib/*" @test-sources.txt
if %errorlevel% neq 0 exit /b %errorlevel%

echo === 4. Running Automated Tests ===
set "JUNIT_JAR="
for %%f in (lib\junit-platform-console-standalone-*.jar) do set "JUNIT_JAR=%%f"
if not defined JUNIT_JAR (echo JUnit jar missing. & exit /b 1)

rem JUnit's --class-path does NOT expand lib\*  -> build it explicitly
set "TEST_CP=bin\classes;bin\test-classes"
for %%j in (lib\*.jar) do set "TEST_CP=!TEST_CP!;%%j"

java -jar "!JUNIT_JAR!" execute -cp "!TEST_CP!" --scan-classpath
if %errorlevel% neq 0 (
    echo [X] Tests failed! Aborting build package.
    exit /b %errorlevel%
)
echo [OK] Tests passed successfully!

echo === 5. Extracting Dependencies for Fat JAR ===
cd stage
for %%j in (..\lib\*.jar) do (
    echo %%~nxj | findstr /i "junit-platform-console-standalone" >nul
    if errorlevel 1 (
        echo Extracting: %%~nxj
        jar -xf "%%j"
    )
)

rem Strip signatures + manifests (they break repacking) but KEEP
rem META-INF\services so ServiceLoader-based deps still work at runtime.
if exist META-INF (
    del /s /q META-INF\*.SF  >nul 2>&1
    del /s /q META-INF\*.RSA >nul 2>&1
    del /s /q META-INF\*.DSA >nul 2>&1
    del /q    META-INF\MANIFEST.MF >nul 2>&1
)
cd ..

echo === 6. Merging Assets into Fat JAR ===
rem Merge ONLY production classes on top of the extracted deps
xcopy /e /q /y /i bin\classes\* stage\ >nul
rem Package the whole tree (deps + your classes), not just stage\classes
jar cfe dist/app.jar com.example.App -C stage .
if %errorlevel% neq 0 exit /b %errorlevel%

del sources.txt test-sources.txt >nul 2>&1
rmdir /s /q stage
echo ======================================
echo Fat JAR Created! Run it with:
echo java -jar dist/app.jar
echo ======================================
pause