Understanding task realization and lazy configuration

Hello,

The following code works but I don’t know if it is correct in terms of lazy configuration. There is a call to producerProvider.get() so I am not sure this is proper lazy configuration as we’re not supposed to realize tasks during configuration.

class MyPlugin implements Plugin<Project> {
    @Override
    public void apply(Project project) {
        TaskProvider<Producer> producerProvider = project.getTasks().register("produce", Producer.class)
        producerProvider.configure (provider -> {
            provider.getOutputFile().set(project.getLayout().getBuildDirectory().file("file.txt"));
        });

        TaskProvider<Consumer> consumerProvider = project.getTasks().register("consume",  consumer.class)
        consumerProvider.configure(provider -> {
            // Next line calls get() on the task provider
            provider.getInputFile().set(producerProvider.get().getOutputFile());
        });
    }
}

As you correctly said, using get() at configuration time is bad, as it eagerly realizes in this case, or in other cases would add the “same” race conditions you get from using afterEvaluate.

provider.getInputFile().set(producerProvider.flatMap(Producer::getOutputFile)); is most probably what you intended to do. :slight_smile:

1 Like