How to run bootRun with spring profile via gradle task
Simplest way would be to define default and allow it to be overridden. I am not sure what is the use of systemProperty in this case. Simple arguments will do the job.
def profiles = 'prod'
bootRun {
args = ["--spring.profiles.active=" + profiles]
}
To run dev:
./gradlew bootRun -Pdev
To add dependencies on your task you can do something like this:
task setDevProperties(dependsOn: bootRun) << {
doFirst {
System.setProperty('spring.profiles.active', profiles)
}
}
There are lots of ways achieving this in Gradle.
Edit:
Configure separate configuration files per environment.
if (project.hasProperty('prod')) {
apply from: 'gradle/profile_prod.gradle'
} else {
apply from: 'gradle/profile_dev.gradle'
}
Each configuration can override tasks for example:
def profiles = 'prod'
bootRun {
systemProperty "spring.profiles.active", activeProfile
}
Run by providing prod
flag in this case just like that:
./gradlew <task> -Pprod
For those folks using Spring Boot 2.0+, you can use the following to setup a task that will run the app with a given set of profiles.
task bootRunDev(type: org.springframework.boot.gradle.tasks.run.BootRun, dependsOn: 'build') {
group = 'Application'
doFirst() {
main = bootJar.mainClassName
classpath = sourceSets.main.runtimeClasspath
systemProperty 'spring.profiles.active', 'dev'
}
}
Then you can simply run ./gradlew bootRunDev
or similar from your IDE.
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
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'