Spring not overriding active profile from command line

conquester picture conquester · Dec 31, 2018 · Viewed 8.4k times · Source

Here is my application.yml file:

spring:
  freemarker:
    template-loader-path: classpath:/templates

  datasource:
    url: jdbc:postgresql://localhost:5432/myapp
    username: postgres
    password: password
    driver-class-name: org.postgresql.Driver

  jpa:
    show-sql: true
    properties:
      hibernate:
        enable_lazy_load_no_trans: false
        jdbc:
          lob:
            non_contextual_creation: true
        dialect: org.hibernate.dialect.PostgreSQLDialect
    hibernate:
      ddl-auto: create-drop


---

spring:
  profiles:
    active: development


---

spring:
  profiles: staging

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update

logging:
  level:
    root: DEBUG

---

spring:
  profiles: production

  jpa:
    show-sql: false
    hibernate:
      ddl-auto: update

I run the application using:

java -jar application.jar -Dspring.profiles.active=staging

In the log, I can see that spring boot prints out: The following profiles are active: development

So why isn't the active profile set to staging even though I explicitly set it in the command line args?

Answer

Andrew Tobilko picture Andrew Tobilko · Dec 31, 2018

The order matters. To set a system property, use

java -jar -Dspring.profiles.active=staging application.jar

The line you mentioned passes an application argument.