How to change the port of a Spring Boot application using Gradle?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Changing a Spring Boot port is mostly a Spring configuration task, but Gradle can be used to pass or enforce that configuration when you run the application. The right approach depends on whether you want a default port in source control or a temporary override for local development.
Set the Default Port in Spring Configuration
The most common solution is still the Spring Boot config file. This is the best choice when the application should normally start on the same non-default port.
If your project uses YAML:
This works whether you start the app from Gradle, from your IDE, or from a packaged JAR. The port is part of the application configuration, so it stays consistent across run methods.
Override the Port When Running With Gradle
If you only want to change the port for one local run, pass it as a command-line argument through bootRun.
This is convenient for testing two services side by side or avoiding a port conflict without editing committed config files.
You can do the same with an environment variable:
Spring Boot maps SERVER_PORT to server.port, so the application picks it up automatically.
Configure bootRun in build.gradle
If your team wants Gradle to always start the app on a specific port during local development, configure the bootRun task directly.
Now every ./gradlew bootRun invocation uses port 9095 unless a higher-precedence setting overrides it.
This is useful for local conventions, but remember what it does and does not affect:
- It changes
bootRun - It does not rewrite
application.properties - It does not automatically change how a packaged JAR runs outside Gradle
Understand Property Precedence
When several sources provide the same setting, Spring Boot uses a precedence order. In practical terms, command-line arguments usually win over environment variables, which usually win over application config files.
So if you have:
- '
server.port=9090inapplication.properties' - '
SERVER_PORT=9092in the shell' - '
--server.port=9093inbootRun --args'
the application normally starts on 9093.
Knowing that order makes troubleshooting much easier when the server starts on a different port than you expected.
Use Profile-Specific Ports Carefully
You can also put the port in a profile-specific file such as application-dev.properties:
Then run:
This is a good choice when different environments need different ports and you want that behavior to be explicit rather than hidden inside a build script.
Verify the Running Port
After startup, check the application logs for the embedded server message. Spring Boot will usually report the actual port it bound to, which is helpful when multiple configuration sources are competing.
Common Pitfalls
- Gradle is not the only place to change the port, and often it is not the best permanent place either.
- Setting the port in
bootRunaffects Gradle runs, but packaged deployments may still use a different value. - Command-line arguments and environment variables can override config files, which surprises people during debugging.
- Hard-coding a port in
build.gradlecan confuse teammates if the project already uses profile-based configuration.
Summary
- Use
application.propertiesorapplication.ymlfor the normal default port. - Use
./gradlew bootRun --args='--server.port=...'for temporary overrides. - Use
SERVER_PORT=... ./gradlew bootRunwhen environment-based config fits your workflow. - Configure the
bootRuntask only when you deliberately want Gradle-specific startup behavior.

