Extend existing Gradle task and override configuration
-
21-12-2019 - |
Question
I'd like to define a new task called dbStatus
that calls (or extends?) run
, and just overrides the args
property.
apply plugin: 'application'
run {
args "server", "service.yml"
}
task(dbStatus, type: run) {
args "db", "status", "service.yml
}
This doesn't work because "run"
isn't a valid task class. Is there a quick way to extend a task and just override a property?
UPDATE: Resolution
Unfortunately I had to just define a brand new JavaExec
task, and recreate the logic that run
is configured to do. Here is what I came up with:
task(dbStatus, type: JavaExec) {
main mainClassName
classpath sourceSets.main.runtimeClasspath
args "db", "status", "service.yml"
}
I don't think this is exactly the same as run
, since it isn't running against the build jar
I don't believe, but it works for my purposes.
Solution
Tasks cannot be "extended" in this way. Instead, declare another task and configure it as appropriate. (It's common to configure multiple tasks at once to avoid code duplication.)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow