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

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

Related

gradle: Task :jar SKIPPED while I get my jar with gradlew build

My Question is: Why does the jar-creation work with gradlew build, while I see "Task :jar SKIPPED" when I click on jar in intellij's gradle window ? And how can I fix it in IntelliJ ?
Just created something with spring initializer and loaded the project in intellij as it is.
( it is org.springframework.boot, .. 'org.springframework.boot:spring-boot-starter-web')
I wonder about Task :jar SKIPPED ( nor jar created )
and than I discovered that I get the jar when I start from console.
( and the jar runs fine, it finds the main class - even without jar manifest attribute in build.gradle)
( yesterday I failed in maven with "no main manifest attribute in .... .jar )
This is because Springboot Gradle plugin will create a bootJar task and by default will disable jar and war tasks, as described here: https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#packaging-executable-and-normal
So you need to execute bootJar task , from the IDE. When executing gradlew build, the tasks bootJar gets automatically executed, due to tasks dependencies created by the plugin.
When running task build (from console or IDE), you can see the tasks executed by Gradle depending on tasks dependencies, e.g.:
> Task :backend:compileJava
> Task :backend:processResources
> Task :backend:classes
> Task :backend:bootJar ## <== this is the task register by Springboot plugin, which produces the "Fat/executable" jar
> Task :backend:jar SKIPPED ## <== task disabled by Springboot plugin
> Task :backend:assemble
> Task :backend:processTestResources
> Task :backend:testClasses
> Task :backend:test
> Task :backend:check
> Task :backend:build
For your remark
the jar runs fine, it finds the main class - even without jar manifest
attribute in build.gradle
The Springboot plugin will automatically configure this for you, see : https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#packaging-executable-configuring-main-class
EDIT 27-05-2021
Starting from Springboot 2.5, the jaris not disabled by default anymore. see more details in release notes here
You can enble this, with add code below in projectName.gradle
it works for me :
spring-boot : 2.0.8.RELEASE
Gradle : 4.5 or >
jar {
baseName = 'projectName'
enabled=true
manifest {
....
}
}

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 to change the port of a Spring Boot application using Gradle?

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

Liquibase changesets are not running in bootRepackage gradle task

I have created liquibase changesets, in the Spring boot application. If I execute bootRun task then changesets get executed however, if I run bootRepackage changesets are not executed.
What additional configuration I need to do in order to run changesets with bootRepackage.
I suggest that you add the task tree plugin to your script
plugins {
id "com.dorongold.task-tree" version "1.3"
}
And then run
gradle bootRun bootRepackage taskTree
You should then see which tasks are run by the bootRun task which are not run by the bootRepackage task.
You'd then likely add a task dependency in build.gradle
bootRepackage {
dependsOn someLiquibaseTask
}

Spring-boot and spring boot dev tools integration not showing the updated class changes

I am trying to follow this example to do spring boot and spring boot dev tools integration to do automatic restart. The classes in the build folder are getting updated when i run build --continuous task but the application still talks to the old classes. In the example the bootRun task is as below. My project has its custom task for running the application. Right now with build -continuous when I make a change the application it is rebuilding the classes but the running application is not showing the changes. How to change my custom h2Run task so that it loads the changed classes? Thank you.
The boot run task in the example
bootRun {
classpath = sourceSets.main.runtimeClasspath + configurations.dev
}
My custom task for bootRun
class Run extends JavaExec {
Run() {
group "application"
dependsOn project.tasks.classes, project.tasks.pathingJar
classpath = project.files("$project.buildDir/classes/main", "$project.buildDir/resources/main", project.tasks.pathingJar.archivePath)
main = "com.mycompany.Application"
}
}
task h2Run(type: Run) {
classpath = sourceSets.main.runtimeClasspath + configurations.dev // this is not working
description "Start $appName using H2 database"
args "--spring.profiles.active=dev"
mustRunAfter 'cleanH2'
dependsOn copyContentTypeLibraries
}
I walked through the DZone article you linked to. I didn't add your custom Run class or task, I just grabbed the bootRun task right out of the article. Even without any of your custom code, I initially experienced the same behavior you do.
The article states:
At the first terminal, start Gradle build as a continuous task:
gradle build --continuous
At the second terminal, start the Gradle bootRun task: gradle
bootRun
If I do these things, in this order, I also see my classes recompile, but the servlet container doesn't pick the changes up. Just like you describe.
However, if I do gradle bootRun first, and gradle build --continuous second, after the application is running, the application restarts as expected whenever I edit a java file.
Have you tried executing the commands in the two terminal windows in reverse order?

Resources