24

How to set active profile in spring boot Application. This application will be deployed in stand alone Tomcat.

I have 2 property files application-{profile}.properties.

My Application class

    @SpringBootApplication
        public class Application extends SpringBootServletInitializer {

            @Override
            protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {        
                return application.sources(Application.class);
            }

            public static void main(String[] args) {
               System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
 ApplicationContext ctx = SpringApplication.run(Application.class, args);
      }
    }

if I run the app with embedded tomcat the dev profile is set as active and it works fine. But when I deploy in stand alone tomcat. It does not work.

I tried to set active profile in configure method. but i get null pointer exception, when i get the environment from the context.

Any help on how to set the active profile.

Mukun
  • 1,756
  • 10
  • 30
  • 57

6 Answers6

26

You can set additional profiles on start up:

   SpringApplication springApp = new SpringApplication(Main.class);
   springApp.setAdditionalProfiles("profile1", "profile2");
   springApp.run(args);
Markus Pscheidt
  • 6,853
  • 5
  • 55
  • 76
Alex Stanovsky
  • 1,286
  • 1
  • 13
  • 28
  • 3
    This is a better answer - Reason - The Accepted Answer does not take into account of Deployment done on Micro profile Servers. On these , typically many wars are deployed which would share the active.profiles flag. By doing Accepted Answer - we would override the other app flags ! – user1428716 Oct 14 '19 at 01:54
  • 1
    @user1428716, which is better depends on if you want to replace the active profile or add additional profiles – Kymo Wang Jul 01 '21 at 07:59
  • This adds additional profiles at the beginning, is there a way to set them at the end? e.g if current profile is set to "profile0", `springApp.setAdditionalProfiles("profile1", "profile2");` would make it as profile1, profile2, profile0. – Arun Gopalpuri Oct 28 '22 at 07:28
20

I also had the same problem and after struggling for half a day I ended up with this:

    @SpringBootApplication
    public class MyApplication extends SpringBootServletInitializer {

        public static void main(String[] args) {
            System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
            SpringApplication.run(MyApplication.class, args);
        }

        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev"); 
            super.onStartup(servletContext);
        }
    }
falcon
  • 1,332
  • 20
  • 39
12

Another way of doing it in Spring Boot 2 is by using SpringApplicationBuilder:

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class MySpringProgram {

    public static void main(String[] args) {
        new SpringApplicationBuilder(MySpringProgram.class)
                .profiles("profile1", "profile2")
                .run(args);
    }
}
Markus Pscheidt
  • 6,853
  • 5
  • 55
  • 76
nyxz
  • 6,918
  • 9
  • 54
  • 67
3

Instead of activating the profile dynamically, you can put the profiles as vm-arguments in the catalina.sh

CATALINA_OPTS="-Dspring.profiles.active=dev"
Qkyrie
  • 921
  • 7
  • 10
  • I have a similar situation. As of now, application-{profile} is created and active profile is set while running the application through --spring.profiles.active=dev. Its working fine. Currently the application is ran from eclipse. If I deploy this to tomcat, how can i achieve this? – sunitha Jul 20 '16 at 12:00
  • @sunitha you can set the profile in tomcat vm-rguments and it should work – Mukun Jul 28 '17 at 05:37
3
System.setProperty("spring.profiles.active", "dev");
DAG
  • 6,710
  • 4
  • 39
  • 63
Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
1
SpringApplication app =new SpringApplication(Application.class);
    Properties props = new Properties();
    props.put("AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAM", "dev");
    app.setDefaultProperties(props);
    app.run(args);

I think this code is better solution, because it doesn't set system property

taha
  • 731
  • 2
  • 7
  • 18
  • 3
    Generally, Stack Overflow prefers an explanation over "Code only" answers. Consider adding detail on how this code answers the question. – Vaccano Dec 13 '19 at 01:08