Maven Profile

Maven profiles are a powerful feature that allows you to tailor the build process for different environments or build scenarios. You can use profiles to define different configurations and activate them conditionally.

In this tutorial, we'll walk through the creation and activation of Maven profiles.

1. Define a profile

Profiles are defined within the <profiles> element in your POM file. You can create a profile by adding a <profile> element with an <id> and other optional configuration elements.

Here's an example of a profile definition:

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <env>development</env>
    </properties>
  </profile>
</profiles>

In this example, we created a profile with the ID dev that sets a property named env with the value "development."

2. Customize profile configurations

You can customize various aspects of your project configuration within a profile. For example, you can set different dependencies, plugins, or properties based on the active profile.

Here's an example of a profile with different dependencies:

<profiles>
  <profile>
    <id>dev</id>
    <dependencies>
      <dependency>
        <groupId>com.example</groupId>
        <artifactId>dev-dependency</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>
  </profile>
</profiles>

With this configuration, the dev-dependency will only be included in the project when the dev profile is active.

3. Activate profiles

Profiles can be activated in several ways:

  • Using the command line: To activate a profile from the command line, use the -P option followed by the profile ID:

    mvn clean install -Pdev
    

    In this example, the dev profile will be activated during the build.

  • Using an environment variable: To activate a profile based on an environment variable, use the <activation> element within the <profile>:

    <profiles>
      <profile>
        <id>dev</id>
        <activation>
          <property>
            <name>env</name>
            <value>development</value>
          </property>
        </activation>
        <!-- other configurations -->
      </profile>
    </profiles>
    

    In this example, the dev profile will be activated when the env environment variable is set to "development."

  • Using a file: To activate a profile based on the presence or absence of a file, use the <file> element within the <activation>:

    <profiles>
      <profile>
        <id>dev</id>
        <activation>
          <file>
            <exists>src/main/resources/dev.properties</exists>
          </file>
        </activation>
        <!-- other configurations -->
      </profile>
    </profiles>
    

    In this example, the dev profile will be activated when the src/main/resources/dev.properties file exists.

  • By default: To activate a profile by default, use the <activeByDefault> element within the <activation>:

    <profiles>
      <profile>
        <id>dev</id>
        <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
        <!-- other configurations -->
      </profile>
    </profiles>
    

    In this example, the dev profile will be active by default if no other profile is activated.

  1. How to use profiles in Maven projects:

    • Profiles allow you to customize build configurations for different scenarios.
    • Define profiles in the POM file using the <profiles> section.
    <!-- Example Maven profile definition -->
    <profiles>
        <profile>
            <id>development</id>
            <!-- Configuration for development profile -->
        </profile>
        <!-- Additional profiles... -->
    </profiles>
    
  2. Configuring environment-specific settings with Maven profiles:

    • Customize settings such as database URLs, logging levels, etc., based on the environment.
    <!-- Example environment-specific configuration in a profile -->
    <profiles>
        <profile>
            <id>development</id>
            <properties>
                <db.url>jdbc:dev-database-url</db.url>
                <!-- Additional properties... -->
            </properties>
        </profile>
    </profiles>
    
  3. Activating Maven profiles based on conditions:

    • Use activation conditions to automatically activate profiles based on certain criteria.
    <!-- Example profile activation based on OS -->
    <profiles>
        <profile>
            <id>windows</id>
            <activation>
                <os>
                    <family>windows</family>
                </os>
            </activation>
            <!-- Configuration for Windows... -->
        </profile>
    </profiles>
    
  4. Overriding properties and dependencies with Maven profiles:

    • Override properties or dependencies specific to a profile.
    <!-- Example property override in a profile -->
    <profiles>
        <profile>
            <id>production</id>
            <properties>
                <db.url>jdbc:prod-database-url</db.url>
                <!-- Additional properties... -->
            </properties>
        </profile>
    </profiles>
    
  5. Profiles for release and development builds in Maven:

    • Use profiles to differentiate between release and development builds.
    • Include configurations like code optimization and signing during release builds.
    <!-- Example release profile -->
    <profiles>
        <profile>
            <id>release</id>
            <!-- Release-specific configurations... -->
        </profile>
    </profiles>
    
  6. Integrating Maven profiles with CI/CD pipelines:

    • Define environment-specific variables in your CI/CD pipeline.
    • Pass these variables to Maven as system properties to activate the desired profiles.
    mvn clean install -Pproduction