如何通过梯度任务运行带有弹簧配置文件的 bootRun

我试图建立梯度启动 bootRun过程与各种弹簧配置文件启用。

我当前的 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']
}
}

我想用一个 gradle任务设置系统属性,然后执行 bootRun

我的尝试是这样的:

task bootRunDev


bootRunDev  {
System.setProperty("spring.profiles.active", "Dev")
}

有几个问题:

  1. systemProperty是春季启动引导运行配置的一部分吗?
  2. 是否可以在另一个任务中设置系统属性?
  3. 我的下一步应该是什么? 我需要让 bootRunDev配置发生在 bootRun之前
  4. 还有别的办法吗

艾瑞克

180675 次浏览

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.

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.

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:

./gradlew <task> -Pprod

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_PROFILES_ACTIVE=test gradle clean bootRun

And on Windows you can use:

SET SPRING_PROFILES_ACTIVE=test
gradle clean bootRun

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.

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.

For someone from internet, there was a similar question https://stackoverflow.com/a/35848666/906265 I do provide the modified answer from it here as well:

// 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

Configuration for 4 different task with different profiles and gradle tasks dependencies:

  • bootRunLocal and bootRunDev - run with specific profile
  • bootPostgresRunLocal and bootPostgresRunDev same as prev, but executing custom task runPostgresDocker and killPostgresDocker before/after bootRun

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)
}
}

Using this shell command it will work:

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.

Add to VM options: -Dspring.profiles.active=dev

Or you can add it to the build.gradle file to make it work: bootRun.systemProperties = System.properties.

I wanted it simple just to be able to call gradle bootRunDev like you without having to do any extra typing..

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 :)

task bootRunDev {
bootRun.configure {
systemProperty "spring.profiles.active", 'Dev'
}
}


bootRunDev.finalizedBy bootRun

Spring Boot v2 Gradle plugin docs provide an answer:

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.

To run server with active profile set to dev:

$ ./gradlew bootRun --args='--spring.profiles.active=dev'

For anyone looking how to do this in Kotlin DSL, here's a working example for 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")
}

In your build.gradle file simply use the following snippet

bootRun {
args = ["--spring.profiles.active=${project.properties['profile'] ?: 'prod'}"]
}

And then run following command to use dev profile:

./gradlew bootRun -Pprofile=dev

Kotlin edition: Define the following task in you build.gradle.kts file:

tasks.named<BootRun>("bootRun") {
args("--spring.profiles.active=dev")
}

This will pass the parameter --spring.profiles.active=dev to bootRun, where the profile name is dev in this case.

Every time you run gradle bootRun the profile dev is used.

my way:

in gradle.properties:

profile=profile-dev

in build.gradle add VM options -Dspring.profiles.active:

bootRun {
jvmArgs = ["-Dspring.output.ansi.enabled=ALWAYS","-Dspring.profiles.active="+profile]
}

this will override application spring.profiles.active option

Working solution for Spring Boot 2.5+

tasks.register('runDev') {
dependsOn 'bootRun'
bootRun.systemProperty('spring.profiles.active', 'dev')
}

and to run:

./gradlew runDev

For the command line as of Gradle 7.5 and Spring Boot 2.7.3, I do this (if it helps anyone):

gradle bootRun --args=--spring.profiles.active=myprofile