How to run bootRun with spring profile via gradle task

前端 未结 13 1372
误落风尘
误落风尘 2020-12-04 19:20

I\'m trying to set up gradle to launch the bootRun process with various spring profiles enabled.

My current bootRun configuration looks lik

相关标签:
13条回答
  • 2020-12-04 19:44

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-04 19:49

    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")
    }
    
    0 讨论(0)
  • 2020-12-04 19:50

    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

    0 讨论(0)
  • 2020-12-04 19:50

    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.

    0 讨论(0)
  • 2020-12-04 19:52

    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
    
    0 讨论(0)
  • 2020-12-04 19:53

    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
    
    0 讨论(0)
提交回复
热议问题