How to start bootRun with spring profile using gradle task

I am trying to configure gradle to start the bootRun process with spring profiles enabled.

My current bootRun configuration is as follows:

 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 would like to set the system properties using the gradle task and then execute bootRun .

My attempt looked like this:

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

A few questions:

  • is systemProperty part of the bootRun spring boot configuration?
  • Is it possible to set a system property in another task?
  • What should be my next step? I need to get bootRunDev configuration before bootRun
  • Is there any other approach that I should consider in

Eric

+23
source share
8 answers

The easiest way is to define a default value and allow it to be overridden. I am not sure what the use of systemProperty is 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 to your task, you can do something like this:

 task setDevProperties(dependsOn: bootRun) << { doFirst { System.setProperty('spring.profiles.active', profiles) } } 

Gradle has many ways to achieve this.

Edit:

Configure separate configuration files for each 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 the prod flag in this case like this:

 ./gradlew <task> -Pprod 
+23
source

Environment variables can be used to set spring properties as described in the documentation . So, to install 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 
+25
source

For those who use Spring Boot 2. 0+, you can use the following to set up a task that will launch the application 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.

+7
source

Using this shell command, it will work:

 SPRING_PROFILES_ACTIVE=test gradle clean bootRun 

Unfortunately, this is the easiest way I've found. It sets the environment property for this call and then launches the application.

+3
source

For someone from the Internet, a similar question arose fooobar.com/questions/1004933 / ... Here I will also provide a modified answer:

 // 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 the terminal:

 gradle runDev 

Used gradle 3.4.1 and spring boot 1.5.10.RELEASE

+2
source

Configuration for 4 different tasks with different profiles and dependencies of graphic tasks:

  • bootRunLocal and bootRunDev - start with a specific profile
  • bootPostgresRunLocal and bootPostgresRunDev same as the previous ones, but doing the custom task runPostgresDocker and killPostgresDocker before / after loading

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) } } 
+2
source

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

+1
source

I wanted it to be just to be able to call gradle bootRunDev like you, without the need for any extra printing.

This worked for me - first setting up bootRun in my task, and then right after starting bootRun, which worked fine for me :)

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

All Articles