I’ve implemented this approach in a simple example, and it’s mostly working as I need. Here’s the simplified consumer build.gradle.
configurations {
quote
}
dependencies {
quote(project(path: ":produce-quote", configuration: 'quote'))
}
tasks.register('addQuoteSource', AddQuoteSource) {
source = 'Scarface (1983)'
inputFile = file(configurations.quote.asPath) // how to automatically add the task dependency?
dependsOn configurations.quote //is this the best option?
}
A task in my consumer requires as input the artifact (a text file) created by the producer. To do that I’m querying the path of the quote configuration to get the location of the text file.
The problem with this approach is that I have to manually add a task dependency on the quote configuration. With this in place, Gradle automatically builds the relevant task on the producer subproject when I execute the addQuoteSource task on the consumer subproject.
Is there a way to set this up so that Gradle automatically adds the task dependency, due to the fact that my task depends on the configuration?
Inferred task dependencies only work when you use the Gradle file types. You have both inputs and outputs that are acceptable types, but the mapping between them breaks this. When you write file(configurations.quote.asPath), this converts to a String path before mapping back to a file and the necessary information is lost. If, for example, you were to make the task input a FileCollection and then pull out the single file in the task action, you would not have to declare an explicit task dependency.
configurations.quote.fileCollection() is returning an empty list, even though configurations.quote.asPath returns the expected text file location. Here’s my debug output:
A Configuration is a FileCollection, but the fileCollection methods filter that further. You should be fine with just inputFiles.from(configurations.quote).