Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot - automatically activate profile based on operating system

Let's say I have a spring profile that is specific to a particular operating system:

spring:
  profiles: mac
  cloud:
    zookeeper:
      discovery:
        preferIpAddress: false
        instanceHost: docker.for.mac.localhost

Is there some way to automatically enable a spring profile based on the current operating system?

So in my case, I want the above profile to automatically become active if it is on a Darwin os.

Maybe there is some way to do it with system properties? In this case, I want the mac profile to be active if os.name contains "Darwin"?

Any ideas?

like image 439
Nicholas DiPiazza Avatar asked Jun 07 '26 18:06

Nicholas DiPiazza


1 Answers

something like this would work for you? Of course you should adjust the condition.

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(DemoApplication.class);
    if (System.getProperty("os.name").contains("darwin")) {
      app.setAdditionalProfiles("mac");
    }
    app.run(args);
  }

}
like image 109
bilak Avatar answered Jun 10 '26 06:06

bilak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!