We have a custom project plugin that wraps
GitHub - michel-kraemer/gradle-download-task: 📥 Adds a download task to Gradle that displays progress information with additional
functionality.
Each project configures the task using gradle.properties. As java properties are
raw text strings we are looking into other means of configuring tasks using a
configure-block (and possibly extensions), but cannot unfortunately figure out
how to do this, especially while also correctly wiring input and outputs between
the tasks.
Below is a simplified fictive working example based on gradle.properties. The
task producer produces a set of output files that are configured within each
project’s gradle.properties like so:
project gradle.properties
files = path/to/file1 path/to/file2 etc.
plugin build.gradle:
task producer(type: DefaultTask) {
outputs.files ((project.findProperty('files')?.split(' ') ?: []).collect { x -> "/tmp/producer/${x}" })
doLast {
outputs.files.each { x -> x.text = "${x.name} contents" }
}
}
task consumer(type: Copy) {
def producerTask = project.tasks.getByPath(':producer')
def src = producerTask.outputs.files
src.each { x -> from x }
into '/tmp/consumer'
}
given the files:
/tmp/producer/
/tmp/producer/etc.
/tmp/producer/path/
/tmp/producer/path/to/
/tmp/producer/path/to/file1
/tmp/producer/path/to/file2
this produces:
/tmp/consumer/
/tmp/consumer/file1
/tmp/consumer/file2
/tmp/consumer/etc.
It avoids running the tasks if up-to-date. Each project only needs to configure
gradle.properties. The output of producer is based on the configured value of
files and in this case transformed slightly (by prefixing with /tmp dir). The
value of files is supplied by each project. The output of producer is fed as
input to consumer by the plugin itself, without each project having to wire
this.
We would instead like to configure this using a task Property and/or extensions
instead of gradle.properties. This would allow us to use gradle structured data
instead of parsing text strings. Something like:
project build.gradle:
consumer.configure {
files = [ "path/to/file1", "path/to/file2", "etc." ]
}
or a corresponding configuration using extensions or similar. The idea is to
only require each project to supply the value for files and keep everything else
within the plugin, especially the wiring of input/outputs between the tasks.
We are using Gradle 6.8.3. Any ideas on how to approach this? We have
experimented with using lazy properties but are struggling with figuring out how
to do this.
Thanks for any help,