10

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.

Maurice Müller
  • 1,312
  • 3
  • 16
  • 32

3 Answers3

21

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'
Helder Pereira
  • 5,522
  • 2
  • 35
  • 52
11

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.

Maurice Müller
  • 1,312
  • 3
  • 16
  • 32
2

Running by Gradle:

  • Run in default port(8080): ./gradlew bootRun
  • Run in provided port(8888): ./gradlew bootRun --args='--server.port=8888'
  • If we have any variable in the application.properties file named PORT, run this: PORT=8888 ./gradlew bootRun

Running by Maven:

  • Run in default port(8080): mvnw spring-boot:run
  • Run in provided port(8888): mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
  • Run in provided port(8888): mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'
  • Run in provided port(8888) with other custom property: mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
  • If we have any variable in the application.properties file named PORT, run this: SERVER_PORT=9093 mvn spring-boot:run

Using java -jar:

  • Create the .jar file:
    • For Gradle: ./gradlew clean build. We will find the jar file inside: build/libs/ folder.
    • For Maven: mvn clean install. We will find the jar file inside:target folder.
  • Run in default port(8080): java -jar myApplication. jar
  • Run in provided port(8888): java -jar myApplication.jar --port=8888
  • Run in provided port(8888): java -jar -Dserver.port=8888 myApplication.jar
  • Run in provided port(8888) having variable SERVER_PORT in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar
Md. Shahariar Hossen
  • 1,367
  • 10
  • 11