Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the port of a Spring Boot application using Gradle?

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.

like image 725
Maurice Müller Avatar asked Nov 09 '17 09:11

Maurice Müller


People also ask

How do I run a spring boot program on a different port?

The fastest and easiest way to customize Spring Boot is by overriding the values of the default properties. For the server port, the property we want to change is server. port. By default, the embedded server starts on port 8080.

Can we change port number in spring boot?

Method 1: By Adding the configuration in the application properties of the Spring Boot project. We need to change the port number using the application. properties file in the project structure of the spring application.


2 Answers

In case you don't want to add extra configuration to your Gradle scripts, you can achieve it by setting the SERVER_PORT environment variable:

SERVER_PORT=8888 ./gradlew bootRun

[UPDATE] Since Gradle 4.9, it's possible to pass arguments to bootRun without extra configuration:

./gradlew bootRun --args='--server.port=8888'
like image 109
Helder Pereira Avatar answered Oct 12 '22 01:10

Helder Pereira


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]"]
}
  • obviously, replace [PORT] with the actual port number

OR 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.

like image 29
Maurice Müller Avatar answered Oct 11 '22 23:10

Maurice Müller