How to set environment vars in IntelliJ for Gradle tasks

前端 未结 3 1625
小蘑菇
小蘑菇 2021-02-05 02:46

the easiest way to pass spring profiles to gradle bootRun is (for me) by environment variable (e.g. SPRING_PROFILES_ACTIVE), when run on commandline.

相关标签:
3条回答
  • 2021-02-05 03:10

    I've had success adding the following to my build.gradle file:

    tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
        systemProperty('spring.profiles.active', 'local')
    }
    

    This allows gradlew bootRun to be run from IntelliJ without requiring any changes to the IntelliJ Run/Debug Configurations (and also from the command line without having to manually specify a profile).

    0 讨论(0)
  • 2021-02-05 03:11

    Here is my solution for setting up Spring environment variables / settings with Gradle / IntelliJ

    Firstly, define a basic properties file, and then one based on your environment, such as:

    @Configuration
    @PropertySources(value = {@PropertySource("classpath:default.properties"),@PropertySource("classpath:${env}.properties")})
    

    Int the above example, pay careful attention to the @PropertySource("classpath:${env}.properties"). This is an environment variable being pulled through.

    Next, add a VM argument to IntelliJ (via the Gradle Tasks Run Configurations) - or as an argument via the gradle command line.

    How to add a VM argument to the Gradle run configuration Lastly, copy the properties across in the gradle task as @cfrick mentioned and @mdjnewman have correctly shown:

    tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
        bootRun.systemProperties = System.properties
    }
    
    0 讨论(0)
  • 2021-02-05 03:18

    Make the System.properties available in the bootRun (or other) tasks.

    bootRun.systemProperties = System.properties
    

    This way we can set in IntelliJ VM Options like -Dspring.profiles.active=dev.

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