Search code examples
spring-bootgradlegradlew

How can I run multiple profiles with Gradle + Spring boot?


I am trying to learn using gradle with springboot from maven so I wanted to know how I can run my project in command line to pick up a different configuration file. The scenario here is I have my properties eg.

application-qa.properties and application.properties

however If run gradle with something like ./gradlew -PspringProfile=qa bootRun

It loads only the default properties in application.properties and not the qa properties.

How do I accomplish that with gradle as with maven before?

This is what I have tried. I created gradle run config in IntelliJ but this has not also worked,

Is there a commad-line option?

Run Config in Intelij


Solution

  • First of all, as another user said in the comments to your question, you are not using the correct name of the parameter.

    But furthermore, you can't set the active profile with a project property through -P (at least not out of the box). You have a few other options.

    A

    Use --spring.profiles.active=qa as an argument like this:

    gradlew bootRun --args='--spring.profiles.active=qa'

    I don't think you can put that type of argument into the "arguments" list in the IntelliJ run configuration though, but you can put it directly into the "tasks" value instead if you like. But that's a bit weird.

    B

    Default to a particular environment in the Gradle build file when run though Gradle:

    bootRun {
        environment('spring.profiles.active', 'qa')
    }
    

    This makes sense if you are always running against the same environment when using the bootRun task from Gradle.

    C

    Set an environment variable spring.profiles.active=qa, either on your local computer or in the IntelliJ run configuration.