I’m playing around with the new task API promoted in Gradle 4.9, and so far, everything is working as expected – except for the integration with the Project’s ArtifactHandler.
Here is a pattern we use for many projects that produce custom artifacts:
plugins {
id 'base'
}
task producer(type: Producer) {
destFile = layout.buildDirectory.file('fnord')
}
artifacts {
'default' producer.destFile
}
class Producer extends DefaultTask {
@OutputFile
RegularFileProperty destFile = newOutputFile()
@TaskAction
void produce() {
destFile.get().asFile.text = 'Fnord!'
}
}
Migrating this to the new task API should look similar to this:
plugins {
id 'base'
}
tasks.register 'producer', Producer, {
destFile = layout.buildDirectory.file('fnord')
}
artifacts {
'default' tasks.named('producer').get().destFile
}
But this causes the producer
task to be configured eagerly, instead of lazily, ruining the benefits of deferred task configuration.
If I leave out the .get()
, I get an error when the project is configured:
> Could not get unknown property 'destFile' for provider(task producer, class Producer) of type org.gradle.api.internal.tasks.DefaultTaskContainer$TaskCreatingProvider_Decorated.
Not surprising, but what’s the alternative?
In a special case, we want to package the artifact in a Zip task, and then everything
works as expected:
plugins {
id 'base'
}
tasks.register 'producer', Producer, {
destFile = layout.buildDirectory.file('fnord')
}
tasks.register 'packageProducer', Zip, {
from tasks.named('producer')
}
artifacts {
'default' tasks.named('packageProducer')
}
But in some projects, we really don’t want to be building a zip file, but something else (e.g., a multimedia container), which should be registered as the default artifact without being zipped.
Any feedback or solutions would be appreciated!