2

I am having an application, which is running on some port(ex-8080) now when I start this application using gradlew I want to pass dynamic port to start the application?

./gradlew :testApplication:bootRun

is there anyway to pass the dynamic port here??

Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
Ashwini Jha
  • 757
  • 1
  • 8
  • 16
  • Possible duplicate of [How to change the port of a Spring Boot application using Gradle?](https://stackoverflow.com/questions/47198100/how-to-change-the-port-of-a-spring-boot-application-using-gradle) – Helder Pereira Dec 12 '20 at 13:37

2 Answers2

4

Add the following to build.gradle so that we can pass parameters to gradlew along to the underlying java command:

bootRun {
    if (project.hasProperty('args')) {
        args project.args.split(',')
    }
}

Pass the arguments you would normally send to a java command (in this case, overriding the server.port) as -Pargs to gradlew:

/gradlew :testApplication:bootRun -Pargs="--server.port=8081"

What is here:

  • When you run java with arguments --server.port=8081, Spring Boot will override default property (e.g. Spring Boot will ignore your port in properties file, it will use value from command line
  • -Pargs is the way to ask bootRun to command line arguments. See details here.

See also the same question for maven.

Chris Trevarthen
  • 5,221
  • 2
  • 16
  • 10
Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
  • 2
    Thanks for the info, -Pargs is not considering, I added the below things and it started working. bootRun { systemProperties = System.properties } and then passed -Dserver.port=8081 and It started working. I have another question , Can I able to modify the custom value specified in application.properties by passing the argument in gradlew bootRun? example in application.properties there is a variable call test.url="http:localhost:8081"now I am running the gradlew :testApplication:bootRun and here I wanna modify the content of test.url, is this possible? – Ashwini Jha Nov 17 '18 at 16:19
1

I couldn't pass the port directly.

But if you want a workaround, do the following:

  • Build the application with gradle build.
  • Navigate in your project and open the directory build/libs
  • Now you have to see the jar of your project and then run this command java -jar yourJarProject.jar --server.port=8081.
Guilherme Alencar
  • 1,243
  • 12
  • 21