Setting active profile and config location from command line in spring boot

后端 未结 11 1537
借酒劲吻你
借酒劲吻你 2020-11-27 09:33

I have a spring boot application.

I have three profiles in my application-> development, staging and production. So I have 3 files

  1. ap
相关标签:
11条回答
  • 2020-11-27 10:08

    you can use the following command line:

    java -jar -Dspring.profiles.active=[yourProfileName] target/[yourJar].jar
    
    0 讨论(0)
  • 2020-11-27 10:11

    I think your problem is likely related to your spring.config.location not ending the path with "/".

    Quote the docs

    If spring.config.location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from spring.config.name before being loaded).

    http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files

    0 讨论(0)
  • 2020-11-27 10:12

    There are two different ways you can add/override spring properties on the command line.

    Option 1: Java System Properties (VM Arguments)

    It's important that the -D parameters are before your application.jar otherwise they are not recognized.

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

    Option 2: Program arguments

    java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config
    0 讨论(0)
  • 2020-11-27 10:13

    I had to add this:

    bootRun {
        String activeProfile =  System.properties['spring.profiles.active']
        String confLoc = System.properties['spring.config.location']
        systemProperty "spring.profiles.active", activeProfile
        systemProperty "spring.config.location", "file:$confLoc"
    }
    

    And now bootRun picks up the profile and config locations.

    Thanks a lot @jst for the pointer.

    0 讨论(0)
  • 2020-11-27 10:15
    -Dspring.profiles.active=staging -Dspring.config.location=C:\Config
    

    is not correct.

    should be:

    --spring.profiles.active=staging --spring.config.location=C:\Config
    
    0 讨论(0)
  • 2020-11-27 10:19

    If you use Gradle:

    -Pspring.profiles.active=local
    
    0 讨论(0)
提交回复
热议问题