복붙노트

[SPRING] gradle 작업을 통해 spring 프로필로 bootRun을 실행하는 방법

SPRING

gradle 작업을 통해 spring 프로필로 bootRun을 실행하는 방법

다양한 spring profile이 활성화 된 bootRun 프로세스를 시작하기 위해 gradle을 설정하려고합니다.

현재 bootRun 구성은 다음과 같습니다.

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']
    }
}

난 gradle 작업과 시스템 속성을 설정하고 bootRun을 실행하고 싶습니다.

내 시도는 다음과 같이 보였다.

task bootRunDev

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

몇 가지 질문 :

- 에릭

해결법

  1. ==============================

    1.가장 간단한 방법은 기본값을 정의하고 재정의되도록 허용하는 것입니다. 이 경우 systemProperty의 사용법을 잘 모릅니다. 간단한 논쟁이 그 일을 할 것입니다.

    가장 간단한 방법은 기본값을 정의하고 재정의되도록 허용하는 것입니다. 이 경우 systemProperty의 사용법을 잘 모릅니다. 간단한 논쟁이 그 일을 할 것입니다.

    def profiles = 'prod'
    
    bootRun {
      args = ["--spring.profiles.active=" + profiles]
    }
    

    dev를 실행하려면 :

    ./gradlew bootRun -Pdev
    

    태스크에 의존성을 추가하려면 다음과 같이 할 수 있습니다.

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

    Gradle에서이 작업을 수행하는 데는 여러 가지 방법이 있습니다.

    편집하다:

    환경별로 별도의 구성 파일을 구성하십시오.

    if (project.hasProperty('prod')) {
      apply from: 'gradle/profile_prod.gradle'
    } else {
      apply from: 'gradle/profile_dev.gradle'
    }
    

    각 구성은 다음과 같은 작업을 무시할 수 있습니다.

    def profiles = 'prod'
    bootRun {
      systemProperty "spring.profiles.active", activeProfile
    }
    

    이 경우 prod 플래그를 제공하면 다음과 같이 실행됩니다.

    ./gradlew <task> -Pprod
    
  2. ==============================

    2.환경 변수는 문서에 설명 된대로 스프링 속성을 설정하는 데 사용할 수 있습니다. 따라서 활성 프로파일 (spring.profiles.active)을 설정하려면 Unix 시스템에서 다음 코드를 사용할 수 있습니다.

    환경 변수는 문서에 설명 된대로 스프링 속성을 설정하는 데 사용할 수 있습니다. 따라서 활성 프로파일 (spring.profiles.active)을 설정하려면 Unix 시스템에서 다음 코드를 사용할 수 있습니다.

    SPRING_PROFILES_ACTIVE=test gradle clean bootRun
    

    그리고 창문에서는 다음을 사용할 수 있습니다 :

    SET SPRING_PROFILES_ACTIVE=test
    gradle clean bootRun
    
  3. ==============================

    3.Spring Boot 2.0 이상을 사용하는 사용자는 다음을 사용하여 주어진 프로필 세트로 앱을 실행할 작업을 설정할 수 있습니다.

    Spring Boot 2.0 이상을 사용하는 사용자는 다음을 사용하여 주어진 프로필 세트로 앱을 실행할 작업을 설정할 수 있습니다.

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

    그런 다음 IDE에서 ./gradlew bootRunDev 또는 유사한 도구를 간단하게 실행할 수 있습니다.

  4. ==============================

    4.인터넷에서 온 사람의 경우 비슷한 질문이있었습니다. https://stackoverflow.com/a/35848666/906265 수정 된 답변도 여기에서 제공합니다.

    인터넷에서 온 사람의 경우 비슷한 질문이있었습니다. https://stackoverflow.com/a/35848666/906265 수정 된 답변도 여기에서 제공합니다.

    // 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"]
                }
            }
        }
    }
    
    <...>
    

    다음 터미널 :

    gradle runDev
    

    gradle 3.4.1과 spring boot 1.5.10을 사용하십시오.

  5. ==============================

    5.서로 다른 프로필 및 작업을 그라디언트하는 4 가지 작업에 대한 구성 :

    서로 다른 프로필 및 작업을 그라디언트하는 4 가지 작업에 대한 구성 :

    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)
        }
    }
    
  6. ==============================

    6.이 셸 명령을 사용하면 다음과 같이 작동합니다.

    이 셸 명령을 사용하면 다음과 같이 작동합니다.

    SPRING_PROFILES_ACTIVE=test gradle clean bootRun
    

    슬프게도 이것은 내가 찾은 가장 간단한 방법입니다. 해당 호출에 대한 환경 속성을 설정 한 다음 응용 프로그램을 실행합니다.

  7. ==============================

    7.VM 옵션에 추가 : -Dspring.profiles.active = dev

    VM 옵션에 추가 : -Dspring.profiles.active = dev

  8. from https://stackoverflow.com/questions/36923288/how-to-run-bootrun-with-spring-profile-via-gradle-task by cc-by-sa and MIT license