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

前端 未结 13 1535
醉梦人生
醉梦人生 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:12

    I use the Intellij Community Edition. Go to the "Run/Debug Configurations" > Runner tab > Environment variables > click button "...". Add: SPRING_PROFILES_ACTIVE = local

    spring.profiles.active

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

    In my case I used below configuration at VM options in IntelliJ , it was not picking the local configurations but after a restart of IntelliJ it picked configuration details from IntelliJ and service started running.

    -Dspring.profiles.active=local
    
    0 讨论(0)
  • 2021-01-30 00:14

    You can try the above way to activate a profile

    0 讨论(0)
  • Set -Dspring.profiles.active=local under program arguments.

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

    If you actually make use of spring boot run configurations (currently only supported in the Ultimate Edition) it's easy to pre-configure the profiles in "Active Profiles" setting.

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

    Spring Boot seems had changed the way of reading the VM options as it evolves. Here's some way to try when you launch an application in Intellij and want to active some profile:

    1. Change VM options

    Open "Edit configuration" in "Run", and in "VM options", add: -Dspring.profiles.active=local

    It actually works with one project of mine with Spring Boot v2.0.3.RELEASE and Spring v5.0.7.RELEASE, but not with another project with Spring Boot v2.1.1.RELEASE and Spring v5.1.3.RELEASE.

    Also, when running with Maven or JAR, people mentioned this:

    mvn spring-boot:run -Drun.profiles=dev
    

    or

    java -jar -Dspring.profiles.active=dev XXX.jar
    

    (See here: how to use Spring Boot profiles)

    2. Passing JVM args

    It is mentioned somewhere, that Spring changes the way of launching the process of applications if you specify some JVM options; it forks another process and will not pass the arg it received so this does not work. The only way to pass args to it, is:

    mvn spring-boot:run -Dspring-boot.run.jvmArguments="..."
    

    Again, this is for Maven. https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html

    3. Setting (application) env var

    What works for me for the second project, was setting the environment variable, as mentioned in some answer above: "Edit configuration" - "Environment variable", and:

    SPRING_PROFILES_ACTIVE=local
    
    0 讨论(0)
提交回复
热议问题