Issue
I'm trying to set up gradle to launch the bootRun
process with various spring profiles enabled.
My current bootRun
configuration looks like:
bootRun {
// pass command line options from gradle to bootRun
// usage: gradlew bootRun "-Dspring.profiles.active=local,protractor"
if (System.properties.containsKey('spring.profiles.active')) {
systemProperty "spring.profiles.active", System.properties['spring.profiles.active']
}
}
I'd like to set system properties with a gradle
task, and then execute bootRun
.
My attempt looked like this:
task bootRunDev
bootRunDev {
System.setProperty("spring.profiles.active", "Dev")
}
A few questions:
- is
systemProperty
a part of the spring boot bootRun configuration? - is it possible to set a system property in another task?
- What should my next step be? I need to get
bootRunDev
configuration to happen beforebootRun
- Is there another approach I should look into
-Eric
Solution
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
Answered By - Vaelyr
Answer Checked By - Terry (JavaFixing Volunteer)