All 15 Build Tools: Maven & Gradle questions
More in Build Tools: Maven & Gradle
Advertisement
Java › Build Tools: Maven & Gradle

How do you manage different configurations for different environments (development, staging, production) using Maven profiles or Gradle's equivalent mechanisms?

Intermediate

Answer

Maven profiles (<profiles> in pom.xml, activated via -P flag, an environment variable, or automatic activation conditions) let you conditionally include different dependencies, properties, or plugin configurations depending on which profile is active -- Gradle typically achieves the same outcome more flexibly through plain conditional logic in the build script itself (checking a project property or environment variable), or through separate source sets/build variants, since Gradle's scripting nature doesn't require a dedicated "profile" abstraction the way Maven's declarative model does.
<!-- Maven profile for production-specific configuration -->
<profiles>
    <profile>
        <id>production</id>
        <properties>
            <log.level>WARN</log.level>
        </properties>
    </profile>
</profiles>

# Activate explicitly
mvn package -Pproduction
Real-world example A CI/CD pipeline activates a different Maven profile depending on the deployment target (dev, staging, production), each profile substituting the appropriate database connection properties and logging configuration into the packaged artifact without maintaining entirely separate pom.xml files per environment.

Common follow-ups: What are the risks of baking environment-specific configuration directly into the build artifact versus externalizing it at deploy/runtime instead?;How does Gradle's build variant / source set approach compare structurally to Maven profiles?