How to set spring boot active profile in Gradle 4.7

时光怂恿深爱的人放手 提交于 2019-12-05 12:27:08

What you are looking for is setting system properties on the Test task, which is what will run your unit tests:

test {
  systemProperty 'spring.profiles.active', 'development'
}

Edited after comment - leaving the original answer below as it may still be useful.

Gradle does not know the way bootRun exposes its system properties.

You thus have to add a configuration in your build script, to expose what you need to the Gradle command line.

Something like:

bootRun {
    bootRun.systemProperty 'spring.profiles.active', "${springProfile}"
}

and then have a default in gradle.properties:

springProfile = development

and possibly override the value on the command line:

./gradlew -PspringProfile=test build

If you using gradle boot run you need to add this to your build.gradle file

bootRun {
    String activeProfile =  System.properties['spring.profiles.active']
    systemProperty "spring.profiles.active", activeProfile
}

and then at the time of building you can use gradle bootRun -Dspring.profiles.active=test

or to build you can use gradle build -Dspring.profiles.active=test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!