I need some help restoring a little bit of sanity ...
# plugin-development
k
I need some help restoring a little bit of sanity in some borked Gradle code. Suppose I have this little thing here:
Copy code
abstract class MyTask : DefaultTask() {
  init {
    val task = project.tasks.register("myTask-$name-derived") {
    }
    finalizedBy(task)
  }
}
What would be a sane redesign so that you don't have this?
e
What is it exactly you are trying to do?
k
Not register a task inside the init of another, for one. It seems like every
MyTask
that is registered necessitates a related
derived
task.
e
So you are trying to always register an additional "derived task" any time a task type of
MyTask
is registered?
k
Yes. Further complicating matters is that this "derived task" takes some of its property values from those of
MyTask
, so there are some weird "input to input" and "output to output" wirings in place.
(The intent is that they have the same value, at least)
e
What you want to do is create an extension with a
NamedDomainObjectContainer
property that, when something is registered, will create the two tasks and wire the inputs / outputs appropriately. You should find this section of the docs helpful: https://docs.gradle.org/current/userguide/custom_gradle_types.html#nameddomainobjectcontainer