Publish only if file(s) is present

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

distributions {
    customReport {
        distributionBaseName = 'custom'
        distributionClassifier = 'report'
        contents {
            from "generated/reports"
            include '**/*'
        }
    }
}

publishing {
    publications {
        custom(MavenPublication) {
            // CDGB.SiLvECU.DownloadProject
            if (!fileTree(dir: "generated/reports", include: '**/*').empty) {
                artifact customReport
            }
        }
    }
}

However, I need to set the file structure twice (in the conditional publication and in the distribution); and the conditional check happens at configuration phase.

So, what’s the correct way to implement my use case?

You could have a look at Customizing publishing
There are several customization options shown to customize publishing.

A combination of onlyIf together with a customized publishing task could help you.
With onlyIf on that task you could the, if the report file is present.

1 Like

Oh! :slight_smile:
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:

publishing {
    publications {
        custom(MavenPublication) {
            if (!fileTree(dir: "generated/reports", include: '**/*').empty) {
                artifact customReport
            }
            if (another_condition) {
                artifact anotherDistribution
            }
            if (yet_another_condition) {
                artifact yetAnotherDistribution
            }
            ...
        }
    }
}

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.

distributions {
    customReport {
        distributionBaseName = 'custom'
        distributionClassifier = 'report'
        contents {
            from "generated/reports"
            include '**/*'
        }
    }
}

publishing {
    publications {
        custom(MavenPublication) {
            artifact customReport
        }
    }
}

publishCustomPublicationToMavenRepository.onlyIf{
    !fileTree(dir: "generated/reports", include: '**/*').empty
}

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!

I do not know, if it is better to create a custom task in your situation, as I don´t know about side effects here.
Using dedicated custom tasks probably improves readability of your build sources.