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.
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.
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.
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'
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]"]
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With