Search code examples
javaspringgradle

How to run bootRun with Spring profile via Gradle task?


I'm trying to set up Gradle to launch the bootRun process with various Spring profiles enabled.

My current bootRun configuration looks like:

bootRun {
    // pass command line options from gradle to bootRun
    // usage: gradlew bootRun "-Dspring.profiles.active=local,protractor"
    if (System.properties.containsKey('spring.profiles.active')) {
        systemProperty "spring.profiles.active", System.properties['spring.profiles.active']
    }
}

I'd like to set system properties with a gradle task, and then execute bootRun.

My attempt looked like this:

task bootRunDev

bootRunDev  {
    System.setProperty("spring.profiles.active", "Dev")
}

A few questions:

  1. Is systemProperty a part of the Spring Boot bootRun configuration?
  2. Is it possible to set a system property in another task?
  3. What should my next step be? I need to get bootRunDev configuration to happen before bootRun?
  4. Is there another approach I should look into?

Solution

  • Spring Boot v2 Gradle plugin docs provide an answer:

    6.1. Passing arguments to your application

    Like all JavaExec tasks, arguments can be passed into bootRun from the command line using --args='<arguments>' when using Gradle 4.9 or later.

    To run server with active profile set to dev:

    $ ./gradlew bootRun --args='--spring.profiles.active=dev'