Task dependencies when sharing outputs between projects

If you want to have a consumer subproject use an artifact created by a producer subproject, the Sharing outputs between projects documentation suggests using configurations.

OK, no probs!

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?

See this GitHub repository for the full example.

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.

Thanks for the suggestion @jjustinic . I’ve modified the consumer build.gradle to the following

tasks.register('addQuoteSource', AddQuoteSource) {
    source = 'Scarface (1983)'
    inputFiles.from(configurations.quote.fileCollection())
    dependsOn configurations.quote
}

configurations.quote.fileCollection() is returning an empty list, even though configurations.quote.asPath returns the expected text file location. Here’s my debug output:

configurations.quote.asPath: C:\workspace\gradle-tutorials\task-inputs-and-outputs\sharing-outputs-between-projects\produce-quote\build\quote-with-quotation-marks.txt
configurations.quote.fileCollection().size(): 0

This results in this error during task execution:

Expected file collection to contain exactly one file, however, it contains no files.

I understand that Configuration.fileCollection() resolves the configuration lazily. Is this related?

Any further help is greatly appreciated.

A Configuration is a FileCollection, but the fileCollection methods filter that further. You should be fine with just inputFiles.from(configurations.quote).

Thanks @jjustinic that worked! I’ve updated the example, for anyone that’s interested.