我正在尝试在build.gradle中启用不同的运行配置文件。一种用于“正常”运行条件,另一种用于本地开发。本地开发人员应激活我的“本地” Spring 配置文件,并在特定端口上侦听调试。这是我到目前为止在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。
最佳答案
在这种特定情况下,这对我有用:
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
对于带有调试的“本地”配置文件。
可能有更好或不同的方法来执行此操作,但这对我有用。
关于java - 如何使用Gradle从Spring bootRun中传递扩展的JVM选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50629471/