如何使用Gradle更改Spring Boot应用程序的端口? [英] How to change the port of a Spring Boot application using Gradle?

查看:1379
本文介绍了如何使用Gradle更改Spring Boot应用程序的端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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






此处已经列出很多正确的答案,如果你不使用gradle。如果您还没有使用, https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.htmlrel =nofollow noreferrer> Spring Boot Gradle Plugin add它到您的构建脚本(当然,适应Spring Boot版本以满足您的需求):

  buildscript {
ext {springBootVersion ='1.5.7.RELEASE'}
依赖关系{
classpath(org.springframework.boot:spring-boot-gradle-plugin:$ {springBootVersion})
}

apply plugin:'org.springframework.boot'

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

  bootRun {
args + = [--server.port = [PORT] ]
}

要获得更多动态,您可以使用项目属性更改端口。你必须做类似的事情:

  if(!project.hasProperty(port))
project .ext.set(port,8080)

bootRun {
args + = [--server.port = $ {project.port}]
}

然后您可以使用 $ b


./ gradlew bootRun -Pport = 8888

如果在此示例中跳过了-Pport,它将使用8080。

The simple question is: How can you change the Spring Boot application port with gradle?


Here are already listed a lot of correct answers if you are not using gradle. So for none gradle issues, please refere to this post.

解决方案

If you're not already using the Spring Boot Gradle Plugin add it to your build script (of course, adapt the Spring Boot version to your needs):

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

With this plugin you can do the following:

bootRun {
    args += ["--server.port=[PORT]"]
}

For more dynamic you can use a project property to change the port. You have to do something similar like this:

if(!project.hasProperty("port"))
    project.ext.set("port", 8080)

bootRun {
    args += ["--server.port=${project.port}"]
}

Then you can start the application with

./gradlew bootRun -Pport=8888

If you skip the -Pport in this example it will use 8080.

这篇关于如何使用Gradle更改Spring Boot应用程序的端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆