How to run bootRun with spring profile via gradle task

前端 未结 13 1391
误落风尘
误落风尘 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: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

提交回复
热议问题