如何在分叉进程上使用 gradle 任务启动 spring 引导应用程序?

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

我有一个 spring 启动应用程序,它有一个虚拟端点,我想在其上执行 gatling 负载测试。

gradle 任务:

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()
    ]
}

虚拟休息端点:

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

我想在 gradle 中创建另一个任务,它将启动 spring 启动应用程序。

testLoad.dependsOn startSpringBoot

所以,问题是我不知道如何在 gradle 任务中启动 spring 启动应用程序:

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

然后用另一个任务关闭 spring 引导应用程序:

testLoad.finalizedBy stopSpringBoot


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

由于您使用的是 Gradle,因此您可以使用 GradleBuild 任务而不是 JavaExec 任务,如下所示:

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

我发现一个教程确实向您展示了如何做到这一点: http://brokenrhythm.blog/gradle-gatling-springboot-automation#Gradle

classpath 'com.github.jengelman.gradle.plugins:gradle-processes:0.3.0'
apply plugin: 'com.github.johnrengelman.processes'

此插件为您提供对任务执行的分叉进程的支持。

task startSpringBoot(type: JavaFork) {
    description = 'Start Spring Boot in the background.'
    group = 'Load Test'
    classpath = sourceSets.main.runtimeClasspath
    main = 'webservice.Application'   
}

但最后我选择了不同的方法,没有部署 spring 启动应用程序,但仍然调用 java 代码来完成我的工作。