--- description: This guide provides opinionated, actionable best practices for Maven projects, focusing on reproducible builds, efficient dependency management, and high code quality. globs: **/*.java --- # Maven Best Practices Maven is the definitive build tool for modern Java. Adhere to these rules for consistent, maintainable, and high-quality projects. ## Code Organization and Structure ### Centralize Configuration with a Parent POM/BOM Always use a parent POM (or Bill of Materials - BOM) to define shared properties, plugin versions, and a `` section. This guarantees consistent library versions and build behavior across all modules. ❌ BAD: Duplicating dependency versions and plugin configurations in every child `pom.xml`. ```xml org.springframework.boot spring-boot-starter-web 3.2.0 org.apache.maven.plugins maven-compiler-plugin 3.11.0 17 ``` ✅ GOOD: Define a parent POM with `` and ``. Child modules inherit these. ```xml 4.0.0 com.mycompany my-parent 1.0.0-SNAPSHOT pom 17 3.2.0 3.11.0 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin ${maven.compiler.plugin.version} ${java.version} com.mycompany my-parent 1.0.0-SNAPSHOT 4.0.0 my-child-module org.springframework.boot spring-boot-starter-web ``` ## Common Patterns and Anti-patterns ### Declare the Narrowest Possible Dependency Scope Limit dependency transitivity and classpath bloat by using the most restrictive scope (`compile`, `provided`, `runtime`, `test`). ❌ BAD: Using `compile` scope for everything, including test-only dependencies. ```xml org.junit.jupiter junit-jupiter-api 5.11.0 ``` ✅ GOOD: Explicitly define scopes. * `compile` (default): Available in all classpaths, propagated to dependents. For core libraries. * `provided`: Expected to be provided by JDK or container at runtime. For Servlet API, Java EE APIs. * `runtime`: Not needed for compilation, but for execution. For JDBC drivers. * `test`: Only for test compilation and execution. For JUnit, Mockito. ```xml org.junit.jupiter junit-jupiter-api test jakarta.servlet jakarta.servlet-api 6.0.0 provided ``` ### Manage Transitive Dependencies with Exclusions Prevent unwanted or conflicting transitive dependencies from polluting your classpath. ❌ BAD: Relying on Maven's "nearest definition" for conflicting transitive dependencies, leading to unpredictable builds or runtime issues. ```xml com.example library-b 1.0 com.example library-c 1.0 ``` ✅ GOOD: Explicitly exclude problematic transitive dependencies and declare the desired version directly. ```xml com.example library-b 1.0 com.example dependency-d com.example library-c 1.0 com.example dependency-d 2.0 ``` ## Performance Considerations ### Use Maven Wrapper for Consistent Builds Ensure all developers and CI/CD pipelines use the exact same Maven version, preventing "works on my machine" issues related to Maven itself. ✅ GOOD: Include Maven Wrapper in your project. ```bash mvn wrapper:wrapper ``` Then, use `./mvnw` (or `mvnw.cmd` on Windows) instead of `mvn`. ```bash ./mvnw clean install ``` ## Common Pitfalls and Gotchas ### Avoid Over-reliance on Maven Profiles for Environment Configuration Modern applications should be environment-agnostic (12-factor app principles). Use external configuration (environment variables, configuration servers) for environment-specific settings, not Maven profiles. ❌ BAD: Using profiles to inject environment-specific database URLs or API keys. ```xml dev jdbc:h2:mem:devdb prod jdbc:postgresql://prod-db:5432/appdb ``` ✅ GOOD: Keep application configuration external to the build. Maven profiles are acceptable for *build process* variations (e.g., different test suites, packaging types), but not runtime environment configuration. ## Testing Approaches ### Integrate Static Analysis Plugins Enforce coding standards and identify potential issues early by integrating static analysis tools like Checkstyle, SpotBugs, or PMD into the Maven build lifecycle (e.g., `verify` phase). ✅ GOOD: Configure static analysis plugins in your parent POM's `` section. ```xml org.apache.maven.plugins maven-checkstyle-plugin 3.3.1 validate validate check checkstyle.xml true com.github.spotbugs spotbugs-maven-plugin 4.8.3.0 analyze verify check ```