Eric Francis
Eric Francis

Reputation: 24287

How to run bootRun with Spring profile via Gradle task?

I'm trying to set up Gradle to launch the bootRun process with various Spring profiles enabled.

My current bootRun configuration looks like:

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.

My attempt looked like this:

task bootRunDev

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

A few questions:

  1. Is systemProperty a part of the Spring Boot bootRun configuration?
  2. Is it possible to set a system property in another task?
  3. What should my next step be? I need to get bootRunDev configuration to happen before bootRun?
  4. Is there another approach I should look into?

Upvotes: 130

Views: 262647

Answers (18)

deamon
deamon

Reputation: 92437

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

tasks.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.

Upvotes: 11

Lance Peterson
Lance Peterson

Reputation: 121

As of today (February 2024), this works for me. It sets a default active profile every time I run Spring Boot, and also allows me to override the profile from the command line.

bootRun {
  systemProperty 'spring.profiles.active', findProperty('spring.profiles.active') ?: 'development'
}

Then from the command line, I can override the active profile(s) with...

./gradlew -Pspring.profiles.active=staging

...replacing "staging" with a comma-delimited list of profiles as needed If I don't pass anything, it defaults to the "development" profile.

How it works: The -Pspring.profiles.active sets a Gradle project property, which is then used in the bootRun block above to set a system property for the Spring Boot application itself. If the project property is not found, it defaults to setting the application's system property to "development".

I found this technique very clearly explained in the Spring Boot Gradle Plugin doc here: https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#running-your-application.passing-system-properties.

Upvotes: 0

Vaelyr
Vaelyr

Reputation: 3176

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", profiles
}

Run by providing prod flag in this case just like that:

./gradlew <task> -Pprod

Upvotes: 70

maruf571
maruf571

Reputation: 1945

For Gragle Kotlin, following works for me

tasks.bootRun {
    args("--spring.profiles.active=dev")
}

Upvotes: 2

Shubham K.
Shubham K.

Reputation: 105

If you are using jib for containerisation then, Add these two line of codes in your build.gradle and application.properties files respectively.

jib.container.jvmFlags = ['-Dspring.profiles.active=test']
spring.profiles.active=test

that's it.

Upvotes: 0

Patrice Gagnon
Patrice Gagnon

Reputation: 1464

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

Upvotes: 15

DevTomek
DevTomek

Reputation: 653

Working solution for Spring Boot 2.5+

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

and to run:

./gradlew runDev

Upvotes: 2

Dmitriy
Dmitriy

Reputation: 343

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

Upvotes: 2

meiskalt7
meiskalt7

Reputation: 656

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.

Upvotes: 2

Akter Hossain
Akter Hossain

Reputation: 91

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

Upvotes: 8

Jakub Zalas
Jakub Zalas

Reputation: 36191

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

Upvotes: 8

Ivar
Ivar

Reputation: 4901

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'

Upvotes: 198

david
david

Reputation: 1057

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

Upvotes: 86

Mohamed Omar
Mohamed Omar

Reputation: 178

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

Upvotes: 8

Rafael
Rafael

Reputation: 2676

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.

Upvotes: 17

radistao
radistao

Reputation: 15504

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

Upvotes: 4

Yona Appletree
Yona Appletree

Reputation: 9142

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.

Upvotes: 15

Ivar
Ivar

Reputation: 4901

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

Upvotes: 4

Related Questions