I’ve a Spring Boot project, which includes a default Test task. I need to create a “clone” of this task which should behave exactly as the default task but sets an additional system property. As it seems there is no way to really clone a task in gradle, I’ve defined my custom task as shown below, but this has the disadvantage, that my custom test task doesn’t support the same command line arguments as the default Test task. Is there any way in Gradle, where I can define a “clone” of a task which behaves the same as the original task including command line args and has some additional actions? I really cannot believe, there is no easy way doing such a common thing?
tasks.register('testPerformance') {
doLast {
systemProperty('test.include.performance', true)
}
finalizedBy test
}
There is no way, and this is not at all a common thing.
Also the approach you showed cannot work at all.
You register a task of type Task and try to call systemProperty on it at execution time.
But Task does not have such a method, so it will fail when you try to execute it.
And even if it would work, you should never try to change the configuration of a task during its execution time.
And also even if it would work, you would only set the system property for the testPerformance task but not at all influence the test task.
I can highly recommend using the Kotlin DSL instead of the Groovy DSL, because then you instantly get type-safe build scripts, much better error messages if you mess up syntax, and amazingly better IDE support if you use a proper IDE like IntelliJ IDEA. You would much earlier get a compilation error already when trying to execute your build and not just at runtime when you try to execute that task.
If you want only to run either-or, just configure the existing task accordingly depending on some condition, for example in Kotlin DSL similar to
and then you can do ./gradlew test -P testPerformance=true.
If you want separate tasks, just register another task of type Test that you configure however you need it.
Alternatively, you could also use the JVM Test Suite Plugin to create a separate test suite for performance tests and have the performance tests in that suite, then you also have a clearer separation between unit tests and performance tests.