From b9c9c4f21cb8b40b240593e240754d5bce54a023 Mon Sep 17 00:00:00 2001 From: Kostya Date: Sun, 26 Jul 2026 18:29:52 +0300 Subject: fix build scripts --- build.bat | 58 +++++++++++++++++++++++++++++++++++++--------------------- build.sh | 48 ++++++++++++++++++++++++------------------------ 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/build.bat b/build.bat index e1f3d72..eb6b460 100644 --- a/build.bat +++ b/build.bat @@ -2,20 +2,22 @@ setlocal enabledelayedexpansion echo === 1. Cleaning Workspace === -if exist bin rmdir /s /q bin -if exist bin-test rmdir /s /q bin-test -if exist fat-stage rmdir /s /q fat-stage -if exist app.jar del app.jar -mkdir bin -mkdir bin-test -mkdir fat-stage +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 === -javac -d bin -cp "lib/*" src/com/example/App.java +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 === -javac -d bin-test -cp "bin;lib/*" test/com/example/AppTest.java +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 === @@ -23,34 +25,48 @@ 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) -java -jar "!JUNIT_JAR!" --classpath "bin;bin-test;lib/*" --scan-class-path +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 ❌ Tests failed! Aborting build package. + echo [X] Tests failed! Aborting build package. exit /b %errorlevel% ) -echo ✅ Tests passed successfully! +echo [OK] Tests passed successfully! echo === 5. Extracting Dependencies for Fat JAR === -cd fat-stage +cd stage for %%j in (..\lib\*.jar) do ( - echo %%j | findstr /i "junit-platform-console-standalone" >nul + echo %%~nxj | findstr /i "junit-platform-console-standalone" >nul if errorlevel 1 ( echo Extracting: %%~nxj jar -xf "%%j" ) ) -if exist META-INF rmdir /s /q META-INF + +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 === -xcopy /e /q /y bin\* fat-stage\ -jar cfm app.jar manifest.txt -C fat-stage/ . +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% -rmdir /s /q fat-stage +del sources.txt test-sources.txt >nul 2>&1 +rmdir /s /q stage echo ====================================== -echo 🎉 Fat JAR Created! Run it anywhere using: -echo java -jar app.jar +echo Fat JAR Created! Run it with: +echo java -jar dist/app.jar echo ====================================== pause - diff --git a/build.sh b/build.sh index ca768c4..6fdf7e1 100755 --- a/build.sh +++ b/build.sh @@ -3,23 +3,26 @@ set -e echo "=== 1. Cleaning Workspace ===" rm -rf bin stage -mkdir bin bin/classes bin/test-classes stage +mkdir -p bin/classes bin/test-classes stage dist # dist was never created echo "=== 2. Compiling Production Code ===" -javac -d bin/classes -cp "lib/*" src/main/java/com/example/App.java +javac -d bin/classes -cp "lib/*" @<(find src/main/java -name '*.java') echo "=== 3. Compiling Test Code ===" -javac -d bin/test-classes -cp "bin:lib/*" src/test/java/com/example/AppTest.java +# prod classes are in bin/classes, not bin +javac -d bin/test-classes -cp "bin/classes:lib/*" @<(find src/test/java -name '*.java') echo "=== 4. Running Automated Tests ===" -JUNIT_JAR=$(find lib/junit-platform-console-standalone-*.jar | head -n 1) +JUNIT_JAR=$(find lib -maxdepth 1 -name 'junit-platform-console-standalone-*.jar' | head -n 1) if [ -z "$JUNIT_JAR" ]; then echo "JUnit jar missing." exit 1 fi set +e -java -jar "$JUNIT_JAR" execute -cp "bin:lib/*" --scan-classpath +java -jar "$JUNIT_JAR" execute \ + -cp "bin/classes:bin/test-classes:lib/*" \ + --scan-classpath TEST_STATUS=$? set -e @@ -32,30 +35,27 @@ echo "✅ Tests passed successfully!" echo "=== 5. Extracting Dependencies for Fat JAR ===" cd stage for jarfile in ../lib/*.jar; do - echo "$jarfile" - # Skip the JUnit jar so we don't bloat production code - if [[ "$jarfile" == *"junit-platform-console-standalone"* ]]; then - continue - fi - if [ -f "$jarfile" ]; then - echo "Extracting: $(basename "$jarfile")" - jar -xf "$jarfile" - fi + [ -f "$jarfile" ] || continue + case "$(basename "$jarfile")" in + junit-platform-console-standalone-*) continue ;; + esac + echo "Extracting: $(basename "$jarfile")" + jar -xf "$jarfile" done -# Clean up extracted manifest directories to prevent conflicts -rm -rf META-INF + +# Strip signatures + manifests (they break repacking), but KEEP +# META-INF/services so ServiceLoader-based deps still work at runtime. +find META-INF -type f \( -name '*.SF' -o -name '*.RSA' -o -name '*.DSA' -o -name 'MANIFEST.MF' \) -delete 2>/dev/null || true cd .. echo "=== 6. Merging Assets into Fat JAR ===" -# Copy your own compiled production classes into the staging folder -cp -r bin/* stage/ - -# Package everything in the staging area into the final JAR -jar cfe dist/app.jar com.example.App -C stage/classes/ . +# Merge YOUR production classes on top of the extracted deps +cp -r bin/classes/. stage/ +# Package the whole tree (deps + your classes), not just stage/classes +jar cfe dist/app.jar com.example.App -C stage . -# Clean up staging area rm -rf stage echo "======================================" -echo "🎉 Fat JAR Created! Run it anywhere using:" -echo "java -jar app.jar" +echo "🎉 Fat JAR Created! Run it with:" +echo "java -jar dist/app.jar" echo "======================================" -- cgit v1.2.3