Configuring Spring Boot Applications with Maven Profiles
Last Updated :
02 Aug, 2024
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.
In this article, we will guide you on configuring Spring Boot applications using Maven profiles, providing examples and step-by-step instructions for setting up the Maven project, adding dependencies, configuring plugins, and defining the pom.xml
file.
Maven Profiles
What are 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.
How do Maven Profiles Work?
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.
Key Terminologies
- 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 Maven Profile Definition
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: Mave
- Packaging: jar
Click on the Next button.
Step 2: Add Dependencies
Add the following dependencies to the project:
After the successful creation of the project, the project folder structure will be like below:
Step 3: Configure Properties
Create different profile properties for the application.
application.properties:
spring.application.name=spring-maven-profiles
message=Hello from general file!
application-dev.properties:
message=Hello from Development!
application-prod.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.
Java
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.
Java
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
<?xml version="1.0" encoding="UTF-8"?>
<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 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.
Install the Dev Profile
mvn clean install -Pdev
Output:
Activate the Dev Profile
Activate the dev profile and run the application, using the below command.
mvn spring-boot: run -Pdev
Output:
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: dev
and prod
. Each profile sets a different Spring profile and resource directory for the Spring Boot application.
Similar Reads
Configuring Store Procedure in Spring Boot Application
The Store Procedure is a prepared SQL statement that is used over and over again (Reusable code). So, if we have an SQL query that we need to write over and over again, we can save it as a Stored Procedure, and then just call it to execute it. We can also pass parameters to a Stored Procedure so tha
5 min read
Spring Boot â Managing Application Properties with Profiles
In Spring Boot, managing application properties is crucial for configuring the application based on different environments (e.g., development, testing, production). Spring Boot provides a feature called Profiles to help with this. Profiles allow you to define different sets of properties for various
6 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
Configuring Multiple Spring Batch Jobs in a Spring Boot Application
Spring Batch serves as a robust framework within the Spring ecosystem, specifically tailored for managing batch processing tasks efficiently. It's designed to tackle big data jobs efficiently and comes with handy tools to make batch application development a breeze. In the context of a Spring Boot a
10 min read
Configure Multiple Datasource in Spring Boot Application
Sometimes we build some applications that need multiple databases. Our data is arranged to store multiple databases, like one database designed for another need or another database for another need. So, in this article, we will see how to configure multiple in the Spring Boot application. Note: We a
6 min read
Display Auto-Configuration Report in Spring Boot Application
Spring Boot provides a lot of features to solve real-time problems. In these features, Auto-Configuration is one of the power features in the Spring Boot framework. The Spring Auto configuration configures beans based on the dependencies and configuration found in our project classpath. We need to f
3 min read
How to Create a Simple Spring Boot Application?
Spring Boot is one of the most popular frameworks for building Java-based web applications. It is used because it simplifies the development process by providing default configurations and also reduces boilerplate code. In this article, we will cover the steps to create a simple Spring Boot applicat
2 min read
Configuring a Hikari Connection Pool with Spring Boot
Hikari Connection Pool with Spring Boot mainly involves setting up a high-performance database connection pool for efficient management of the database connections within the application. When developing Spring Boot applications, the application will interact with databases, managing database connec
2 min read
Spring @Configuration Annotation with Example
The @Configuration annotation in Spring is one of the most important annotations. It indicates that a class contains @Bean definition methods, which the Spring container can process to generate Spring Beans for use in the application. This annotation is part of the Spring Core framework. Let's under
4 min read
Deploy a Spring Boot Application with AWS
A spring boot application is a Java-based web application that is built using the Spring Boot framework. Spring Boot is a project designed to simplify the development and deployment of production-ready Java applications. Creating a Spring Boot Project with Spring Initializr: Step-by-Step GuideNote :
5 min read