【问题标题】:Spring boot + gradle pass comand line args to application.propertySpring boot + gradle 将命令行参数传递给 application.property
【发布时间】:2018-05-27 03:48:28
【问题描述】:

我将 Spring Boot 与 Gradle 一起使用,我在 Intelij 中执行此操作:

10:43:37: Executing external task 'bootRun  -Drun.arguments="--
server.port=6666"'...

但我仍然在日志中看到:

Tomcat 使用端口初始化:8080 (http)

我见过这个,但它对我不起作用:https://stackoverflow.com/a/37053004/3871754

【问题讨论】:

    标签: java spring spring-mvc spring-boot gradle


    【解决方案1】:

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

    bootRun --args='--server.port=6666'
    

    注意:单引号很重要

    【讨论】:

    • bootRun --args='--server.port=123 --another.thing=abc'
    【解决方案2】:

    Spring Boot Gradle 插件仍然没有开箱即用地提供此功能。但即使您不想向 Gradle 脚本添加额外配置,也可以使用遵循 these rules 中的命名约定的环境变量来解决此问题(也适用于自定义属性)。

    例如,您可以使用以下命令更改端口:

    SERVER_PORT=6666 ./gradlew bootRun
    

    【讨论】:

      【解决方案3】:

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

      您今天的做法是将所有系统属性传递给线程中提到的 bootRun 任务,就像这样。

      bootRun {
          systemProperties = System.properties
      }
      

      那么你可以简单地使用:

      bootRun -Dserver.port=6666
      

      s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat 初始化为 端口:6666 (http)

      【讨论】:

      • 在项目中找不到参数 [build_2lc1lp789tqz1ov0hbgjhwn3q$_run_closure1@4f7ef951] 的 bootRun() 方法
      猜你喜欢
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多