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

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:
application-development.ymlapplication-staging.ymlapplication-production.yml
Additionally, you have set the active profile in the application.yml file like this:
spring:
profiles.active: developmentTo 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:\ConfigHowever, 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:\configMake 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: developmentStep 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! šš
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



