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

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?

Related

How to add task dependency on all test tasks in gradle in multi module project

I have java gradle project with multiple modules and each module has its tests. How can I configure gradle to run some task (run docker compose up to give you some context, but that's not a part of the question) before first test task is run and some another task after last test has finished?
You can use both dependsOn and finalizedBy on all test tasks from the root project. If all tasks share a common base class (Test in this example), you can use withType:
subprojects*.tasks.withType(Test)*.configure {
dependsOn myPreparationTask
finalizedBy myCleanUpTask
}
If you need to configure tasks of different types, use the matching method instead with a custom condition.

Specifying xmlpathinjar TestNG option with Gradle test task

Is it possible to run a TestNG test suite that is embedded in a JAR file via a Gradle test task?
My project includes JARed bundles of TestNG tests that have an embedded testng.xml file defining which tests should be run in the JAR. Is it possible for Gradle to refer to this embedded XML when running the TestNG tests?
From the command line I use the xmlpathinjar option.
I dont think it can be done using the Gradle TestNG task. I couldn't find any such support in the TestNGOptions
Instead of using
test{
useTestNG()
}
you could try going through this post on SO How can I tell Gradle to use my testng.xml file for Test Classes and Ordering? and maybe employ the approach detailed here https://stackoverflow.com/a/28868416
But when you are using a custom Gradle task to run your TestNG tests, please make sure that you add a reference to the ExitCodeListener
Here's a sample
task ('myTask', type: JavaExec) {
main = 'org.testng.TestNG'
classpath = sourceSets.main.runtimeClasspath + sourceSets.test.runtimeClasspath
args = ["-xmlpathinjar", "suites/mysuite.xml", "-listener", "org.testng.TestNG\$ExitCodeListener"]
}
More details on why the ExitCodeListener needs to be referred, can be found here

how can i pre-process resources in gradle springboot devtools?

I want to pre-compile sass into css.
addResources flag(true) does not solve this problem.
Spring devtools just load raw resources into build.
There are some gradle node plugin, gulp plugin.
When processResoureces task is called, bootRun task running on the server resets build directory and re-copy raw resources into build. So gulp, webpack, etc plugin can't do this...
Rene has given a solution in gradle blog
all those "internal" tasks are created within the
project.afterEvaluate closure which means that they can't be
referenced directly in the build.gradle. As a workaround you can move
the dependsOn declaration in the afterEvaluate phase too.
project.afterEvaluate{
prepareDebugDependencies.dependsOn("generateMyAssets")
}
Resource Link: How to define a preprocessing task for android build?
UPDATE#1:
Q1: Why bootRun task re-copy resources?
If devtools has been added to your project it will automatically
monitor your application for changes. Alternatively, you can also run
the application so that your static classpath resources (i.e. in
src/main/resources by default) are reloadable in the live application,
which can be helpful at development time.
bootRun {
addResources = true
}
Making static classpath resources reloadable means that bootRun does
not use the output of the processResources task, i.e., when invoked
using bootRun, your application will use the resources in their
unprocessed form.

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

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.

gradle bootRun > Use test classpath

The problem I was having, was that I wanted to include test classpath resources in SpringBoot's bootRun gradle task. Why? So that I could use a test profile with test resources, to mock integration points.
What I tried:
The spring boot documentation only offers the addResources = true option (I tried using customConfiguration as per the similar bootRepackage configuration, to no avail)
No additional options are visible by looking at the BootRunTask source code
The equivalent maven plugin has a plethora of options, including useTestClasspath (which isn't mirrored in the gradle version)
I came across the following solution, which solved this issue for me.
Basically, the BootRunTask extends the standard JavaExec task, which offers a classpath option. So, you can add the test classpath resources by using the following gradle configuration:
bootRun {
classpath = sourceSets.test.runtimeClasspath
}

Resources