How to run bootRun with spring profile via gradle task

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

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

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

    tasks.named<BootRun>("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.

    0 讨论(0)
  • 2020-12-04 20:00

    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

    0 讨论(0)
  • 2020-12-04 20:03

    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.

    0 讨论(0)
  • 2020-12-04 20:11

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

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

    ./gradlew <task> -Pprod
    
    0 讨论(0)
  • 2020-12-04 20:11

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