Deployment
Spring Boot Fat JAR
Spring Boot produces a fat JAR that contains all dependencies. No need to install the framework separately.
Build the fat JAR:
# Maven
mvn clean package
# or skip tests
mvn clean package -DskipTests
# Gradle
./gradlew build
The JAR file is located in target/ (Maven) or build/libs/ (Gradle):
target/
├── myapp-1.0.0.jar ← Fat JAR (executable)
├── myapp-1.0.0.jar.original ← Original JAR (without dependencies)
└── myapp-1.0.0.jar.sha1
Running the Fat JAR
java -jar target/myapp-1.0.0.jar
# With a profile
java -jar target/myapp-1.0.0.jar --spring.profiles.active=prod
# With a custom port
java -jar target/myapp-1.0.0.jar --server.port=9090
# With environment variables
java -jar target/myapp-1.0.0.jar \
--spring.datasource.url=jdbc:postgresql://db:5432/myapp \
--spring.datasource.username=postgres \
--spring.datasource.password=secret
Docker
Dockerfile
# Multi-stage build
FROM eclipse-temurin:21-jdk-jammy AS build
WORKDIR /app
COPY . .
RUN mvn clean package -DskipTests
FROM eclipse-temurin:21-jre-jammy
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
Build and run:
# Build the image
docker build -t myapp:1.0.0 .
# Run the container
docker run -d -p 8080:8080 --name myapp myapp:1.0.0
# Check the logs
docker logs -f myapp
# Stop the container
docker stop myapp
Docker Compose
# docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=docker
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/myapp
- SPRING_DATASOURCE_USERNAME=postgres
- SPRING_DATASOURCE_PASSWORD=secret
depends_on:
db:
condition: service_healthy
restart: unless-stopped
db:
image: postgres:16-alpine
environment:
- POSTGRES_DB=myapp
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=secret
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
volumes:
pgdata:
# Build and run all services
docker-compose up -d
# Rebuild
docker-compose up -d --build
# Stop everything
docker-compose down
# Stop and remove volumes
docker-compose down -v
Deploying to the Cloud
Railway
# Install the Railway CLI
npm install -g @railway/cli
# Log in
railway login
# Initialize the project
railway init
# Deploy
railway up
Or connect a GitHub repository directly from the Railway dashboard.
Render
- Create an account at render.com
- Connect a GitHub repository
- Choose Web Service
- Configure:
- Build Command:
mvn clean package -DskipTests - Start Command:
java -jar target/*.jar - Port:
8080 - Deploy
Fly.io
# Install flyctl
curl -L https://fly.io/install.sh | sh
# Log in
fly auth login
# Initialize
fly launch
# Deploy
fly deploy
Environment Variables
Always use environment variables for secrets in production:
export SPRING_PROFILES_ACTIVE=prod
export SPRING_DATASOURCE_URL=jdbc:postgresql://db-host:5432/myapp
export SPRING_DATASOURCE_USERNAME=postgres
export SPRING_DATASOURCE_PASSWORD=mysecretpassword
export JWT_SECRET=myjwtsecretkey123
# application.properties still needs placeholders
spring.datasource.url=${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
Health Checks for Production
# Enable the health endpoint for the load balancer
management.endpoints.web.exposure.include=health,info
management.endpoint.health.show-details=when-authorized
# Health check
curl http://localhost:8080/actuator/health
Deployment Checklist
| Item | Status |
|---|---|
| Fat JAR built successfully | |
| Docker image built successfully | |
| Database migration runs | |
| Environment variables configured | |
| Health check endpoint active | |
| Actuator exposure limited | |
| Logging configured | |
| Security active |
Post-Deployment Monitoring
# Check that the application is running
curl http://your-app.onrender.com/actuator/health
# Check metrics
curl http://your-app.onrender.com/actuator/metrics
# Check logs
docker logs -f myapp
With proper deployment, your application is ready for users. In the final chapter, we'll look at the Spring Boot Cheatsheet as a quick reference.