How best to depend on a single file output by a subproject task

I’m in the process of converting from a single-project build to a multi-project build. My build has a task that takes as an input the single file output of another task (an instance of AbstractArchiveTask in this case, FWIW). In my new multi-project build, these two tasks will be in different projects.

Following the advice in the user documentation on sharing outputs between projects, I’ve made the output of the producer task an artifact of its project. However, I’m not sure of the best way to declare the fact that the consumer depends on the artifact and that it must also be a single file.

Here’s what I have in my consumer (in ultra-distilled form, which I hope illustrates the question without losing anything important; full example here):

val myConfiguration: Configuration by configurations.creating

dependencies {
    myConfiguration(project(mapOf(
        "path" to ":subproject",
        "configuration" to "myFile",
    )))
}

task("useSubprojectArtefact") {
    inputs.files(myConfiguration)
    doLast {
        logger.info(myConfiguration.singleFile.readText())
    }
}

My question is: Is there a way to declare my task depends on myConfiguration.singleFile at configuration time, rather than depending on myConfiguration and then failing at execution time if it contains more than one file? Or is there a better way of getting hold of the output of a task that’s constrained to be a single file, when that task is in another project?

I found this closed issue which refers to a similar problem, and indicates that it’s fixed by file properties, but I can’t work out how to get this to work.

I think what you do is the right thing to do already.
Except that I would not uses the map-y method, but myConfiguration(project(path = ":subproject", configuration = "myFile"))
And that you should not use task("...") as that breaks task-configuration avoidance.

Even if you could express at configuration time that you expect one file in that configuration,
Gradle could at configuration time not determine whether there will be 0, 1, or multiple files in that configuration at execution time.

Thanks for the reassurance!

1 Like