Setting active profile and config location from command line in Spring Boot

Cover Image for Setting active profile and config location from command line in Spring Boot
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Setting Active Profile and Config Location from Command Line in Spring Boot

šŸŒŸ Welcome to my tech blog! Today, we are going to talk about how to set the active profile and config location from the command line in a Spring Boot application. This is a common issue that many developers face, but worry not! We have easy solutions for you. Let's dive in! šŸ’ŖšŸ¼

The Scenario

So, you have a Spring Boot application with three profiles: development, staging, and production. Each profile has its specific configuration file, which is located in the src/main/resources directory:

  1. application-development.yml
  2. application-staging.yml
  3. application-production.yml

Additionally, you have set the active profile in the application.yml file like this:

spring:
  profiles.active: development

To make things even more interesting, the profile-specific config files are stored in the C:\config folder.

The Problem

When you try to run your application using the bootRun Gradle task in Eclipse, you set the command line arguments in your Gradle configuration:

-Dspring.profiles.active=staging -Dspring.config.location=C:\Config

However, you notice that the command line property is not reflecting the desired active profile, and it always defaults to the development profile specified in the application.yml file. Moreover, the application is not searching for profile-specific config files in the C:\Config folder.

The Solution

Don't worry, we're here to help! Let's go step by step to address this issue.

Step 1: Ensure correct command line arguments

First, double-check that you are passing the command line arguments correctly in your Gradle configuration. It should be:

-Dspring.profiles.active=staging -Dspring.config.location=C:\config

Make sure the path to the C:\config folder is spelled correctly and matches the case exactly. Remember that Windows is case-insensitive, so make sure the folder name matches the case.

Step 2: Move config files to the correct location

To make the application consider the C:\config folder for profile specific config files, we need to move them to the correct location. In your case, move the application-staging.yml file to C:\config\application.yml.

Step 3: Check for override order

Spring Boot uses a specific order to determine the active profile and config locations. By default, the command line arguments have a higher priority than the application.yml file. However, if you explicitly set the spring.profiles.active property in the application.yml file, it will override the command line arguments.

To fix this, remove the active profile configuration from the application.yml file:

# Remove this line
spring:
  profiles.active: development

Step 4: Verify active profile

Lastly, let's verify that the active profile is set correctly. In your Spring Boot application, create a simple REST endpoint that returns the active profile:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProfileController {

    @Value("${spring.profiles.active}")
    private String activeProfile;

    @GetMapping("/profile")
    public String getActiveProfile() {
        return activeProfile;
    }
}

Run your application and access http://localhost:8080/profile. It should display "staging" if you've set the correct command line arguments.

Conclusion

Congratulations! You've successfully configured the active profile and config location from the command line in your Spring Boot application. By following the steps outlined in this blog post, you should now be able to set the desired active profile and have your application search for profile-specific config files in the correct location.

If you have any further questions or face any issues, don't hesitate to reach out in the comments section below or on our social media channels. Happy coding! šŸ˜„šŸš€

Did you find this blog post helpful? Let us know in the comments and share it with your fellow developers! Don't forget to subscribe to our newsletter to receive more valuable tips and tricks for your tech journey. Stay tuned for our next blog post, where we'll dive into another exciting topic! šŸ’ŒšŸ“š


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my

Matheus Mello
Matheus Mello