How can I activate Profile in Spring Boot YAML? - spring-boot

I have below application YAML of my Spring Boot (2.4.5) application with two profiles.
spring:
profiles: dev
spring:
profiles: prod
Now when I pass the arguments -Dspring.profiles.active=dev, it actually doesn't pickup the profile. Is there any changes in profile has been made recently.
When I activate the profile, like below it works
spring:
profiles:
active: dev
But as per the env profile can be different. I don't want to hard code the profile

You should use the following instead in your application.yml:
spring
config:
activate:
on-profile: dev
---
spring
config:
activate:
on-profile: prod

Related

spring boot 3 profile property values getting overridden in boostrap.yaml

Recently trying to upgrade to Spring Boot 3, but facing property issue.
My bootstrap yml looks like below,
When I run with profile dev, the configserver url is always getting connected to qa, basically whichever is declared last.
This is working fine in 2.7.X version of spring, but not in spring boot 3
spring:
application:
name: appname
profiles:
active: ${spring.profiles.active}
include:
- global-defaults
group:
dev:
- config-server-dev
- cloud-config-dev
qa:
- config-server-qa
- cloud-config-qa
---
spring:
config:
activate:
on-profile: config-server-dev
cloud:
config:
enabled: true
uri: http://urltoconfigserverdev
label: master
---
spring:
config:
activate:
on-profile: config-server-qa
cloud:
config:
enabled: true
uri: http://urltoconfigserverqa
label: master
---
But the same is working fine if add the property migration pom dependency. which is not suggested for higher environments.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>
If adding the properties migrator fixes the problem, it's likely that your configuration file is using a configuration property name that's been removed after deprecation. I'm not seeing anything specific to Spring Boot in the changelog, maybe this is due to a change in spring cloud config? The properties migrator should print WARN logs during startup, looking at those should tell you what needs fixing.

How can use ConfigDataEnvironmentPostProcessor and spring.profiles.group?

I'm using Spring Boot 2.4.2 and tried to use spring.profiles.group in my property files like:
application.yaml
spring:
profiles:
group:
local: core
application-core.yaml
---
spring:
config:
activate:
on-profile: local
...
but the "group" property is not found.
As I checked, it is using deprecated ConfigFileApplicationListener instead of ConfigDataEnvironmentPostProcessor.
Why it uses legacy spring profile processing even though version of spring is upper 2.4.0?
application-core.yaml doesn't require the following:
---
spring:
config:
activate:
on-profile: local
This is what is needed:
application.yaml:
spring:
profiles:
group:
local: core
application-core.yaml
...

spring boot - application yml for each developer

In my work the fields in application.yml are dynamics and each developer have another (dynamic) configuration. So, we have the general yml section, that relevant for all of us (and have updates from time to time) and the “private profile”
Like this:
spring:
profiles: default
configuration:
...
...
spring:
profiles: development1
configuration: ...
spring:
profiles: development2
configuration: ...
We have “small” problems with this (mistakes, conflicts, etc.) and we’re look for solution.
I think about separate yml file for each developer, such as:
spring:
profiles: development1
configuration:
yml-file: app_dev1.yml
spring:
profiles: development2
configuration:
yml-file: app_dev2.yml
but didn’t find how to do this…
Will be glad to know if it can be done? (or you have another way to separate the yml between us).
We do not want to set an application yaml with annotation (git issues again..), something like Environment Variables...
Spring boot by default loads application.yaml but in addition if you run with --spring.profiles.active=dev1 it will load file application-dev1.yaml
So you could create yaml file per developer + have some "common" configuration in application.yaml
With such an approach you won't need lines like:
spring:
profiles: dev1
... in any of the yaml files

Spring cloud config in local dev mode

Is it possible to control if the application requires spring-config-server based on profile.
I want to pick properties from resource/... in say local-dev profile and use cloud-config only with a profile where there would be a config-server running.
You can disable using config-server for a specific profile. Please try to define the properties in bootstrap.yml like below. Please note, we should configure these settings only in bootstrap.yml. Setting these properties in application.yml will not work. In this case, local profile will be running only with local profiles in /resources folder. In dev profile, properties from config-server will override properties from resources/ folder.
spring:
profiles: local
cloud:
config:
enabled: false
---
spring:
profiles: dev
cloud:
config:
uri: http://<your config server>

Spring Boot + Spring Cloud Config - How to add more profiles from the Git external configuration

I have this configuration in a spring boot application:
spring:
application:
name: my-app
profiles:
active: ${ENVIRONMENT}
cloud:
config:
uri: http://localhost:8888
My config server reads the following files:
my-app-dev.yaml
prop: dev property
my-app-pro.yaml
prop: pro property.
I launch the spring boot app setting -DENVIRONMENT=dev, loading correctly the dev external Git properties.
When I inject the Environment in let's say a controller and do env.getActiveProfiles() I get "dev" as expected.
I would like to add more profiles from the git configuration. For instance:
my-app-dev.yaml
prop: dev property
spring:
active:
profiles: dev,business1
my-app-pro.yaml
prop: dev property
spring:
active:
profiles: pro,business2
So that env.getActiveProfiles() returns ["dev","business1"]. However what it returns is the initial "dev".
How could this be done?
UPDATE:
As suggested by Dave Syer I tried using spring.profiles.include in the Git files but the new profiles aren't added to the Environment:
my-app-dev.yaml
prop: dev property
spring:
profiles:
include: business1
my-app-pro.yaml
prop: dev property
spring:
profiles:
include: business2
environment.getActiveProfiles() ---> "dev"
Update your spring-boot to 1.5.4.
I tested the same case on my Mac, I found that spring with different version behaves different.
When I'm using Spring Boot 1.3.8.RELEASE with Spring Cloud Brixton.SR7, I got the [dev] profile as active profile(also with Spring Boot 1.4.5.RELEASE)
When I'm using Spring Boot 1.5.4.RELEASE with Spring Cloud Dalston.SR1, I got the
[business1, dev] profile as active profile
So I believe it is a bug in Spring Boot 1.3.x and 1.4.x

Resources