Spring not overriding active profile from command line [duplicate]
Asked Answered
D

3

9

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?

Delicatessen answered 31/12, 2018 at 12:57 Comment(0)
S
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.

Swamy answered 31/12, 2018 at 13:1 Comment(0)
M
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
Motivity answered 31/12, 2018 at 14:15 Comment(2)
" --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?Fassett
From the document docs.spring.io/spring-boot/docs/current/reference/html/…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 @SamMotivity
H
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
Higdon answered 31/12, 2018 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.