javaspring-bootgradlegroovygradle-task

How to start spring boot application with a gradle task on a forked process?


I have a spring boot application with a dummy endpoint on which I wanna execute a gatling load test.

gradle task:

task testLoad(type: JavaExec) {
    description = 'Test load the Spring Boot web service with Gatling'
    group = 'Load Test'
    classpath = sourceSets.test.runtimeClasspath
    jvmArgs = [
            "-Dgatling.core.directory.binaries=${sourceSets.test.output.classesDir.toString()}",
            "-Dlogback.configurationFile=${logbackGatlingConfig()}"
    ]
    main = 'io.gatling.app.Gatling'
    args = [
            '--simulation', 'webservice.gatling.simulation.WebServiceCallSimulation',
            '--results-folder', "${buildDir}/gatling-results",
            '--binaries-folder', sourceSets.test.output.classesDir.toString()
    ]
}

dummy rest endpoint:

@RestController
public class GreetingController {
    @RequestMapping("/greeting")
    public Greeting greeting() {
        return new Greeting("Hello, World");
    }
}

and I want to create another task in gradle which will start the spring boot application.

testLoad.dependsOn startSpringBoot

So, the problem is that I don't know how to start the spring boot application inside the gradle task:

task startSpringBoot(type: JavaExec) {
    // somehow start main = 'webservice.Application'
}

and then close the spring boot application with another task:

testLoad.finalizedBy stopSpringBoot


task stopSpringBoot(type: JavaExec) {
    // somehow stop main = 'webservice.Application'
}

Solution

  • Since you are using Gradle you can use a GradleBuild task instead of a JavaExec task, like this:

    task startSpringBoot(type: GradleBuild) {
      tasks = ['bootRun']
    }