How to run Spring Boot with a dev profile and Gradle

jakub_zalas

Jakub Zalas

Posted on June 16, 2020

How to run Spring Boot with a dev profile and Gradle

I like to use the dev profile in my Spring Boot projects to store the application configuration for development. Spring Boot will conviniently load the configuration dedicated to the profile we've selected when starting the app (i.e. application-dev.yaml).

To use the dev profile, all I need to do with maven is to pass -Dspring.profiles.active=dev property to my spring-boot:run target.

Gradle's bootRun task supports passing --args='--spring.profiles.active=dev' with the Spring Boot Gradle plugin.

There's an alternative too. All we need is a task that would set the property, before spring boot app starts.

Here's an example from build.gradle.kts:

tasks.register("bootRunDev") {
    group = "application"
    description = "Runs the Spring Boot application with the dev profile"
    doFirst {
        tasks.bootRun.configure {
            systemProperty("spring.profiles.active", "dev")
        }
    }
    finalizedBy("bootRun")
}
Enter fullscreen mode Exit fullscreen mode

Usage:

$ ./gradlew bootRunDev

> Task :bootRun
  ,---.    ,-----. ,--.   ,--. ,------. 
 /  O  \  '  .--./ |   `.'   | |  .---' 
|  .-.  | |  |     |  |'.'|  | |  `--,  
|  | |  | '  '--'\ |  |   |  | |  `---. 
`--' `--'  `-----' `--'   `--' `------'
:: Spring Boot ::  (v2.2.5.RELEASE)

2020-06-11 09:52:53.382  INFO 25367 --- [  restartedMain] c.k.demo.ApplicationKt       : Starting ApplicationKt on MacBook-Pro.local with PID 25367 (/demo/build/classes/kotlin/main started by kuba in /demo)
2020-06-11 09:52:53.385  INFO 25367 --- [  restartedMain] c.h.demo.ApplicationKt       : The following profiles are active: dev
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
jakub_zalas
Jakub Zalas

Posted on June 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related