DEV Community

Jakub Zalas
Jakub Zalas

Posted on • Edited on

8 1

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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (4)

Collapse
 
rob_mitchell_4251b26ffe93 profile image
Rob Mitchell

I have a few different files like this:

  • application-local.properties
  • application-dev.properties
  • application-test.properties

Each has a datasource config defined for my Spring Boot web app.

Using gradle v7.2, I try this:

gradle build --args='--spring.profiles.active=local'

FAILURE: Build failed with an exception.

  • What went wrong: Problem configuring task :build from command line. > Unknown command-line option '--args'.

but this seems to at least start gradle, but has doesn't seem to get a DB connection

gradle build -Dspring.profiles.active=local

any ideas?

Collapse
 
jakub_zalas profile image
Jakub Zalas

@rob_mitchell_4251b26ffe93 you seem to be building the project, not trying to run it.

Collapse
 
robm99x profile image
Rob Mitchell

For running tests (including unit and integration), I used:

gradle test -Dspring.profiles.active=test

and have a file called "application-test.properties" with my name/value properties for testing.

Collapse
 
rizalp profile image
Mohammad Shahrizal Prabowo

Thank You so much for this!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay