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 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.
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.
Running by Gradle:
./gradlew bootRun
./gradlew bootRun --args='--server.port=8888'
application.properties
file named PORT
, run this: PORT=8888 ./gradlew bootRun
Running by Maven:
mvnw spring-boot:run
mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
application.properties
file named PORT
, run this: SERVER_PORT=9093 mvn spring-boot:run
Using java -jar
:
./gradlew clean build
. We will find the jar file inside: build/libs/
folder.mvn clean install
. We will find the jar file inside:target
folder.java -jar myApplication. jar
java -jar myApplication.jar --port=8888
java -jar -Dserver.port=8888 myApplication.jar
SERVER_PORT
in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar