Skip to content

additionalProfiles overwrites spring.profiles.default #43157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
david0 opened this issue Nov 14, 2024 · 5 comments
Closed

additionalProfiles overwrites spring.profiles.default #43157

david0 opened this issue Nov 14, 2024 · 5 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@david0
Copy link

david0 commented Nov 14, 2024

I would like to be able to do something like this:

	public static void main(String[] args) {
		System.setProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "mongo");
		var application = new SpringApplication(Application.class);
		application.setAdditionalProfiles("default-values");
		application.run(args);
	}

My expectation was that this comes up with mongo,default-values but it comes up with default-values only.

This makes default profiles & additional profiles unusable together.

(I guess #24688 could offer an overall nicer solution for my initial problem, but until then...)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 14, 2024
@nosan
Copy link
Contributor

nosan commented Nov 14, 2024

I believe this is the correct behavior. Default profiles are used only when no active profiles are specified. In your example, you added default-values as an additional active profile, so only default-values is considered active.

You can use ACTIVE_PROFILES_PROPERTY_NAME system property instead of DEFAULT_PROFILES_PROPERTY_NAME

System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "mongo");

com.example.demo.DemoApplication : The following 2 profiles are active: "default-values", "mongo"

@mhalbritter
Copy link
Contributor

mhalbritter commented Nov 14, 2024

This is correct. Documentation for that is here: https://docs.spring.io/spring-framework/reference/core/beans/environment.html#beans-definition-profiles-default

The default profile represents the profile that is enabled if no profile is active.

@mhalbritter mhalbritter added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 14, 2024
@mhalbritter mhalbritter closed this as not planned Won't fix, can't repro, duplicate, stale Nov 14, 2024
@david0
Copy link
Author

david0 commented Nov 14, 2024

I was thinking of additionalProfiles as something that should always be active on top of the rest.

I cannot use active profiles, since we set spring_profiles_active via env in the CI and via YAML in production to disable some development-only profiles.

@philwebb
Copy link
Member

I was thinking of additionalProfiles as something that should always be active on top of the rest.

They're added on top of active/included profiles but not added on top of default profiles. Perhaps you might be able to use spring.profiles.include (https://docs.spring.io/spring-boot/reference/features/profiles.html#features.profiles.adding-active-profiles).

Failing that, you could use an EnvironmentPostProcessor ordered behind ours that sets the profiles directly on ConfigurableEnvironment.

@david0
Copy link
Author

david0 commented Nov 15, 2024

spring.profile.include will also override spring.profiles.default, exactly as setAdditionalProfiles does.

Yes good hint, maybe I should implement EnvironmentPostProcessor, feels a little bit wired to need that for such a simple case, but maybe it is the easiest right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

5 participants