0

I'd like to create a task that would take as input the @OutputFile of some other task. I know the output file that needs to be taken as input but not the task that creates that output file so that the task that uses the output can dependsOn the output creator. How do I find which task creates that output?

More concretely:

  • $unknownTask creates someKnownOutput
  • newTask uses someKnownOutput
  • newTask would like to dependsOn $unknownTask

How can the value of $unknownTask be found?

Noel Yap
  • 18,822
  • 21
  • 92
  • 144
  • By "output" do you mean console output or a file? If the task marks the file as a TaskOutput, you can probably gain insight by running with -i or -d (I imagine Gradle logs such things). If it doesn't, then Gradle has no way of knowing what the task is doing internally, so you'd kind of be on your own. – Oliver Charlesworth May 04 '17 at 16:42
  • I've edited the question to be more clear (even though I thought the description under `More concretely` was pretty clear). – Noel Yap May 04 '17 at 16:54
  • The regular approach would be to understand how Gradle or the used plugins work to get the neccessary insights. Is the name of the file constant or does it follow a pattern? – Lukas Körfer May 04 '17 at 18:40
  • And how does your `build.gradle` file look like? – Lukas Körfer May 04 '17 at 19:20
  • For this specific instance, I've found what creates that file. Since this isn't the first time I've run into this issue, I was hoping there's a more general solution to this general problem. I think I'm getting close to codifying it, though, and will post the answer when I have. – Noel Yap May 04 '17 at 19:53

1 Answers1

0

The following should print out the tasks whose output is thisIsTheOne:

task findTaskCreatingSpecificOutput() {
  doLast {
    subprojects { subproject ->
      subproject.tasks.findAll { task ->
        task.outputs.getFiles().getFiles().findAll { output ->
          output == 'thisIsTheOne'
        }
      }.flatten().each { println it }
    }
  }
}
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
  • 1
    Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - [From Review](/review/low-quality-posts/16035083) – Martin Tournoij May 05 '17 at 03:29
  • That's an answer to my question. – Noel Yap May 05 '17 at 20:37
  • 1
    Alright, fair enough. Without any text it's a bit hard to tell, tough, especially since someone asked "how does your build.gradle file look like?" in the comments ;-) – Martin Tournoij May 05 '17 at 20:59
  • Text added. Sorry for the ambiguity. – Noel Yap May 05 '17 at 23:30
  • How do you ensure that this task runs after all the other tasks to make sure that the outputs are populated? – mjaggard Feb 07 '23 at 13:43
  • @mjaggard, does https://stackoverflow.com/questions/23880461/how-to-make-tasks-execute-one-after-another address your question? – Noel Yap Feb 08 '23 at 15:30
  • Sort of. I solved it by making this task depend on the task I was debugging at the time - but there doesn't seem to be a general way to run a task last. – mjaggard Feb 08 '23 at 17:35
  • If https://stackoverflow.com/questions/16166605/how-to-execute-a-gradle-task-once-at-the-end-of-subproject-tasks doesn't help in your scenario, try creating a new question. – Noel Yap Feb 08 '23 at 21:42