From 325d40e9bcc54d3389450fb951c873ffb466bb64 Mon Sep 17 00:00:00 2001 From: Kostya Date: Sat, 25 Jul 2026 14:59:02 +0000 Subject: fat jar build --- .gitignore | 9 ++-- build.bat | 56 ++++++++++++++++++++++ build.sh | 61 ++++++++++++++++++++++++ lib/junit-platform-console-standalone-6.1.1.jar | Bin 0 -> 2996922 bytes run.bat | 4 +- run.sh | 4 +- 6 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 build.bat create mode 100755 build.sh create mode 100644 lib/junit-platform-console-standalone-6.1.1.jar diff --git a/.gitignore b/.gitignore index b63bf17..774d6b1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,12 @@ # Ignore compiled Java bytecode bin/ +stage/ +dist/ *.class -# Ignore packaged binaries -*.jar -*.war - # Ignore OS-specific files .DS_Store Thumbs.db -# Optional: Ignore environment properties if they hold local secrets -# config.properties +config.properties diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..e1f3d72 --- /dev/null +++ b/build.bat @@ -0,0 +1,56 @@ +@echo off +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 + +echo === 2. Compiling Production Code === +javac -d bin -cp "lib/*" src/com/example/App.java +if %errorlevel% neq 0 exit /b %errorlevel% + +echo === 3. Compiling Test Code === +javac -d bin-test -cp "bin;lib/*" test/com/example/AppTest.java +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) + +java -jar "!JUNIT_JAR!" --classpath "bin;bin-test;lib/*" --scan-class-path +if %errorlevel% neq 0 ( + echo ❌ Tests failed! Aborting build package. + exit /b %errorlevel% +) +echo ✅ Tests passed successfully! + +echo === 5. Extracting Dependencies for Fat JAR === +cd fat-stage +for %%j in (..\lib\*.jar) do ( + echo %%j | 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 +cd .. + +echo === 6. Merging Assets into Fat JAR === +xcopy /e /q /y bin\* fat-stage\ +jar cfm app.jar manifest.txt -C fat-stage/ . +if %errorlevel% neq 0 exit /b %errorlevel% + +rmdir /s /q fat-stage +echo ====================================== +echo 🎉 Fat JAR Created! Run it anywhere using: +echo java -jar app.jar +echo ====================================== +pause + diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..ca768c4 --- /dev/null +++ b/build.sh @@ -0,0 +1,61 @@ +#!/bin/bash +set -e + +echo "=== 1. Cleaning Workspace ===" +rm -rf bin stage +mkdir bin bin/classes bin/test-classes stage + +echo "=== 2. Compiling Production Code ===" +javac -d bin/classes -cp "lib/*" src/main/java/com/example/App.java + +echo "=== 3. Compiling Test Code ===" +javac -d bin/test-classes -cp "bin:lib/*" src/test/java/com/example/AppTest.java + +echo "=== 4. Running Automated Tests ===" +JUNIT_JAR=$(find lib/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 +TEST_STATUS=$? +set -e + +if [ $TEST_STATUS -ne 0 ]; then + echo "❌ Tests failed! Aborting build package." + exit 1 +fi +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 +done +# Clean up extracted manifest directories to prevent conflicts +rm -rf META-INF +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/ . + +# Clean up staging area +rm -rf stage +echo "======================================" +echo "🎉 Fat JAR Created! Run it anywhere using:" +echo "java -jar app.jar" +echo "======================================" diff --git a/lib/junit-platform-console-standalone-6.1.1.jar b/lib/junit-platform-console-standalone-6.1.1.jar new file mode 100644 index 0000000..ce038a5 Binary files /dev/null and b/lib/junit-platform-console-standalone-6.1.1.jar differ diff --git a/run.bat b/run.bat index 6d3f6e6..8480652 100644 --- a/run.bat +++ b/run.bat @@ -4,12 +4,12 @@ rmdir /s /q bin mkdir bin :: compile source files and include libraries -javac -d bin -cp "lib/*" src/main/java/com/example/App.java +javac -d "bin/classes" -cp "lib/*" src/main/java/com/example/App.java :: run the application if compilation succeeded if %errorlevel% equ 0 ( echo --- Running Application --- - java -cp "bin;lib/*" com.example.App + java -cp "bin/classes;lib/*" com.example.App ) else ( echo Compilation failed. ) diff --git a/run.sh b/run.sh index d50ae4d..55f1094 100644 --- a/run.sh +++ b/run.sh @@ -4,9 +4,9 @@ rm -rf bin && mkdir bin # run the app if compilation succeeded -if javac -d bin -cp "lib/*" src/main/java/com/example/App.java; then +if javac -d "bin/classes" -cp "lib/*" src/main/java/com/example/App.java; then echo "--- Running Application ---" - java -cp "bin:lib/*" com.example.App + java -cp "bin/classes:lib/*" com.example.App else echo "Compilation failed." fi -- cgit v1.2.3