When I'm replacing a task with `tasks.replace("nam...
# community-support
s
When I'm replacing a task with
tasks.replace("name")
, I can only do so it I maintain the task's type. However, in my case the task's constructor requires arguments. How can I specify these when replacing a task?
There only seems to be the possibility to pass constructor arguments when *register*ing a task, but not when replacing it:
Copy code
<T extends Task> TaskProvider<T> register(String name, Class<T> type, Object... constructorArgs) throws InvalidUserDataException;
So I'm getting
Copy code
* What went wrong:
Could not create task ':printVersion'.
> Could not create task of type 'PrintTask'.
   > Unable to determine constructor argument #1: missing parameter of type Function0, or no service of type Function0<?>.
v
Are you super super super super super super sure you really need to do that? As far as I remember it is a very discouraged thing to do and can generate quite some problems. Besides that it is not capable of task-configuration avoidance as it is eager API, it might make serious problems for example if you retrieved the original task and wired it in somewhere and then replace it. And iirc under some circumstances it will also fail. I might misremember though, never used that.
And no, I think it anyway only works for tasks that do not need explicit arguments but only with no-arg constructor or only automatically injected services.
s
Yes, I'm sure in this case. It's a final report task that just prints some information. Anyway, I'm onto something, maybe I have a solution soon.
v
In almost all cases it is better to simply adjust the properties of the existing task. 🙂 But if you really need to do it, I guess you can use
remove
or
removeIf
to remove it and then just register a fresh instance.