How do I activate a Spring Boot profile when running from IntelliJ?

前端 未结 13 1537
醉梦人生
醉梦人生 2021-01-30 00:09

I have 5 environments:

 - local (my development machine)
 - dev
 - qc
 - uat
 - live
 - staging

I want different application properties to be u

相关标签:
13条回答
  • 2021-01-30 00:22

    I ended up adding the following to my build.gradle:

    bootRun {
      environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "local"
    }
    
    test {
      environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "test"
    }
    

    So now when running bootRun from IntelliJ, it defaults to the "local" profile.

    On our other environments, we will simply set the 'SPRING_PROFILES_ACTIVE' environment variable in Tomcat.

    I got this from a comment found here: https://github.com/spring-projects/spring-boot/pull/592

    0 讨论(0)
  • 2021-01-30 00:24

    I added -Dspring.profiles.active=test to VM Options and then re-ran that configuration. It worked perfectly.

    This can be set by

    • Choosing Run | Edit Configurations...
    • Go to the Configuration tab
    • Expand the Environment section to reveal VM options
    0 讨论(0)
  • 2021-01-30 00:26

    Try add this command in your build.gradle

    So for running configure that shape:

    0 讨论(0)
  • 2021-01-30 00:28

    A probable cause could be that you do not pass the command line parameters into the applications main method. I made the same mistake some weeks ago.

    public static final void main(String... args) {
        SpringApplication.run(Application.class, args);
    }
    
    0 讨论(0)
  • 2021-01-30 00:35

    Try this. Edit your build.gradle file as followed.

    ext { profile = project.hasProperty('profile') ? project['profile'] : 'local' }
    
    0 讨论(0)
  • 2021-01-30 00:35

    So for resuming...

    If you have the IntelliJ Ultimate the correct answer is the one provided by Daniel Bubenheim

    But if you don't, create in Run->Edit Configurations and in Configuration tab add the next Environment variable:

    SPRING_PROFILES_ACTIVE=profilename
    

    And to execute the jar do:

    java -jar -Dspring.profiles.active=profilename XXX.jar
    
    0 讨论(0)
提交回复
热议问题