Lesson 40 • Advanced

    Deployment & Packaging

    Your code is only useful when it's running in production. This lesson covers the entire path from source code to live application — creating executable JARs, containerizing with Docker, deploying to cloud platforms, and setting up CI/CD pipelines that ship code automatically.

    Before You Start

    You should know Maven & Gradle (build tools create your JARs) and Modular Java (jlink for slim runtimes). Basic command-line familiarity is assumed.

    What You'll Learn

    • ✅ Fat JARs and executable JARs with Maven/Gradle
    • ✅ Docker containerization for Java apps
    • ✅ Multi-stage Docker builds for smaller images
    • ✅ Cloud deployment: AWS, GCP, Azure
    • ✅ CI/CD pipelines with GitHub Actions
    • ✅ Health checks and monitoring in production

    1️⃣ Deployment Options

    Analogy: Deploying is like shipping a package. A JAR is the product. Docker puts it in a standardized container. Kubernetes is the fleet of trucks managing delivery. CI/CD is the automated assembly line.

    MethodProsBest For
    JAR on VMSimple, directSmall apps
    DockerConsistent, portableMost production apps
    KubernetesAuto-scaling, self-healingMicroservices at scale
    GraalVM Native10ms startup, low memoryServerless, CLIs

    Try It: Docker Multi-Stage Build

    Try it Yourself »
    JavaScript
    // 💡 Try modifying this code and see what happens!
    // Docker multi-stage build simulation
    
    console.log("=== Docker Multi-Stage Build ===\n");
    
    // 1. The problem with single-stage
    console.log("1. THE PROBLEM — Single Stage:");
    let singleStage = { jdk: 400, maven: 200, source: 5, deps: 150, total: 755 };
    console.log("  JDK:       " + singleStage.jdk + "MB");
    console.log("  Maven:     " + singleStage.maven + "MB");
    console.log("  Source:    " + singleStage.source + "MB");
    console.log("  Deps:     
    ...

    2️⃣ CI/CD Pipelines

    Analogy: CI/CD is like a factory assembly line. Every time a developer pushes code, it automatically gets compiled, tested, packaged, and deployed. No manual steps, no "works on my machine" — if the pipeline passes, the code is production-ready.

    CI (Continuous Integration): Compile → Test → Code quality check (every push)

    CD (Continuous Delivery): Build artifact → Deploy to staging → Manual approval

    CD (Continuous Deployment): Same, but deploys to production automatically

    Try It: CI/CD Pipeline Simulation

    Try it Yourself »
    JavaScript
    // 💡 Try modifying this code and see what happens!
    // CI/CD Pipeline simulation
    
    console.log("=== CI/CD Pipeline ===\n");
    
    // Simulate pipeline stages
    function runStage(name, durationMs, successRate) {
      let success = Math.random() < successRate;
      return { name, duration: durationMs, success, 
        message: success ? "✅ passed" : "❌ failed" };
    }
    
    // Pipeline definition
    console.log("1. GITHUB ACTIONS PIPELINE:");
    console.log(`  name: Java CI/CD
      on: [push, pull_request]
      jobs:
        build:
         
    ...

    Common Mistakes

    • ⚠️ Not using multi-stage Docker builds: Build image is 800MB, runtime should be 80MB
    • ⚠️ Secrets in Dockerfiles: Use environment variables, Docker secrets, or a vault
    • ⚠️ No health checks: Without /actuator/health, orchestrators can't restart crashed services
    • ⚠️ No graceful shutdown: Configure server.shutdown=graceful for in-flight requests

    Production Checklist

    • 💡 ✅ Health endpoint configured (/actuator/health)
    • 💡 ✅ Environment-specific configs via Spring Profiles
    • 💡 ✅ Structured JSON logging for log aggregation
    • 💡 ✅ Graceful shutdown enabled
    • 💡 ✅ Resource limits set in Docker/K8s
    • 💡 ✅ Secrets in environment variables, never hardcoded

    Try It: Production Health & Monitoring

    Try it Yourself »
    JavaScript
    // 💡 Try modifying this code and see what happens!
    // Production health checks and monitoring
    
    console.log("=== Production Health & Monitoring ===\n");
    
    // 1. Health check endpoint
    console.log("1. SPRING BOOT ACTUATOR HEALTH:");
    let health = {
      status: "UP",
      components: {
        db: { status: "UP", details: { database: "PostgreSQL", validationQuery: "isValid()" } },
        diskSpace: { status: "UP", details: { total: "500GB", free: "320GB", threshold: "10MB" } },
        redis: { status: "UP", details
    ...

    📋 Quick Reference

    TaskCommandNotes
    Packagemvn clean packageCreates JAR in target/
    Run JARjava -jar app.jarExecutable fat JAR
    Docker builddocker build -t app .Multi-stage builds
    GraalVMnative-image -jar app.jar~10ms startup
    jlinkjlink --add-modulesCustom slim JRE

    🎉 Lesson Complete!

    You can now deploy Java apps from local development to production cloud environments! Next: Logging — professional logging with SLF4J and Logback.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service