advanced~1h

Packaging & Running Standalone

A short but necessary bridge module: before either service can be containerized (Module 19), it needs to run correctly outside the IDE.

Learning objectives

  • Beginner: Build an executable JAR for a Spring Boot Kafka service and run it outside the IDE.
  • Intermediate: Pass environment-specific configuration (broker address, credentials) to a packaged JAR without hardcoding it.
  • Advanced: Diagnose a "works in the IDE, fails standalone" bug caused by an environment/configuration mismatch.
./gradlew clean build -x test java -jar build/libs/library-events-producer-0.0.1-SNAPSHOT.jar

Spring Boot's Gradle plugin repackages the JAR into a self-contained "fat JAR" with an embedded servlet container (Tomcat by default) — there's no separate application server to install or configure. (If your own project uses Maven instead, the equivalent is ./mvnw clean package -DskipTests and java -jar target/<artifact>.jar — same idea, different build tool, different output directory.)

💻 Code example

./gradlew clean build -x test java -jar build/libs/library-events-producer-0.0.1-SNAPSHOT.jar

Spring Boot's relaxed binding maps environment variables to application.yml properties automatically — SPRING_KAFKA_PRODUCER_BOOTSTRAP_SERVERS overrides spring.kafka.producer.bootstrap-servers with no extra code. This is the exact mechanism Module 19's "same image, different environments" and Module 20's ConfigMaps both build on.

SPRING_KAFKA_PRODUCER_BOOTSTRAP_SERVERS=kafka-staging:9092 \ java -jar build/libs/library-events-producer-0.0.1-SNAPSHOT.jar

✓ Quick recap

How does an environment variable like SPRING_KAFKA_PRODUCER_BOOTSTRAP_SERVERS map to configuration? Spring Boot's relaxed binding maps it automatically to spring.kafka.producer.bootstrap-servers with no extra code.

💻 Code example

SPRING_KAFKA_PRODUCER_BOOTSTRAP_SERVERS=kafka-staging:9092 \ java -jar build/libs/library-events-producer-0.0.1-SNAPSHOT.jar

Want a visual for this concept?

Generate a diagram tailored to “Packaging & Running Standalone” — the AI picks whichever visual (flowchart, comparison, sequence, etc.) best fits.

Sign in to generate a visual →

Practice quiz

Next Step

Continue to Containerization with Docker← Back to all Kafka & Microservices chapters