Chirag's Blog

Conditional Dependency Management Using Maven Profiles

August 6, 2024

Maven is an efficient build tool and project management tool mainly known for effectively building projects in Java. It is one of the most useful features in Maven, to effectively manage the consumers and producers of dependencies in a project.

In complex projects, you might need different dependencies or configurations based on various conditions such as the development environment, target platform, or specific build requirements. This is where Maven Profiles come into play. Maven Profiles

Maven Profiles is a set of parameters that can be adapted to set or alter the default parameters of Maven build. They enable you to define the application construction process according to specific contexts like development, testing, or production.

Key Concepts:

Setting Up a Maven Project with Profiles

Let's walk through the process of setting up a Maven project that uses profiles for conditional dependency management.

Step 1: Create a New Maven Project

First, let's create a new Maven project using the command line:

1mvn archetype:generate -DgroupId=com.example -DartifactId=profile-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Output:

This command will create a new Maven project with a basic structure. Here is what the output should look like:

Output of the create new Maven project command

Now, let's take a look at the directory structure that Maven has created for us:

Directory Structure

Step 2: Configure pom.xml

Now, let's modify the pom.xml file to include profiles. We'll create two profiles: development and production, each with its own set of dependencies.

1<project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5
6 <groupId>com.example</groupId>
7 <artifactId>profile-demo</artifactId>
8 <version>1.0-SNAPSHOT</version>
9
10 <properties>
11 <maven.compiler.source>1.8</maven.compiler.source>
12 <maven.compiler.target>1.8</maven.compiler.target>
13 </properties>
14
15 <dependencies>
16 <!-- Common dependencies -->
17 <dependency>
18 <groupId>junit</groupId>
19 <artifactId>junit</artifactId>
20 <version>4.13.2</version>
21 <scope>test</scope>
22 </dependency>
23 </dependencies>
24
25 <profiles>
26 <profile>
27 <id>development</id>
28 <activation>
29 <activeByDefault>true</activeByDefault>
30 </activation>
31 <dependencies>
32 <dependency>
33 <groupId>org.slf4j</groupId>
34 <artifactId>slf4j-simple</artifactId>
35 <version>1.7.32</version>
36 </dependency>
37 </dependencies>
38 </profile>
39 <profile>
40 <id>production</id>
41 <dependencies>
42 <dependency>
43 <groupId>ch.qos.logback</groupId>
44 <artifactId>logback-classic</artifactId>
45 <version>1.2.6</version>
46 </dependency>
47 </dependencies>
48 </profile>
49 </profiles>
50</project>

In this configuration:

Step 3: Create Java Classes

Let's create a simple Java class that uses the logger:

1package com.example;
2
3import org.slf4j.Logger;
4import org.slf4j.LoggerFactory;
5
6public class App {
7 private static final Logger logger = LoggerFactory.getLogger(App.class);
8
9 public static void main(String[] args) {
10 logger.info("Hello, Maven Profiles!");
11 }
12}

Step 4: Build the Project

Now that we have set up our project with profiles, let's see how to use them.

Building with the Default Profile

To build the project with the default profile (development in our case), simply run:

1mvn clean package

This will include the slf4j-simple logger in the build.

Building with a Specific Profile

To build the project with the production profile, use the -P flag:

1mvn clean package -Pproduction

This will include the logback-classic logger in the build instead.

Advanced Profile Usage

Profiles can be activated in various ways:

  1. Command Line: As shown above, using -P<profile-id>.

For example, To activate the 'development' profile:

1mvn clean package -Pdevelopment
  1. Maven Settings: In the settings.xml file:
1<activeProfiles>
2 <activeProfile>production</activeProfile>
3</activeProfiles>
  1. Environment Variable: In the pom.xml:
1<activation>
2 <property>
3 <name>env</name>
4 <value>prod</value>
5 </property>
6</activation>

Then activate using: mvn clean package -Denv=prod

  1. OS Settings: Activate based on the operating system:
1<activation>
2 <os>
3 <name>Windows 10</name>
4 <family>Windows</family>
5 <arch>amd64</arch>
6 <version>10.0</version>
7 </os>
8</activation>

Resource Filtering:

Profiles can also be used for resource filtering. For example:

1<profile>
2 <id>development</id>
3 <build>
4 <resources>
5 <resource>
6 <directory>src/main/resources</directory>
7 <filtering>true</filtering>
8 <includes>
9 <include>application-dev.properties</include>
10 </includes>
11 </resource>
12 </resources>
13 </build>
14</profile>

This will only include the application-dev.properties file when the development profile is active.

Conclusion

Maven Profiles provide a powerful way to manage conditional dependencies and configurations in your Java projects. By using profiles, you can easily switch between different build configurations for various environments or conditions. This flexibility allows for more maintainable and adaptable projects, especially when dealing with complex build requirements or multiple deployment scenarios.

References: