Java 如何通过 gradle 任务使用 spring 配置文件运行 bootRun
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36923288/
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 run bootRun with spring profile via gradle task
提问by Eric Francis
I'm trying to set up gradle to launch the bootRun
process with various spring profiles enabled.
我正在尝试设置 gradle 以启动bootRun
启用了各种弹簧配置文件的过程。
My current bootRun
configuration looks like:
我当前的bootRun
配置看起来像:
bootRun {
// pass command line options from gradle to bootRun
// usage: gradlew bootRun "-Dspring.profiles.active=local,protractor"
if (System.properties.containsKey('spring.profiles.active')) {
systemProperty "spring.profiles.active", System.properties['spring.profiles.active']
}
}
I'd like to set system properties with a gradle
task, and then execute bootRun
.
我想用gradle
任务设置系统属性,然后执行bootRun
.
My attempt looked like this:
我的尝试是这样的:
task bootRunDev
bootRunDev {
System.setProperty("spring.profiles.active", "Dev")
}
A few questions:
几个问题:
- is
systemProperty
a part of the spring boot bootRun configuration? - is it possible to set a system property in another task?
- What should my next step be? I need to get
bootRunDev
configuration to happen beforebootRun
- Is there another approach I should look into
- 是
systemProperty
spring boot bootRun 配置的一部分吗? - 是否可以在另一个任务中设置系统属性?
- 我的下一步应该是什么?我需要让
bootRunDev
配置发生之前bootRun
- 我应该研究另一种方法吗
-Eric
-埃里克
采纳答案by Vaelyr
Simplest way would be to define default and allow it to be overridden. I am not sure what is the use of systemProperty in this case. Simple arguments will do the job.
最简单的方法是定义默认值并允许它被覆盖。我不确定在这种情况下 systemProperty 的用途是什么。简单的参数就可以完成这项工作。
def profiles = 'prod'
bootRun {
args = ["--spring.profiles.active=" + profiles]
}
To run dev:
运行开发:
./gradlew bootRun -Pdev
To add dependencies on your task you can do something like this:
要为您的任务添加依赖项,您可以执行以下操作:
task setDevProperties(dependsOn: bootRun) << {
doFirst {
System.setProperty('spring.profiles.active', profiles)
}
}
There are lots of ways achieving this in Gradle.
在 Gradle 中有很多方法可以实现这一点。
Edit:
编辑:
Configure separate configuration files per environment.
为每个环境配置单独的配置文件。
if (project.hasProperty('prod')) {
apply from: 'gradle/profile_prod.gradle'
} else {
apply from: 'gradle/profile_dev.gradle'
}
Each configuration can override tasks for example:
每个配置都可以覆盖任务,例如:
def profiles = 'prod'
bootRun {
systemProperty "spring.profiles.active", activeProfile
}
Run by providing prod
flag in this case just like that:
prod
在这种情况下通过提供标志来运行,就像这样:
./gradlew <task> -Pprod
回答by david
Environment variables can be used to set spring properties as described in the documentation. So, to set the active profiles (spring.profiles.active
) you can use the following code on Unix systems:
环境变量可用于设置 spring 属性,如文档中所述。因此,要设置活动配置文件 ( spring.profiles.active
),您可以在 Unix 系统上使用以下代码:
SPRING_PROFILES_ACTIVE=test gradle clean bootRun
And on Windows you can use:
在 Windows 上,您可以使用:
SET SPRING_PROFILES_ACTIVE=test
gradle clean bootRun
回答by Yona Appletree
For those folks using Spring Boot 2.0+, you can use the following to setup a task that will run the app with a given set of profiles.
对于那些使用 Spring Boot 2.0+ 的人,您可以使用以下内容来设置一个任务,该任务将使用一组给定的配置文件运行应用程序。
task bootRunDev(type: org.springframework.boot.gradle.tasks.run.BootRun, dependsOn: 'build') {
group = 'Application'
doFirst() {
main = bootJar.mainClassName
classpath = sourceSets.main.runtimeClasspath
systemProperty 'spring.profiles.active', 'dev'
}
}
Then you can simply run ./gradlew bootRunDev
or similar from your IDE.
然后您可以简单地./gradlew bootRunDev
从您的 IDE运行或类似。
回答by Aivaras
For someone from internet, there was a similar question https://stackoverflow.com/a/35848666/906265I do provide the modified answer from it here as well:
对于来自互联网的人,有一个类似的问题https://stackoverflow.com/a/35848666/906265我也在这里提供修改后的答案:
// build.gradle
<...>
bootRun {}
// make sure bootRun is executed when this task runs
task runDev(dependsOn:bootRun) {
// TaskExecutionGraph is populated only after
// all the projects in the build have been evaulated https://docs.gradle.org/current/javadoc/org/gradle/api/execution/TaskExecutionGraph.html#whenReady-groovy.lang.Closure-
gradle.taskGraph.whenReady { graph ->
logger.lifecycle('>>> Setting spring.profiles.active to dev')
if (graph.hasTask(runDev)) {
// configure task before it is executed
bootRun {
args = ["--spring.profiles.active=dev"]
}
}
}
}
<...>
then in terminal:
然后在终端:
gradle runDev
Have used gradle 3.4.1
and spring boot 1.5.10.RELEASE
用过gradle 3.4.1
和spring boot 1.5.10.RELEASE
回答by radistao
Configuration for 4 different task with different profiles and gradle tasks dependencies:
具有不同配置文件和 gradle 任务依赖项的 4 个不同任务的配置:
bootRunLocal
andbootRunDev
- run with specific profilebootPostgresRunLocal
andbootPostgresRunDev
same as prev, but executing custom taskrunPostgresDocker
andkillPostgresDocker
before/after bootRun
bootRunLocal
和bootRunDev
- 使用特定配置文件运行bootPostgresRunLocal
和bootPostgresRunDev
prev 相同,但执行自定义任务runPostgresDocker
和killPostgresDocker
bootRun 之前/之后
build.gradle
:
build.gradle
:
final LOCAL='local'
final DEV='dev'
void configBootTask(Task bootTask, String profile) {
bootTask.main = bootJar.mainClassName
bootTask.classpath = sourceSets.main.runtimeClasspath
bootTask.args = [ "--spring.profiles.active=$profile" ]
// systemProperty 'spring.profiles.active', profile // this approach also may be used
bootTask.environment = postgresLocalEnvironment
}
bootRun {
description "Run Spring boot application with \"$LOCAL\" profile"
doFirst() {
configBootTask(it, LOCAL)
}
}
task bootRunLocal(type: BootRun, dependsOn: 'classes') {
description "Alias to \":${bootRun.name}\" task: ${bootRun.description}"
doFirst() {
configBootTask(it, LOCAL)
}
}
task bootRunDev(type: BootRun, dependsOn: 'classes') {
description "Run Spring boot application with \"$DEV\" profile"
doFirst() {
configBootTask(it, DEV)
}
}
task bootPostgresRunLocal(type: BootRun) {
description "Run Spring boot application with \"$LOCAL\" profile and re-creating DB Postgres container"
dependsOn runPostgresDocker
finalizedBy killPostgresDocker
doFirst() {
configBootTask(it, LOCAL)
}
}
task bootPostgresRunDev(type: BootRun) {
description "Run Spring boot application with \"$DEV\" profile and re-creating DB Postgres container"
dependsOn runPostgresDocker
finalizedBy killPostgresDocker
doFirst() {
configBootTask(it, DEV)
}
}
回答by Rafael
Using this shell command it will work:
使用这个 shell 命令它将工作:
SPRING_PROFILES_ACTIVE=test gradle clean bootRun
Sadly this is the simplest way I have found. It sets environment property for that call and then runs the app.
可悲的是,这是我找到的最简单的方法。它为该调用设置环境属性,然后运行该应用程序。
回答by meiskalt7
Add to VM options: -Dspring.profiles.active=dev
添加到 VM 选项:-Dspring.profiles.active=dev
回答by Mohamed Omar
I wanted it simple just to be able to call gradle bootRunDev like you without having to do any extra typing..
我希望它很简单,只需能够像您一样调用 gradle bootRunDev 而无需进行任何额外的输入。
This worked for me - by first configuring it the bootRun in my task and then right after it running bootRun which worked fine for me :)
这对我有用 - 首先在我的任务中配置 bootRun,然后在它运行 bootRun 之后立即运行,这对我来说很好:)
task bootRunDev {
bootRun.configure {
systemProperty "spring.profiles.active", 'Dev'
}
}
bootRunDev.finalizedBy bootRun
回答by Aivaras
Spring Boot v2 Gradle plugin docsprovide an answer:
Spring Boot v2 Gradle 插件文档提供了答案:
6.1. Passing arguments to your application
Like all JavaExec tasks, arguments can be passed into bootRun from the command line using
--args='<arguments>'
when using Gradle 4.9 or later.
6.1. 将参数传递给您的应用程序
与所有 JavaExec 任务一样,
--args='<arguments>'
使用Gradle 4.9 或更高版本时,可以使用命令行将参数传递到 bootRun 中。
To run server with active profile set to dev:
使用设置为dev 的活动配置文件运行服务器:
$ ./gradlew bootRun --args='--spring.profiles.active=dev'
回答by Jakub Zalas
For anyone looking how to do this in Kotlin DSL, here's a working example for build.gradle.kts
:
对于任何想在 Kotlin DSL 中执行此操作的人,这里有一个工作示例build.gradle.kts
:
tasks.register("bootRunDev") {
group = "application"
description = "Runs this project as a Spring Boot application with the dev profile"
doFirst {
tasks.bootRun.configure {
systemProperty("spring.profiles.active", "dev")
}
}
finalizedBy("bootRun")
}