Reputation: 37008
the easiest way to pass spring profiles to gradle bootRun
is (for me) by environment variable (e.g. SPRING_PROFILES_ACTIVE
), when run on commandline.
Unlike the Application configurations, the config for gradle tasks does not provide an input for environment variables. And as VM options don't get picked up either as it seems, I can not run those tasks from the IDE.
I am aware, that I could start IntelliJ with the envvar set, but this seems rather cumbersome.
So what I need is the IntelliJ pendant for SPRING_PROFILES_ACTIVE=dev,testdb gradle bootRun
, unless there is a good reason, they have left this out.
System is linux, intellij 14. The project in question is using springboot and I want to move over from running main
in IntelliJ to running with springloaded+bootRun
and separate compileGroovy
calls as IntelliJ is not "understanding" the gradle file completely and therefor hides errors.
Upvotes: 37
Views: 34263
Reputation: 13519
Here is my solution for setting up Spring environment variables / settings with Gradle / IntelliJ
Firstly, define a basic properties file, and then one based on your environment, such as:
@Configuration
@PropertySources(value = {@PropertySource("classpath:default.properties"),@PropertySource("classpath:${env}.properties")})
Int the above example, pay careful attention to the @PropertySource("classpath:${env}.properties")
. This is an environment variable being pulled through.
Next, add a VM argument to IntelliJ (via the Gradle Tasks Run Configurations) - or as an argument via the gradle command line.
Lastly, copy the properties across in the gradle task as @cfrick mentioned and @mdjnewman have correctly shown:
tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
bootRun.systemProperties = System.properties
}
Upvotes: 6
Reputation: 37008
Make the System.properties
available in the bootRun
(or other) tasks.
bootRun.systemProperties = System.properties
This way we can set in IntelliJ VM Options like -Dspring.profiles.active=dev
.
Upvotes: 10
Reputation: 156
I've had success adding the following to my build.gradle file:
tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
systemProperty('spring.profiles.active', 'local')
}
This allows gradlew bootRun
to be run from IntelliJ without requiring any changes to the IntelliJ Run/Debug Configurations (and also from the command line without having to manually specify a profile).
Upvotes: 4