Spring Boot + Gradle将comand行args传递给application.property

Kam*_*icz 1 java spring spring-mvc gradle spring-boot

我将Spring Boot与Gradle结合使用,在Intelij中执行以下操作:

10:43:37: Executing external task 'bootRun  -Drun.arguments="--
server.port=6666"'...
Run Code Online (Sandbox Code Playgroud)

但仍然在日志中,我看到:

Tomcat已初始化端口:8080(http)

我已经看到了,但是对我不起作用:https : //stackoverflow.com/a/37053004/3871754

dds*_*tan 7

以下语法对我有用,无需对代码进行任何更改:

bootRun --args='--server.port=6666'
Run Code Online (Sandbox Code Playgroud)

注意:单引号很重要

  • bootRun --args='--server.port=123 --another.thing=abc' (3认同)

Kam*_*icz 5

在Spring Gradle BootRun任务中使用命令行参数并不容易。这是本Spring Boot Issue Thread中有关该主题的非常有趣的讨论。

今天,您可以像这样在线程中提到的那样,通过将所有系统属性传递给bootRun任务来做到这一点。

bootRun {
    systemProperties = System.properties
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以简单地使用:

bootRun -Dserver.port=6666
Run Code Online (Sandbox Code Playgroud)

sbcetTomcatEmbeddedServletContainer:使用端口初始化的Tomcat:6666(http)