In Gradle build, start Spring boot application, run tests and then stop the Spring boot application - spring

With my Gradle build I want to start my Spring Boot application, run some tests and then shutdown the Spring boot application. First I try just to start the server and then shutdown, leaving out the tests for now.
Part of my build.gradle file looks like this
bootRun {
systemProperties = System.properties
}
task shutdown(type: org._10ne.gradle.rest.RestTask) {
httpMethod = 'post'
uri = 'http://localhost:8080/shutdown'
}
task integrationTest() {
dependsOn bootRun
finalizedBy shutdown
}
I start the build from the command line like this
gradle integrationTest -Dendpoints.shutdown.enabled=true
The build starts the Spring Boot application but it is not proceeding (of course) to the shutdown task. I can run the shutdown task separately and it will take down the Spring Boot app.
But, how can I get the server to run and shutdown in the same build?

I'm not sure it will work with a task like bootRun, but I think you're looking for something like a ParallelizableTask
Also you would have to pass the --parallel command line option.

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

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"

Inject Gradle properties into Spring Boot application.yml, not working in IntelliJ IDEA

I've managed to inject the Gradle proj.ver into application.yml and after that injected it into service application.
My application.yml looks like this:
project:
version: ${version}
But it works only if I started the app from cli with:
gradle bootRun
If I'm trying to start the app from IntelliJ, it didn't work and it failed with:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in value "${version}"
I read all the answers from Stackoverflow and they suggested two solutions:
Use spring profiles
Modify run configuration and run before launch the gradle task: processResources
I'd prefer something like a default value for proj.ver when I'm running from IntelliJ. Is that possible? Or are any better solutions for this situation ?
Thanks
As M. Deinum said above in the comment, I managed to run the app from IntelliJ, but now the gradle bootRun started to fail with:
Caused by: groovy.lang.MissingPropertyException: No such property: unknown for class: SimpleTemplateScript2
After some more research it seems that ${version?:unknown}(with the question mark) it works either from the IDE or from cli.
I've updated the response, in order for others to know how to inject Gradle build info into Spring-boot:
1) Tell Gradle to pass the build data towards a Spring yml file like this:
processResources {
filesMatching('appInfo.yml') {
expand(project.properties)
}}
2) The appInfo.yml will look like:
project:
version: ${version?:unknown}
3) Inject the version of the build into Spring services like:
#Value("${project.version}")
private String applicationVersion;
Just to complete for Kotlin user, what works for me was :
build.gradle.kts
tasks.processResources { filesMatching("**/application.properties") { expand(project.properties) } }
application.properties
project.version= ${version}
Service.kt
#Value("\${project.version}") lateinit var version: String

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?

Spring Boot not starting Jetty when using bootRun

In our current app we want to use Jetty instead of Tomcat in our Spring Boot app. As i read in the documentation excluding tomcat and introducing Jetty would be enough.
build.gradle
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
}
But when i try to run the app with using:
# gradle bootRun
Tomcat is indeed gone, but Jetty is not started and the application just terminates with any noticeable error. Any hints on what i am doing wrong?
Works well for me with Boot 1.1.0.RELEASE.
You need to run Gradle command with --info(--debug) hint to get more info what's going on.

Resources