Maven Profiles for Mule Developers

Maven 
profiles are a way to customize the build process of our Mule apps for different environments or conditions by allowing us to specify different configurations, dependencies, or plugins.

They are particularly useful when we need to build our Mule project differently based on the target environment (e.g., development, testing, production), operating system, or other specific criteria.

Using Maven profiles in MuleSoft projects allows you to:
  • Load different properties files or configurations for different environments.
  • Manage external resources such as databases, APIs, or CloudHub settings based on the active profile.
  • Fine-tune build and deployment pipelines for each stage of the software lifecycle.


How to Define Maven Profiles

We can define Profiles by adding the <profiles> element to the pom.xml file of our mule project and a <profile> element within <profiles> for each profile we want to define. Inside each <profile> element, we need to provide
  • An <id> element (which needs to be unique), which will identify our profile.
  • All the elements that define the properties and configurations of your profile
  • The <activation> element, which specifies whether or not the profile is active by default

Example:

<profiles>
<profile>
<id>dev</id>
<properties>
<env>development</env>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>

<profile>
<id>prod</id>
<properties>
<env>production</env>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>

NOTE: Instead of the project’s POM file we can also use a parent POM file or the settings.xml file. When to use which file depends on the scope of the elements you want to include in your profile


How to Use Maven Profiles

Once we’ve got our Profiles defined, we need to tell Maven which ones to use. That’s what in Maven is called Activating profiles. We can activate multiple profiles and profiles can be activated manually or automatically

Manual Activation

You can activate a profile manually from the command line using the -P option:

mvn clean install -Pdev


This command activates the dev profile during the build. If no profile is specified, Maven will use the default settings in the pom.xml (without profiles).


Automatic Activation

You can configure profiles to be activated automatically based on certain conditions:
  • Based on Properties: A profile can be activated if a specific property is set in the environment.
<profile>
<id>dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<db.url>jdbc:mysql://localhost:3306/dev_db</db.url>
</properties>
</profile>

You can then activate this profile by passing the property:
mvn clean install -Denv=dev

  • Based on JDK Version: A profile can be activated if a specific version of the JDK is used.
<profiles>
<profile>
<id>java8</id>
<activation>
<jdk>1.8</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

  • Based on Operating System: You can activate a profile based on the operating system.
<profile>
<id>windows-profile</id>
<activation>
<os>
<name>Windows</name>
</os>
</activation>
<properties>
<win.property>specific value</win.property>
</properties>
</profile>

  • Based on Environment Variables: A profile can be activated if a specific environment variable is set.
<profile>
<id>envProfile</id>
<activation>
<property>
<name>env.MY_ENV_VAR</name>
<value>value</value>
</property>
</activation>
<properties>
<custom.setting>someValue</custom.setting>
</properties>
</profile>


How to use Multiple Profiles

We can use multiple Maven profiles at the same time by specifying them on the command line or activating them automatically through conditions. Maven allows us to combine the settings from multiple profiles into a single effective build configuration. 

However, when using multiple profiles, we need to be aware of how Maven handles conflicts between them. We can activate multiple profiles in Maven using the -P flag and a comma-separated list of profiles:

mvn clean install -Pdev,staging

In this case, both the dev and staging profiles will be activated, and Maven will attempt to merge their configurations.

When multiple profiles define the same elements (like properties, dependencies, or plugins), Maven needs to handle these conflicts by merging or overriding elements as necessary. These are the rules to handle conflicts:
  • Properties, dependencies, plugins, repositories, and other build settings are handled differently, with properties and plugins typically getting overridden by the last active profile.
  • Repositories, dependencies, and resources are generally merged across active profiles.
  • Order matters when resolving conflicts, so the last activated profile takes precedence for conflicting elements.
Example - Properties conflicts

<profiles>
<profile>
<id>dev</id>
<properties>
<db.url>jdbc:mysql://localhost:3306/dev_db</db.url>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<db.url>jdbc:mysql://prod-db.example.com:3306/prod_db</db.url>
</properties>
</profile>
</profiles>

If both the 
dev and prod profiles are activated:
  • If there are different properties, Maven will merge properties from both profiles
  • When both profiles define the same property, the last activated profile (in this case, prod) will set the db.urlproperty, overriding the dev value.
Previous Post Next Post