VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/configuring-spring-boot-applications-with-maven-profiles/

⇱ Configuring Spring Boot Applications with Maven Profiles - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Configuring Spring Boot Applications with Maven Profiles

Last Updated : 9 Apr, 2026

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.

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.

👁 Project Metadata

Step 2: Add Dependencies

Add the following dependencies to the project:

👁 Select Dependencies


Project Structure:

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

👁 Project Folder Structure
Project Structure

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.

Step 5: Main Class

No changes are required in the main class of the project.

pom.xml file:

Step 6: Run the Application

Now, run the application, and it will start at port 8080.

👁 Application Runs

Test the Endpoint

GET http://localhost:8080/hello

Output:

👁 GET Request for hello

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:

👁 Install the Dev Profile

2. Activate the Dev Profile:

Activate the dev profile and run the application, using the below command.

mvn spring-boot: run -Pdev

Output:

👁 Activate the Dev Profile

3. Test the Dev Profile:

We can test the endpoint using postman tool.

GET http://localhost:8080/hello

Output:

👁 Test the Dev Profile

Install the prod Profile

Build the project using the prod profile, using the below command.

mvn clean install -Pprod

Output:

👁 Install the Prod Profile

Activate the prod Profile

Activate the prod profile and run the application, using the following command.

mvn spring-boot:run -Pprod

Output:

👁 Activate the prod Profile

Test the prod profile

Now, test the endpoint again using the postman tool.

GET http://localhost:8080/hello

Output:

👁 Test the prod profile

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.

Comment
Article Tags:
Article Tags:

Explore