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")
}
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
Top comments (4)
I have a few different files like this:
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.
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?
@rob_mitchell_4251b26ffe93 you seem to be building the project, not trying to run it.
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.
Thank You so much for this!