spring 如何将系统属性传递给 Gradle 任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23367507/
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 pass system property to Gradle task
提问by WeMakeSoftware
I'm using Gradle spring-boot
plugin and I need to select a spring active profile for the test run.
我正在使用 Gradlespring-boot
插件,我需要为测试运行选择一个 spring 活动配置文件。
How do I pass spring.profiles.active
system property to the bootRun
plugin's task?
如何将spring.profiles.active
系统属性传递给bootRun
插件的任务?
What has already failed:
已经失败的事情:
task bootRunLocal {
systemProperty "spring.profiles.active", "local"
System.setProperty("spring.profiles.active", "local")
tasks.bootRun.execute() // I suspect that this task is executed in a separate JVM
}
and some command line magic also fails:
一些命令行魔法也失败了:
./gradle -Dspring.profiles.active=local bootRun
Could someone kindly help me solve my troubles?
有人可以帮助我解决我的麻烦吗?
Update from the answers and comments:
从答案和评论中更新:
I'm able to set the systemProperty and pass it to the spring container by doing :
我可以通过执行以下操作来设置 systemProperty 并将其传递给 spring 容器:
run {
systemProperty "spring.profiles.active", "local"
}
However, when I do this, the local profile is being set for both bootRun
task and bootRunLocal
task. I need a way to set this property for bootRunLocal
task and call booRun
task from bootRunLocal
.
但是,当我这样做时,正在为bootRun
任务和bootRunLocal
任务设置本地配置文件。我需要一种方法来为bootRunLocal
任务设置此属性并booRun
从bootRunLocal
.
That might sound very simple, but I come with peace from the structured world of Maven.
这听起来可能很简单,但我从 Maven 的结构化世界中获得了平静。
采纳答案by Vlad
task local {
run { systemProperty "spring.profiles.active", "local" }
}
bootRun.mustRunAfter local
Then run gradle command as:
然后运行 gradle 命令:
gradle bootRun local
回答by Erich
I know I'm late here... but I recently faced this exact issue. I was trying to launch bootRun with spring.profiles.active and spring.config.location set as system properties on the command line.
我知道我迟到了......但我最近遇到了这个确切的问题。我试图在命令行上将 spring.profiles.active 和 spring.config.location 设置为系统属性来启动 bootRun。
So, to get your command line "magic" to work, simply add this to your build.gradle
所以,为了让你的命令行“魔法”发挥作用,只需将它添加到你的 build.gradle
bootRun {
systemProperties System.properties
}
Then running from the command line...
然后从命令行运行...
gradle -Dspring.profiles.active=local bootRun
Will set local as the active profile, without needing to define a separate task simply to add the env variable.
将本地设置为活动配置文件,无需定义单独的任务,只需添加 env 变量即可。
回答by Peter Niederwieser
There is no generic way to pass system properties to a task. In a nutshell, it's only supported for tasks that fork a separate JVM.
没有将系统属性传递给任务的通用方法。简而言之,它仅支持分叉单独 JVM 的任务。
The bootRunLocal
task (as defined above) will not execute in a separate JVM, and calling execute()
on a task isn't supported (and would have to happen in the execution phase in any case). Tests, on the other hand, are always executed in a separate JVM (if executed by a Test
task). To set system properties for test execution, you need to configure the corresponding Test
task(s). For example:
的bootRunLocal
任务(如上所定义)将不会在一个单独的JVM执行,并调用execute()
不支持任务(以及将具有在执行阶段发生在任何情况下)。另一方面,测试总是在单独的 JVM 中执行(如果由Test
任务执行)。要设置测试执行的系统属性,您需要配置相应的Test
任务。例如:
test {
systemProperty "spring.profiles.active", "local"
}
For more information, see Test
in the Gradle Build Language Reference.
欲了解更多信息,请参阅Test
在摇篮构建语言参考。
回答by Soumya
回答by Miko?aj Mitura
For gradle 2.14 below example works.
I have added as below.
When System.properties['spring.profiles.active'] is null then default profile is set.
对于下面的示例 gradle 2.14 有效。我已添加如下。
当 System.properties['spring.profiles.active'] 为 null 时,则设置默认配置文件。
bootRun {
systemProperty 'spring.profiles.active', System.properties['spring.profiles.active']
}
command line example
命令行示例
gradle bootRun -Dspring.profiles.active=dev
回答by norbertas.gaulia
Just for reference if anyone will have this issue:
如果有人遇到这个问题,仅供参考:
Vlad answer didn't quite worked for me but this one works great with 2.4,
弗拉德的回答对我来说不是很有效,但这个在 2.4 上效果很好,
task local <<{
bootRun { systemProperty "spring.profiles.active", "local" }
}
local.finalizedBy bootRun
then gradle local
然后 gradle local
回答by Domo
According to the spring-boot-gradle-plugin documentationyou should be able to pass arguments like this
根据spring-boot-gradle-plugin 文档,您应该能够传递这样的参数
./gradlew bootRun --args='--spring.profiles.active=dev'
Seems like this is a new gradle feature since 4.9. I used it in my project and it worked out of the box.
似乎这是自 4.9 以来的新 gradle 功能。我在我的项目中使用了它,它开箱即用。
回答by driftcatcher
Responding to OP's exact request here ...
在这里回应 OP 的确切要求......
How do I pass spring.profiles.active system property to the bootRun plugin's task?
如何将 spring.profiles.active 系统属性传递给 bootRun 插件的任务?
And assuming by "pass" the OP meant "pass from commandline" or "pass from IDE invocation" ... This is how I like to do it.
假设“通过”OP 意味着“从命令行传递”或“从 IDE 调用传递”......这就是我喜欢这样做的方式。
Add this to build.gradle:
将此添加到 build.gradle:
/**
* Task from spring-boot-gradle-plugin, configured for easier development
*/
bootRun {
/* Lets you pick Spring Boot profile by system properties, e.g. gradle bootRun -Dspring.profiles.active=dev */
systemProperties = System.properties
}
Then when you invoke it, use the familiar Java flag for setting a system property
然后当你调用它时,使用熟悉的 Java 标志来设置系统属性
gradle bootRun -Dspring.profiles.active=local
There is one main advantage of sticking to system properties, over the environment variables option (SPRING_PROFILES_ACTIVE=local gradle bootRun
) ... and that's easy portability between Linux/OS X (bash, etc.) and Windows (cmd.exe anyway).
与环境变量选项 ( SPRING_PROFILES_ACTIVE=local gradle bootRun
)相比,坚持系统属性有一个主要优势……这很容易在 Linux/OS X(bash 等)和 Windows(无论如何都是 cmd.exe)之间移植。
I learned this way from this blog post.
我从这篇博文中学到了这种方式。
(UPDATE: Ah somehow I had missed @Erich's response with same recommendation. Oops! I'm leaving my answer, because of the additional details about portability, etc.)
(更新:啊,不知何故,我错过了@Erich 对相同建议的回应。哎呀!我要留下我的答案,因为关于便携性等的额外细节。)
回答by user3683276
You can create a new task (in discussed case with name bootRunLocal
), that would extend org.springframework.boot.gradle.run.BootRunTask
and setup properties before task execution. You can create such a task with following code:
您可以创建一个新任务(在讨论的情况下使用 name bootRunLocal
),它将org.springframework.boot.gradle.run.BootRunTask
在任务执行之前扩展和设置属性。您可以使用以下代码创建这样的任务:
task bootRunLocal(type: org.springframework.boot.gradle.run.BootRunTask) {
doFirst() {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
systemProperty "spring.profiles.active", "local"
}
}
More details can be found here: https://karolkalinski.github.io/gradle-task-that-runs-spring-boot-aplication-with-profile-activated/
更多细节可以在这里找到:https: //karolkalinski.github.io/gradle-task-that-runs-spring-boot-aplication-with-profile-activated/
回答by CyclingSir
Starting from SpringBoot 2.0.0-M5 setSystemProperties()
is no longer a method of the task bootRun.
The build.gradle needs to be updated to
从 SpringBoot 2.0.0-M5 开始setSystemProperties()
不再是任务 bootRun 的方法。build.gradle 需要更新为
bootRun {
execSpec {
// System.properties["spring.profiles.active"]
systemProperties System.properties
}
}
This is as springBoot's run task uses org.gradle.process.JavaExecSpec
这是作为 springBoot 的运行任务使用的 org.gradle.process.JavaExecSpec
This works for me using Gradle 4.2
这对我使用 Gradle 4.2 有效