如何使用Gradle从Spring bootRun传递扩展JVM选项?

时间:2018-05-31 17:08:28

标签: java spring-boot gradle

我正在尝试在build.gradle中启用不同的运行配置文件。一个是“正常”运行条件,另一个是局部开发。本地开发应激活我的“本地”弹簧配置文件,并监听特定端口的调试。这是我目前在build.gradle中所拥有的:

    task localBootRun(dependsOn: bootRun) {
    bootRun {
        args = ["--spring.profiles.active=local"]
    }
}

当我从gradle调用localBootRun任务时,这是有效的:

./gradlew localBootRun

我无法弄清楚如何传递监听调试器所需的扩展JVM参数。通常,我会在JVM命令行上传递这些:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8005

但我无法弄清楚如何告诉我的localBootRun任务将这些作为参数传递给JVM。

1 个答案:

答案 0 :(得分:0)

在这种特殊情况下,这对我有用:

bootRun {
    args = ["--spring.profiles.active=local"]
    jvmArgs = ["-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8005"]
}

然后,我正常调用该任务:

./gradlew bootRun

似乎工作得很棒!

如果你想要一个不同的或默认的bootRun任务,还有一些工作:

def profileVal = project.hasProperty('profile') ? project.property('profile') : 'default'
bootRun {
    args = ["--spring.profiles.active=${profileVal}"]

    if (profileVal == "local") {
        jvmArgs = ["-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8005"]
    }

然后你可以正常调用它:

./gradlew bootRun

对于“默认”运行或使用配置文件调用它:

./gradlew -Pprofile=local bootRun

对于带有debug的“本地”配置文件。

可能有更好或更不同的方法,但这对我有用。