> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Maven Profiles
- URL: https://www.javahandbook.com/maven-course/maven-profiles/
- Published: 2025-08-17T07:01:40.000Z
- Updated: 2025-08-17T07:03:05.000Z
- Description: 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.
- Author: Gopi Gorantala
- Tags: Maven Project, #docs, #maven-course

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:

```bash
mvn clean install -Pdev
mvn clean install -Pprod

```

### **b) By Property**

Profiles can be activated when a system property is set:

```xml
<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:

```bash
mvn clean install -Denv=h2

```

### **c) By Operating System**

Profiles can be activated based on OS:

```xml
<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:

```xml
<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.