Skip to main content

Maven Project

Maven Profiles

Learn how to use Maven profiles for environment-specific builds. Activate profiles by command-line, property, or OS, and see a practical dev vs prod build example.

Every project has different environments: development, testing, staging, and production.
Each may require:

  • Different database configurations
  • Different logging levels
  • Different dependencies or plugins

Instead of maintaining multiple pom.xml files, Maven allows you to use profiles — a way to customize your build for different scenarios.

1. Why Maven Profiles Are Useful

Maven profiles help you:

  • Switch configurations easily without editing your pom.xml every time
  • Manage environment-specific properties (e.g., dev database vs prod database)
  • Enable or disable certain plugins or dependencies for specific builds
  • Improve CI/CD pipelines by automating builds for multiple environments

📌 Example Use Cases:

  • Use H2 Database in dev and MySQL/PostgreSQL in prod
  • Enable debug logging in dev and disable in prod
  • Include mock dependencies only for testing

2. How to Activate Profiles

Maven supports multiple ways to activate profiles:

a) By Command Line

You can activate a profile with the -P option:

mvn clean install -Pdev
mvn clean install -Pprod

b) By Property

Profiles can be activated when a system property is set:

<profile>
    <id>h2-profile</id>
    <activation>
        <property>
            <name>env</name>
            <value>h2</value>
        </property>
    </activation>
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.1.214</version>
        </dependency>
    </dependencies>
</profile>

Run with:

mvn clean install -Denv=h2

c) By Operating System

Profiles can be activated based on OS:

<profile>
    <id>windows-profile</id>
    <activation>
        <os>
            <family>Windows</family>
        </os>
    </activation>
</profile>

This activates automatically if you build on Windows.

3. Example: Dev vs Prod Build

Here’s a practical pom.xml snippet for dev and prod environments:

<profiles>
    <!-- Development Profile -->
    <profile>
        <id>dev</id>
        <properties>
            <db.url>jdbc:h2:mem:testdb</db.url>
            <logging.level>DEBUG</logging.level>
        </properties>
        <dependencies>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>2.1.214</version>
            </dependency>
        </dependencies>
    </profile>

    <!-- Production Profile -->
    <profile>
        <id>prod</id>
        <properties>
            <db.url>jdbc:mysql://prod-db:3306/myapp</db.url>
            <logging.level>INFO</logging.level>
        </properties>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.33</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>

Usage

  • For Development: mvn clean install -Pdev
  • For Production: mvn clean install -Pprod

✅ This approach keeps your builds clean and avoids maintaining multiple pom.xml files.

Best Practices for Maven Profiles

  • Keep profiles minimal — only override what differs across environments.
  • Use external property files (config.properties) and load them via profiles.
  • Avoid hardcoding sensitive information (use environment variables or CI/CD secrets).
  • Document profile usage in your project’s README.

Key Takeaways

  • Maven profiles let you customize builds for different environments without changing your pom.xml.
  • Profiles can be activated via command line, properties, or OS.
  • A common use case is having separate dev and prod builds with different databases and logging levels.