How to pass system property to Gradle task

前端 未结 14 2502
别跟我提以往
别跟我提以往 2020-12-08 02:17

I\'m using Gradle spring-boot plugin and I need to select a spring active profile for the test run.

How do I pass spring.profiles.active sy

相关标签:
14条回答
  • 2020-12-08 02:59

    Another way which doesn't require any support from the gradle task: Set the JAVA_TOOL_OPTIONS environment variable:

    JAVA_TOOL_OPTIONS='-Dfoo=bar' gradle ...
    

    Or if the variable might already contain anything useful:

    JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -Dfoo=bar" gradle ...
    
    0 讨论(0)
  • 2020-12-08 03:00

    Responding to OP's exact request here ...

    How do I pass spring.profiles.active system property to the bootRun plugin's task?

    And assuming by "pass" the OP meant "pass from commandline" or "pass from IDE invocation" ... This is how I like to do it.

    Add this to build.gradle:

    /**
     * Task from spring-boot-gradle-plugin, configured for easier development
     */
    bootRun {
        /* Lets you pick Spring Boot profile by system properties, e.g. gradle bootRun -Dspring.profiles.active=dev */
        systemProperties = System.properties
    }
    

    Then when you invoke it, use the familiar Java flag for setting a system property

    gradle bootRun -Dspring.profiles.active=local
    

    There is one main advantage of sticking to system properties, over the environment variables option (SPRING_PROFILES_ACTIVE=local gradle bootRun) ... and that's easy portability between Linux/OS X (bash, etc.) and Windows (cmd.exe anyway).

    I learned this way from this blog post.

    (UPDATE: Ah somehow I had missed @Erich's response with same recommendation. Oops! I'm leaving my answer, because of the additional details about portability, etc.)

    0 讨论(0)
  • 2020-12-08 03:00

    Starting from SpringBoot 2.0.0-M5 setSystemProperties() is no longer a method of the task bootRun. The build.gradle needs to be updated to

    bootRun {
        execSpec {
            // System.properties["spring.profiles.active"]
           systemProperties System.properties
        }
    }
    

    This is as springBoot's run task uses org.gradle.process.JavaExecSpec

    This works for me using Gradle 4.2

    0 讨论(0)
  • SPRING_PROFILES_ACTIVE=local gradle clean bootRun
    

    This is according to this and this and it works.

    0 讨论(0)
  • 2020-12-08 03:07

    Just for reference if anyone will have this issue:

    Vlad answer didn't quite worked for me but this one works great with 2.4,

    task local <<{
        bootRun { systemProperty "spring.profiles.active", "local" }
    }
    local.finalizedBy bootRun
    

    then gradle local

    0 讨论(0)
  • 2020-12-08 03:08

    You can create a new task (in discussed case with name bootRunLocal), that would extend org.springframework.boot.gradle.run.BootRunTask and setup properties before task execution. You can create such a task with following code:

    task bootRunLocal(type: org.springframework.boot.gradle.run.BootRunTask) {
        doFirst() {
            main = project.mainClassName
            classpath = sourceSets.main.runtimeClasspath
            systemProperty "spring.profiles.active", "local"
        }
    }
    

    More details can be found here: https://karolkalinski.github.io/gradle-task-that-runs-spring-boot-aplication-with-profile-activated/

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