Chapter 150: Production-Level Java Project Structure in Java
Production structure is not only folders—it is how builds, config, secrets, observability, and operations line up with runtime code. Small services might stay one module; larger products split API, domain, and adapters so teams own boundaries.
1. Topic title
Production layout: code + build + deploy + observe + operate as one system
2. What it means
Repositories — src/main/java, src/test, Dockerfile, helm/ or k8s/, .github/workflows.
Configuration — 12-factor style env vars, application-prod.yaml, feature flags outside git for secrets.
Observability — metrics (Micrometer), traces (OpenTelemetry), structured logs (Chapter 142).
3. Why it is used
On-call engineers find runbooks and dashboards next to code reviews.
Compliance—audit trails for who shipped what artifact.
4. Mental sketch
Demo projects are backpacks—everything jumbled. Production repos are tool cabinets—drawers labeled “cutting”, “measuring”, “safety”.
5. Java code example
service/
build.gradle.kts
src/main/java/.../Application.java
src/main/resources/application.yaml
src/test/java/.../ApplicationTests.java
Dockerfile
README.md
docs/runbook.mddocs/runbook.md lists “what to check when p99 spikes”—living document.
6. Explanation of code
Single Application.java entry per deployable artifact—multiple main classes confuse packaging.
7. Common mistakes
Prod secrets in git—rotate and use secret manager.
Skipping README run instructions—first day pain tax.
8. Best practices
Versioned API changelog + semantic versioning for libraries.
SBOM and dependency scanning in CI—supply chain hygiene.
9. Small practice task
Add /actuator/health readiness check that includes database ping—wire Kubernetes readinessProbe path.
Beginner tip
If docker build works but Kubernetes crashes, check JAVA_TOOL_OPTIONS, memory limits, and file system read-only roots—runtime differs from laptop.