How to change the port of a Spring Boot application using Gradle? - spring-boot

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]"]
}
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.

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

Related

How do I get the active profile in a build.gradle task for a Spring Boot Project?

No matter what I do, I cannot get the active profile in a build.gradle task when I try:
println property('spring.profiles.active')
I'm running the project by running:
./gradlew bootRun --args='--spring.profiles.active=dev'
I want to be able to distinguish between dev and prod in build.gradle

Build Gradle JAR without dependencies

From command line, I need to build an executable jar without dependencies.
The current "gradle build" command gives me a jar with dependencies.
Couldn't find this on StackOverflow. If it's a duplicate question, point me there. Thanks.
As you have SpringBoot plugin enabled, the default behavior is to build an executable jar (fat-jar) containing all dependencies, through thebootJar task.
You can still generate a single "standard" jar if you need, this is explained in the documentation : Spring Boot Gradle plugin
jar {
enabled = true
}
bootJar {
classifier = 'boot'
}

How to: Gradle Build spring boot app with external config file

Maven way is very simple:
mvn clean install -Dspring.config.location=/path/config.properties
How can I do this with Gradle?
You should specify a task and add jvmArgs for running spring boot application in build.gradle file:
bootRun {
jvmArgs = ["-Dspring.config.location=/prop.properties"]
}
or run in command line:
gradle clean build bootRun -Drun.jvmArgs="-Dloader.config.location=/path/to/prop/file"

How do I run FlyWay clean with Spring Boot?

I'm using Spring Boot and FlyWay together. I added the FlyWay dependency to my Gradle build file like this:
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.postgresql:postgresql:9.4-1202-jdbc42")
compile("org.flywaydb:flyway-core")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
I also added a db/migrations folder with an initial migration file. The migration works as expected. But now I want to clean by using the gradle flywayClean task. However, when I run this, I get an error saying the task can't be found. Is there another way I'm supposed to do this with Spring Boot?
To run gradle flywayClean, you have to apply the plugin: 'org.flywaydb.flyway'
See http://flywaydb.org/getstarted/firststeps/gradle.html

Can I use Gradle to build a classpath string

I need to execute an external Java application as part of a Gradle build process. The external application needs the same JARs in its classpath as the application Gradle is building. Is there any way to expose the dependencies I have defined in Gradle to the external application?
i.e. I need to run this:
java -jar -cp [jars from Gradle] myapp.jar
You could add a simple task to your build to get the resulting classpath.
task printClasspath << {
println configurations.runtime.asPath
}

Resources