How to run bootRun with spring profile via gradle task

前端 未结 13 1410
误落风尘
误落风尘 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)
        }
    }
    

提交回复
热议问题