如何在Gradle 4.7中设置Spring Boot活动配置文件
总体:
我正在尝试为特定的弹簧配置文件运行gradle构建任务,但是在通过以下测试时出现错误:
Overall:
I'm trying to run gradle build task for a specific spring profile but I've got an error in passing following test:
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
该测试使用的是Spring 开发配置文件(位于application-development.yaml 中)的某些属性.但是我找不到任何将活动配置文件传递给gradle build命令的方法. 我尝试了以下操作,但还是遇到了相同的问题:
The test is using some properties from spring development profile (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:
- gradlew -Dspring.profiles.active=development build
- gradlew -Pdevelopment build
问题:
无论如何,是否有将活动配置文件传递给gradle(v 4.7) build 任务的方法,例如适用于 bootRun 任务的任务,如下所示:
Question:
Is there anyway to pass active profile to gradle (v 4.7) build task like what is applicable for bootRun task as follows:
bootRun {
bootRun.systemProperty 'spring.profiles.active', 'development'
}
注意:,我在构建时尝试了同样的方法,但是在构建任务中不存在 build.systemProperty 方法.
Note: I tried the same for build but build.systemProperty method does not exist for build task.
由于我是新手,请与我分享您真正的解决方案,我将不胜感激.
As I'm new in gradle, I'd be grateful is you could share your genuine solutions with me.
您正在寻找的是在Test
任务上设置系统属性,这将运行您的单元测试:
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'
}
评论后进行了编辑-将原始答案保留在下面,因为它可能仍然有用.
Gradle不知道bootRun
公开其系统属性的方式.
Gradle does not know the way bootRun
exposes its system properties.
因此,您必须在构建脚本中添加配置,以将所需的内容显示在Gradle命令行中.
You thus have to add a configuration in your build script, to expose what you need to the Gradle command line.
类似的东西:
bootRun {
bootRun.systemProperty 'spring.profiles.active', "${springProfile}"
}
,然后在gradle.properties
中设置默认值:
and then have a default in gradle.properties
:
springProfile = development
,并且可能会覆盖命令行上的值:
and possibly override the value on the command line:
./gradlew -PspringProfile=test build