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 you to customize the build configurations for different environments by specifying different settings in the pom.xml file.
- This ensures that the application runs smoothly in each environment with the correct settings.
Maven Profiles
Maven profiles are sets of configurations that can be activated or deactivated based on certain conditions. They allow you to define different configurations for various environments such as development, testing, and production. Each profile can have its own dependencies, plugin properties, and build settings.
Working of Maven Profiles
Maven profiles can be defined in the pom.xml file. We can activate a profile using command line parameters, environment variables, or other activation mechanisms. When a profile is activated, Maven uses the settings defined in that profile to build the project.
- Maven: Maven is a build automation tool primarily used for Java projects. It provides a comprehensive project management framework that allows you to manage dependencies, build processes, and project structure efficiently.
- Profile: A profile in Maven is a set of configuration settings that allows you to customize the build for different environments. Profiles can be activated via the command line, through the settings.xml, or based on system properties.
- Dependency: A dependency in Maven is a Java library or project that the project relies on to build and run. Dependencies are declared in the pom.xml file and are automatically downloaded from Maven repositories.
- Plugin: A plugin in Maven is an extension to the build lifecycle, providing additional goals and functionality. The Spring Boot Maven Plugin, for example, simplifies the process of building and running Spring Boot applications.
- Dependency Management: It is the process of handling and maintaining all the dependencies required for the project. Maven automates this process by downloading the required libraries and managing versions.
Example: Below is an example of how a Maven profile is defined in the pom.xml file.
<profiles>
<profile>
<id>dev</id>
<properties>
<env>development</env>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources/dev</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>prod</id>
<properties>
<env>production</env>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources/prod</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
Implementation to Configure Spring Boot Application with Maven Profiles
Below are the implementation steps to configure a Spring Boot application with Maven Profiles.
Step 1: Create a Spring Project
Create a new Spring Boot project using IntelliJ IDEA. Refer to the screenshot for better understanding.
- Name: spring-maven-profiles
- Language: Java
- Type: Maven
- Packaging: jar
Click on the Next button.

Step 2: Add Dependencies
Add the following dependencies to the project:

Project Structure:
After the successful creation of the project, the project folder structure will look like this:

Step 3: Configure Properties
Create different profile properties for the application.
1. application.properties: Common properties
spring.application.name=spring-maven-profiles
message=Hello from general file!
2. application-dev.properties: Development environment properties
message=Hello from Development!
3. application-prod.properties: Production environment properties
message=Hello from Production!
Step 4: Create the Controller Class
Create the HelloController class to create a simple endpoint for testing the Spring application.
Go to src > java > com.gfg.springmavenprofiles > main > HelloController and put the below code.
package com.gfg.springmavenprofiles;
// Importing necessary classes and annotations from Spring Framework
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
// Defining a REST controller in Spring
@RestController
public class HelloController {
// Injecting the value of the "message" property from the application's configuration
@Value("${message}")
private String message;
// Mapping HTTP GET requests to the /hello endpoint to this method
@GetMapping("/hello")
public String hello() {
// Returning the injected message value
return message;
}
}
Step 5: Main Class
No changes are required in the main class of the project.
package com.gfg.springmavenprofiles;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringMavenProfilesApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMavenProfilesApplication.class, args);
}
}
pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.2</version>
<relativePath/>
</parent>
<groupId>com.gfg</groupId>
<artifactId>spring-maven-profiles</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-maven-profiles</name>
<description>spring-maven-profiles</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
</profiles>
</project>
Step 6: Run the Application
Now, run the application, and it will start at port 8080.

Test the Endpoint
GET http://localhost:8080/hello
Output:

Using the Maven Profiles
Activating a Maven Profile
We will now activate a Maven profile using the command line by passing the -P option followed by the profile ID.
1. Install the Dev Profile:
mvn clean install -Pdev
Output:

2. Activate the Dev Profile:
Activate the dev profile and run the application, using the below command.
mvn spring-boot: run -Pdev
Output:

3. Test the Dev Profile:
We can test the endpoint using postman tool.
GET http://localhost:8080/hello
Output:

Install the prod Profile
Build the project using the prod profile, using the below command.
mvn clean install -Pprod
Output:

Activate the prod Profile
Activate the prod profile and run the application, using the following command.
mvn spring-boot:run -Pprod
Output:

Test the prod profile
Now, test the endpoint again using the postman tool.
GET http://localhost:8080/hello
Output:

In this example, we have defined two profiles that are dev and prod. Each profile sets a different Spring profile and resource directory for the Spring Boot application.