8

I essentially want to create a task in gradle that executes the command

gradle bootRun -Dspring.profiles.active=test

This command does exactly what I want it to do if executed from the command line but I have had no luck trying to use type:Exec on a task and also no luck passing in System properties

I don't really want to make this into an external command that the user needs to know about to run. I would like it to show up under tasks/other.

My closest attempt so far:

task bootRunTest() {
    executable "gradle"
    args "-Dspring.profiles.active=test bootRun"
 }
Community
  • 1
  • 1
dszopa
  • 315
  • 4
  • 11

3 Answers3

5

The task I was trying to create wound up being this:

task bootRunTest(type: org.springframework.boot.gradle.run.BootRunTask, dependsOn: 'build') {
    group = 'Application'
    doFirst() {
        main = project.mainClassName
        classpath = sourceSets.main.runtimeClasspath
        systemProperty 'spring.profiles.active', 'test'
    }
}
dszopa
  • 315
  • 4
  • 11
2

Here is how you set the properties for the task you wish to run, in this case bootRun

add inside of Build.gradle

bootRun {
        systemProperty "spring.profiles.active", "test,qa,ect"
}

Then from the command line

gradle bootRun
Zergleb
  • 2,212
  • 15
  • 24
  • Shouldn't it be `systemProperty "spring.profiles.active", "test"` not `"test,qa,ect"`? – Zach Jan 16 '21 at 22:06
2

You can also do it by setting the OS variable, SPRING_PROFILES_ACTIVE, to the specific profile.

For eg:

SPRING_PROFILES_ACTIVE=dev gradle clean bootRun
Rothin Sen
  • 444
  • 4
  • 8