My use case is the following: I have a Task that generates reports under conditions which are unknown to the Gradle project. I would like to publish those report only if they were generated. Otherwise, move on without failing the build.
To some extent, the snippet below does what I want
I’ve gone through the Gradle docs a thousand times already… forward, backward, upside-down…
But I prematurely ruled out the onlyIf + custom task option simply because my use case has multiple artifacts, each with different conditions, like this:
I even got to a point of implementing an extra plugin that creates an empty publication at configuration time and only populates it with artifacts at build time, conditionally.
That surprisingly works, but only when publishing single files. It doesn’t work for publishing outputs of distributions or Zip Tasks, because they are evaluated much earlier in the configuration phase.
Your answer took me back to the basics. Thanks!
Only now I realized that by simply spreading the artifacts into dedicated publications, the problem becomes easily solvable.
For completeness, below is a snippet for a single artifact that works for me. I only need to replicate all that to each artifact. But that’s ok as long as it works as expected.
NOTE: I don’t know if I should have created a custom task that depends on the actual publishing task.
Above, I’m updating the publishing task directly. I haven’t noticed any side-effect to this.
If this is a bad idea, please let me know!