8

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?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
conquester
  • 1,082
  • 2
  • 21
  • 44

3 Answers3

19

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.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
5

Launches a Java application. By command

java [ options ] -jar file.jar [ arguments ]

Spring Profiles spring-docs

The Spring Environment has an API for this, but you would normally set a System property (spring.profiles.active) or an OS environment variable (SPRING_PROFILES_ACTIVE). Also, you can launch your application with a -D argument (remember to put it before the main class or jar archive), as follows:

$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar

In Spring Boot, you can also set the active profile in application.properties, as shown in the following example:

spring.profiles.active=production

You can use a spring.profiles.active Environment property to specify which profiles are active, You could also specify it on the command line by using the following switch: spring-docs

$ java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod

For multiple profiles

$ java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev,hsqldb
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • " --spring.profiles.active=dev,hsqldb " how actually this works? if I run in a same server does it really matter?? which properties it will take at the end? – Prasanna Kumar H A Mar 12 '20 at 21:07
  • From the document https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-adding-active-profiles`The spring.profiles.active property follows the same ordering rules as other properties: The highest PropertySource wins` and for me it seems `hsqldb` takes highest priority @Sam – Ryuzaki L Mar 12 '20 at 21:18
3

you have to specify options before your jar file and arguments after it

java [-options] -jar jarfile [args...]

-Dspring.profiles.active=staging is an option and not argument. so please change it to the following

java -jar -Dspring.profiles.active=staging application.jar
mkjh
  • 1,634
  • 13
  • 25