@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