Maven Tutorial
Maven in IDE
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.
How to use profiles in Maven projects:
<profiles>
section.<!-- Example Maven profile definition --> <profiles> <profile> <id>development</id> <!-- Configuration for development profile --> </profile> <!-- Additional profiles... --> </profiles>
Configuring environment-specific settings with Maven profiles:
<!-- 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>
Activating Maven profiles based on conditions:
<!-- Example profile activation based on OS --> <profiles> <profile> <id>windows</id> <activation> <os> <family>windows</family> </os> </activation> <!-- Configuration for Windows... --> </profile> </profiles>
Overriding properties and dependencies with Maven profiles:
<!-- 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>
Profiles for release and development builds in Maven:
<!-- Example release profile --> <profiles> <profile> <id>release</id> <!-- Release-specific configurations... --> </profile> </profiles>
Integrating Maven profiles with CI/CD pipelines:
mvn clean install -Pproduction