【问题标题】:How to change the port of a Spring Boot application using Gradle?如何使用 Gradle 更改 Spring Boot 应用程序的端口?
【发布时间】:2018-04-22 05:25:18
【问题描述】:

简单的问题是:如何使用 gradle 更改 Spring Boot 应用程序端口?


Here 如果您不使用 gradle,已经列出了很多正确答案。所以对于没有 gradle 的问题,请参考这篇文章。

【问题讨论】:

    标签: spring-boot gradle port


    【解决方案1】:

    如果您还没有使用Spring Boot Gradle Plugin,请将其添加到您的构建脚本中(当然,根据您的需要调整 Spring Boot 版本):

    buildscript{
        ext { springBootVersion = '1.5.7.RELEASE' }
        dependencies {
              classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }
    apply plugin: 'org.springframework.boot'
    

    使用此插件,您可以执行以下操作:

    bootRun {
        args += ["--server.port=[PORT]"]
    }
    
    • 显然,将 [PORT] 替换为实际端口号

    或者更动态您可以使用项目属性来更改端口。你必须做类似这样的事情:

    if(!project.hasProperty("port"))
        project.ext.set("port", 8080)
    
    bootRun {
        args += ["--server.port=${project.port}"]
    }
    

    然后就可以用

    启动应用了

    ./gradlew bootRun -Pport=8888

    如果您在本例中跳过 -Pport,它将使用 8080。

    【讨论】:

    • 嗯,我发现这不起作用,因为我得到:无法从类型 [java.lang.String] 转换为类型 [java.lang.Integer] 的值 '[port]'
    • 您可能没有将 [PORT] 替换为实际的端口号。
    • 笨蛋我!谢谢!
    • 我可以用这个来generate client code吗?
    【解决方案2】:

    如果您不想在 Gradle 脚本中添加额外的配置,可以通过设置 SERVER_PORT 环境变量来实现:

    SERVER_PORT=8888 ./gradlew bootRun
    

    [更新]自 Gradle 4.9 起,无需额外配置即可pass arguments to bootRun

    ./gradlew bootRun --args='--server.port=8888'
    

    【讨论】:

      猜你喜欢
      • 2015-07-25
      • 1970-01-01
      • 2017-11-21
      • 2017-03-07
      • 2014-01-31
      • 2016-09-09
      相关资源
      最近更新 更多