I’m trying to write a custom (java) plugin that will extract files for processing by other tasks that are coming from a custom configuration. In this case, they are filled with avro protocol files.
from build.gradle:
Calling Configuration.getAll() is probably not what you want since it will return all of the configurations in the project.
The Action passed to doLast is executed during the Gradle task execution phase. Calling from and into needs to happen during Gradle’s configuration phase (sometime also called evaluation). Some more info on the build lifecycle can be viewed here. This is root problem causing the behaviour you’re seeing because the tasks are not being configured with anything to copy and therefore never execute because there’s “NO-SOURCE”.
You do not want to call get() on providers during Gradle’s configuration phase. The intent of the property/provider model is to defer accessing the true values until the last possible moment (during task execution). Some more info here.
Calling TaskProvider.get() negates the use of TaskContainer.register() because it causes the tasks to be prematurely realized. Here’s some more info on the task configuration avoidance API.
My personal opinion; plugins should not manipulate the default tasks of a project. That should be a project specific concern left up to the build script.
An example of how you could re-work things:
Configuration avroDefinitions = project.getConfigurations().create("avroDefinitions");
var unzipAvro = project.getTasks().register("unzipAvro");
var c = project.getTasks().register("unzipAvroDefinitions", Copy.class, copy -> {
copy.from(project.getProviders().provider( // (1)
() -> avroDefinitions.getFiles().stream()
.map( zip -> project.zipTree(zip) )
.collect(Collectors.toSet)));
copy.into(project.layout.getBuildDirectory().dir(extension.getIprExtractDir()));
});
unzipAvro.configure(t -> t.dependsOn(c));
The provider is used to defer the resolution of the avroDefinitions configuration so that it doesn’t happen during Gradle configuration phase (sorry, the term “configuration” is overloaded in Gradle).
NOTE; I wrote the code in the forum editor, so I’m sure there’s some errors in it