Skip to main content
< All Topics
Print

Java Development

name: java-development

description: Java development best practices covering Maven/Gradle build tools, Spring Boot patterns, JVM tuning, testing, and Docker deployment. Use when building Java applications, configuring build pipelines, optimizing JVM performance, or containerizing Java services.

Java Development

Instructions

Follow these practices for production-grade Java development.

Build tools:

  • Maven: structure projects with pom.xml, manage dependencies via with BOMs, use lifecycle phases (validatecompiletestpackageverifyinstalldeploy)
  • Gradle: define builds in build.gradle(.kts), configure tasks with proper inputs/outputs for caching, use version catalogs (libs.versions.toml) for centralized dependency management
  • Pin all dependency versions — never use dynamic ranges in production

Spring Boot:

  • Use starters (spring-boot-starter-web, spring-boot-starter-data-jpa) to pull coherent dependency sets
  • Leverage auto-configuration; override only what you need via application.yml or @Configuration classes
  • Use profiles (spring.profiles.active) to separate dev/staging/production configuration
  • Enable Actuator (/health, /metrics, /info) for observability in deployed services

JVM tuning:

  • Set explicit heap bounds: -Xms (initial) and -Xmx (max) to the same value in containers to avoid resize pauses
  • Select GC based on workload: G1GC (default, balanced), ZGC (low-latency), Parallel GC (throughput-heavy batch)
  • For containers, use -XX:MaxRAMPercentage=75.0 instead of fixed heap sizes

Testing:

  • JUnit 5 with @Test, @ParameterizedTest, @Nested for structured test suites
  • Mockito for dependency isolation: @Mock, @InjectMocks, when().thenReturn()
  • Integration tests with @SpringBootTest and test slices (@WebMvcTest, @DataJpaTest)

Logging:

  • SLF4J facade with Logback implementation — never use System.out.println
  • Structured logging with MDC for request correlation in distributed systems
  • Log levels: ERROR (failures), WARN (degraded), INFO (business events), DEBUG (diagnostics)

Docker deployment:

  • Multi-stage builds: builder stage with full JDK for compilation, runtime stage with JRE-only base (eclipse-temurin:21-jre-alpine)
  • Copy only the built JAR to the runtime image to minimize attack surface and image size
  • Run as non-root user; set ENTRYPOINT ["java", "-jar", "app.jar"]
Table of Contents