Java 如何在 Gradle 4.7 中设置 Spring Boot 活动配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50665667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set spring boot active profile in Gradle 4.7
提问by saeedj
Overall:
I'm trying to run gradle build task for a specific spring profile but I've got an error in passing following test:
总体而言:
我正在尝试为特定的 spring 配置文件运行 gradle 构建任务,但在通过以下测试时出错:
au.com.mnpd.security.JwtTokenUtilTest > generateToken_succeeds FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: java.lang.IllegalArgumentException
The test is using some properties from spring developmentprofile (located in application-development.yaml). But I couldn't find any way to pass active profile to gradle build command. I tried followings but again the same issue:
该测试使用了 spring开发配置文件中的一些属性(位于 application-development.yaml 中)。但是我找不到任何方法将活动配置文件传递给 gradle build 命令。我尝试了以下但同样的问题:
- gradlew -Dspring.profiles.active=development build
- gradlew -Pdevelopment build
Question:
Is there anyway to pass active profile to gradle (v 4.7) buildtask like what is applicable for bootRuntask as follows:
问题:
是否有将活动配置文件传递给 gradle (v 4.7)构建任务,例如适用于bootRun任务的内容,如下所示:
bootRun {
bootRun.systemProperty 'spring.profiles.active', 'development'
}
Note:I tried the same for build but build.systemPropertymethod does not exist for build task.
注意:我为构建尝试了相同的方法,但构建任务不存在build.systemProperty方法。
As I'm new in gradle, I'd be grateful is you could share your genuine solutions with me.
由于我是 gradle 新手,如果您能与我分享您的真正解决方案,我将不胜感激。
回答by Louis Jacomet
What you are looking for is setting system properties on the Test
task, which is what will run your unit tests:
您正在寻找的是在Test
任务上设置系统属性,这将运行您的单元测试:
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.
Gradle 不知道bootRun
公开其系统属性的方式。
You thus have to add a configuration in your build script, to expose what you need to the Gradle command line.
因此,您必须在构建脚本中添加配置,以向 Gradle 命令行公开您需要的内容。
Something like:
就像是:
bootRun {
bootRun.systemProperty 'spring.profiles.active', "${springProfile}"
}
and then have a default in gradle.properties
:
然后有一个默认值gradle.properties
:
springProfile = development
and possibly override the value on the command line:
并可能覆盖命令行上的值:
./gradlew -PspringProfile=test build
回答by Rahul Anand
If you using gradle boot run you need to add this to your build.gradle file
如果你使用 gradle boot run 你需要把它添加到你的 build.gradle 文件中
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
然后在构建时您可以使用 gradle bootRun -Dspring.profiles.active=test
or to build you can use gradle build -Dspring.profiles.active=test
或者构建你可以使用 gradle build -Dspring.profiles.active=test