Conditional Dependency Management Using Maven Profiles
Last Updated :
06 Aug, 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:
- Profile Activation: Activations can be of several types depending on the option keys like command-line, Maven option settings, system environment variables, and system preferences.
- Dependency Management: It is possible to use profiles to specify which dependencies should be included and which ones should be excluded based on the currently active profile.
- Build Customization: With profiles, you can control how your build process is set up in aspects like plug-in settings, resources being filtered etc.
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:
mvn 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:
Now, let's take a look at the directory structure that Maven has created for us:
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.
XML
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>profile-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- Common dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>production</id>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
In this configuration:
- We have a common dependency (JUnit) that's always included.
- The development profile includes the
slf4j-simple
logger. - The production profile includes the
logback-classic
logger. - The development profile is set as the default profile.
Step 3: Create Java Classes
Let's create a simple Java class that uses the logger:
Java
package com.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App {
private static final Logger logger = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
logger.info("Hello, Maven Profiles!");
}
}
Using Maven Profiles
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:
mvn 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:
mvn clean package -Pproduction
This will include the logback-classic logger in the build instead.
Advanced Profile Usage
Profile Activation:
Profiles can be activated in various ways:
1. Command Line: As shown above, using -P<profile-id>.
For example, To activate the 'development' profile:
mvn clean package -Pdevelopment
2. Maven Settings: In the settings.xml file:
<activeProfiles>
<activeProfile>production</activeProfile>
</activeProfiles>
3. Environment Variable: In the pom.xml:
<activation>
<property>
<name>env</name>
<value>prod</value>
</property>
</activation>
Then activate using: mvn clean package -Denv=prod
4. OS Settings: Activate based on the operating system:
<activation>
<os>
<name>Windows 10</name>
<family>Windows</family>
<arch>amd64</arch>
<version>10.0</version>
</os>
</activation>
Resource Filtering:
Profiles can also be used for resource filtering. For example:
<profile>
<id>development</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application-dev.properties</include>
</includes>
</resource>
</resources>
</build>
</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:
- GeeksforGeeks: Apache Maven
- GeeksforGeeks: Maven Lifecycle and Basic Maven Commands
Similar Reads
Spring Boot - Dependency Management
Spring Boot framework is the most popular web development framework. No doubt, it provides an abundance of essential features and a convenient way to handle those features. At the heart of Spring Boot is the 'Dependency Management' feature. Importance of Dependency ManagementCentralized Dependency M
6 min read
Maven Dependency Management with Selenium
In software testing, Selenium is a popular tool for automating web applications. Managing Selenium dependencies efficiently is crucial for ensuring smooth and consistent builds. Maven is a powerful build automation tool that effectively simplifies this process by handling project dependencies. This
5 min read
Exclude a Dependency in a Maven Plugin
Excluding a dependency in a Maven plugin can be necessary to manage dependencies effectively and prevent conflicts or redundancies in the project. In this article, we will learn how to exclude a dependency in a Maven plugin using the <exclusions> tag within the <dependencies> section of
1 min read
Configuring Spring Boot Applications with Maven Profiles
In a software development environment, applications must often be configured differently for various environments such as development, testing, and production. Managing these configurations can be challenging, but Maven provides powerful features called profiles to handle this. Maven profiles allow
5 min read
Configure Active Profile in SpringBoot via Maven
In Spring Boot, Profiles allows us to define sets of configurations for different environments or use cases. Active Profile in Spring Boot dynamically changes the behavior of the application based on the active profile. Maven is a popular tool that can be used to build the automation tool for Java p
4 min read
Optional Dependency in Maven
Optional dependencies in Maven are used when it is difficult to break up a project into sub-modules (for whatever reason). The idea is that some dependencies are only required for specific aspects of the project and will be removed if such features are not used. Ideally, such a feature would be sepa
2 min read
How to Convert a Maven Project into Library to use it as Dependency?
Converting a Maven project into a library allows us to reuse its functionality across multiple projects by including it as a dependency. This process involves correctly setting up your project, packaging it, and installing or deploying it to a Maven repository. In this article, we will learn how to
2 min read
Spring Security Project Example using Java Configuration
Spring Security is a powerful framework for securing Java web applications. It provides authentication (verifying users) and authorization (controlling access) to protect our app from threats like CSRF attacks, session fixation, and brute-force attacks.With easy integration into Spring Boot and supp
6 min read
Maven Dependency Scopes
Maven Dependency scopes are used to specify the visibility and life cycle of a dependency in a Maven project. In Maven we have six different dependency scopes. Below we explain them by using a pom.xml file. Compile ScopeProvided ScopeRuntime ScopeTest ScopeSystem ScopeImport ScopeCompile Scope:This
3 min read
Creating Profiles in Spring Cloud Config Server
Spring Cloud Config provides both server-side and client-side support for external systems in a distributed system. Using the Config Server, we can manage the configuration for multiple applications from the central place. Profiles in the Spring Cloud Config can allow you to provide different config
4 min read