How to add an active spring profile from an environment variable?
Asked Answered
C

5

11

Until now, I'm setting the following environment variable in my ~/.bash_profile :

export SPRING_PROFILES_ACTIVE=local

This set my active spring profile. But now, I want to add the local profile to other profiles defined in application.properties and not replace them.

In the Spring Boot documentation, there is a section about adding active profile, but I see nothing about adding active profile from an environment variable.

I've tried to set the SPRING_PROFILES_INCLUDE environment variable, but this has no effect.

How to do this?

P.S.: I'm using Spring Boot 1.4.2.

Connaught answered 16/11, 2016 at 16:41 Comment(2)
have you tried utilizing ConfigurableEnvironment docs.spring.io/spring-framework/docs/current/javadoc-api/org/…Williswillison
No, I didn't, but I prefer to use a solution such as the Olszewski one.Connaught
R
11

With default addition profile

You can introduce your own environment variable in the application.properties file, next to the defined profiles using an expression. For instance, if your current file looks like this:

spring.profiles.active=profile1,profile2

with a custom environment variable it will change into:

spring.profiles.active=profile1,profile2,${ADDITIONAL_APP_PROFILES:local}

where ADDITIONAL_APP_PROFILES is the name of the environment variable which you set instead of SPRING_PROFILES_ACTIVE.

The value local is used when the variable is not set on a current environment. In that case, the profile called local will be activated. If you don't set the default value and the environment variable is not present, the whole expression will be used as the name of an active profile.

Without default addition profile

If you like to avoid activating the default profile, you can remove the placeholder value and the comma before the variable expression:

spring.profiles.active=profile1,profile2${ADDITIONAL_APP_PROFILES}

but in that case the variable set on a current environment have to start with a comma:

export ADDITIONAL_APP_PROFILES=,local
Recognizor answered 16/11, 2016 at 17:9 Comment(0)
T
15

The next sentence in the documentation you linked to:

Sometimes it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally add active profiles.

So you can launch your application with a command-line parameter:

-Dspring.profiles.include=${SPRING_PROFILES_INCLUDE}
Tahsildar answered 16/11, 2016 at 17:26 Comment(0)
R
11

With default addition profile

You can introduce your own environment variable in the application.properties file, next to the defined profiles using an expression. For instance, if your current file looks like this:

spring.profiles.active=profile1,profile2

with a custom environment variable it will change into:

spring.profiles.active=profile1,profile2,${ADDITIONAL_APP_PROFILES:local}

where ADDITIONAL_APP_PROFILES is the name of the environment variable which you set instead of SPRING_PROFILES_ACTIVE.

The value local is used when the variable is not set on a current environment. In that case, the profile called local will be activated. If you don't set the default value and the environment variable is not present, the whole expression will be used as the name of an active profile.

Without default addition profile

If you like to avoid activating the default profile, you can remove the placeholder value and the comma before the variable expression:

spring.profiles.active=profile1,profile2${ADDITIONAL_APP_PROFILES}

but in that case the variable set on a current environment have to start with a comma:

export ADDITIONAL_APP_PROFILES=,local
Recognizor answered 16/11, 2016 at 17:9 Comment(0)
P
1

This is an example of adding a programmatically additional active profile from system env or jvm arg.

@Configuration
public class ApplicationInitializer implements WebApplicationInitializer, ApplicationContextInitializer<ConfigurableWebApplicationContext> {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("contextInitializerClasses", this.getClass().getCanonicalName());
    }

    @Override
    public void initialize(ConfigurableWebApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        environment.addActiveProfile(System.getProperty("myProperty"));
        environment.addActiveProfile(System.getEnv("myProperty"));
    }
}
Puerperium answered 28/3, 2019 at 9:22 Comment(0)
B
0

Here is not the Spring way, but also could be useful in some cases.

public static void main(String[] args) {
    System.setProperty("spring.profiles.active", "local");
    SpringApplication.run(MainApplication.class, args);
}
Blastopore answered 28/11, 2022 at 14:51 Comment(0)
U
-2

To support bash environment, available values are SPRING_PROFILES_ACTIVE and SPRING_PROFILES_DEFAULT

not, SPRING_PROFILES_INCLUDE

you probably have to resort commandline way -Dspring.profiles.include or pro grammatically workout with ConfigurableEnvironment

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/env/AbstractEnvironment.html#ACTIVE_PROFILES_PROPERTY_NAME

Unhurried answered 16/11, 2016 at 17:21 Comment(1)
Sorry, but I only understand half of your post. Could you please rephrase it?Scheelite

© 2022 - 2024 — McMap. All rights reserved.