I\'m trying to set up gradle to launch the bootRun process with various spring profiles enabled.
My current bootRun configuration looks lik
Configuration for 4 different task with different profiles and gradle tasks dependencies:
bootRunLocal and bootRunDev - run with specific profilebootPostgresRunLocal and bootPostgresRunDev same as prev, but executing custom task runPostgresDocker and killPostgresDocker before/after bootRunbuild.gradle:
final LOCAL='local'
final DEV='dev'
void configBootTask(Task bootTask, String profile) {
bootTask.main = bootJar.mainClassName
bootTask.classpath = sourceSets.main.runtimeClasspath
bootTask.args = [ "--spring.profiles.active=$profile" ]
// systemProperty 'spring.profiles.active', profile // this approach also may be used
bootTask.environment = postgresLocalEnvironment
}
bootRun {
description "Run Spring boot application with \"$LOCAL\" profile"
doFirst() {
configBootTask(it, LOCAL)
}
}
task bootRunLocal(type: BootRun, dependsOn: 'classes') {
description "Alias to \":${bootRun.name}\" task: ${bootRun.description}"
doFirst() {
configBootTask(it, LOCAL)
}
}
task bootRunDev(type: BootRun, dependsOn: 'classes') {
description "Run Spring boot application with \"$DEV\" profile"
doFirst() {
configBootTask(it, DEV)
}
}
task bootPostgresRunLocal(type: BootRun) {
description "Run Spring boot application with \"$LOCAL\" profile and re-creating DB Postgres container"
dependsOn runPostgresDocker
finalizedBy killPostgresDocker
doFirst() {
configBootTask(it, LOCAL)
}
}
task bootPostgresRunDev(type: BootRun) {
description "Run Spring boot application with \"$DEV\" profile and re-creating DB Postgres container"
dependsOn runPostgresDocker
finalizedBy killPostgresDocker
doFirst() {
configBootTask(it, DEV)
}
}
For anyone looking how to do this in Kotlin DSL, here's a working example for build.gradle.kts:
tasks.register("bootRunDev") {
group = "application"
description = "Runs this project as a Spring Boot application with the dev profile"
doFirst {
tasks.bootRun.configure {
systemProperty("spring.profiles.active", "dev")
}
}
finalizedBy("bootRun")
}
For someone from internet, there was a similar question https://stackoverflow.com/a/35848666/906265 I do provide the modified answer from it here as well:
// build.gradle
<...>
bootRun {}
// make sure bootRun is executed when this task runs
task runDev(dependsOn:bootRun) {
// TaskExecutionGraph is populated only after
// all the projects in the build have been evaulated https://docs.gradle.org/current/javadoc/org/gradle/api/execution/TaskExecutionGraph.html#whenReady-groovy.lang.Closure-
gradle.taskGraph.whenReady { graph ->
logger.lifecycle('>>> Setting spring.profiles.active to dev')
if (graph.hasTask(runDev)) {
// configure task before it is executed
bootRun {
args = ["--spring.profiles.active=dev"]
}
}
}
}
<...>
then in terminal:
gradle runDev
Have used gradle 3.4.1 and spring boot 1.5.10.RELEASE
Add to VM options: -Dspring.profiles.active=dev
Or you can add it to the build.gradle file to make it work: bootRun.systemProperties = System.properties.
In your build.gradle file simply use the following snippet
bootRun {
args = ["--spring.profiles.active=${project.properties['profile'] ?: 'prod'}"]
}
And then run following command to use dev profile:
./gradlew bootRun -Pprofile=dev
Environment variables can be used to set spring properties as described in the documentation. So, to set the active profiles (spring.profiles.active) you can use the following code on Unix systems:
SPRING_PROFILES_ACTIVE=test gradle clean bootRun
And on Windows you can use:
SET SPRING_PROFILES_ACTIVE=test
gradle clean bootRun